int main(void) { double a1 = 0.0, b1 = 1.0; double a2 = 1.0, b2 = 3.0; double eps = 0.000001; double d1 = dichotomy(f1, eps, a1, b1); double i1 = iteration(f1_it, eps, a1, b1); double n1 = newton(f1, f1_pr, eps, a1, b1); double d2 = dichotomy(f2, eps, a2, b2); double i2 = iteration(f2_it, eps, a2, b2); double n2 = newton(f2, f2_pr, eps, a2, b2); printf("Точность: %.6f\n", eps); printf("+-----------+----------+---------------+-----------------------+-----------+----------+---------+\n"); printf("| Уравнение | Отрезок | Базовый метод | Прибл. значение корня | Дихотомии | Итераций | Ньютона |\n"); printf("+-----------+----------+---------------+-----------------------+-----------+----------+---------+\n"); printf("| 1 | [0, 1] | Ньютона | 0.8814 |%.9f|%.8f|%.7f|\n", d1, i1, n1); printf("+-----------+----------+---------------+-----------------------+-----------+----------+---------+\n"); printf("| 2 | [1, 3] | Дихотомии | 1.3749 |%.9f| - |%.7f|\n", d2, n2); printf("+-----------+----------+---------------+-----------------------+-----------+----------+---------+\n"); return 0; }
int main() { /* * Вычисляем ширину эпсилон. */ int epswidth = ceil(-log10(REAL_EPSILON)); printf( "target_1 = 0, x = %.*f; dichotomy, x ∈ [%f, %f]\n", epswidth, dichotomy(target_1, TARGET_1_A, TARGET_1_B), TARGET_1_A, TARGET_1_B ); printf( "target_1 = 0, x = %.*f; iterations, x ∈ [%f, %f]\n", epswidth, iterations(target_1_xfx, TARGET_1_A, TARGET_1_B), TARGET_1_A, TARGET_1_B ); printf( "target_1 = 0, x = %.*f; newton, x ∈ [%f, %f]\n", epswidth, newton(target_1, target_1_derivative, TARGET_1_A, TARGET_1_B), TARGET_1_A, TARGET_1_B ); printf( "target_2 = 0, x = %.*f; dichotomy, x ∈ [%f, %f]\n", epswidth, dichotomy(target_2, TARGET_2_A, TARGET_2_B), TARGET_2_A, TARGET_2_B ); printf( "target_2 = 0, x = %.*f; iterations, x ∈ [%f, %f]\n", epswidth, iterations(target_2_xfx, TARGET_2_A, TARGET_2_B), TARGET_2_A, TARGET_2_B ); printf( "target_2 = 0, x = %.*f; newton, x ∈ [%f, %f]\n", epswidth, newton(target_2, target_2_derivative, TARGET_2_A, TARGET_2_B), TARGET_2_A, TARGET_2_B ); return (0); }
void c_seq::query(uint32 datetime, wisdom_IOStream& io) { SEQ_INIT(); if (is_clean()) { wisdom_IOStream os = new c_ostream_array; remove(os); } c_rlock lock(&m_lock); uint32 begin_index = seq_begin_index(); uint32 end_index = m_seq_head.g_index(); /*int last_diff = 0; uint32 last_key = 0;*/ uint32 ibegin = 0; uint32 iend = 0; map<int, uint32> mfind; int diff = 0; while (true) { if (diff > 0) { ibegin = ibegin; iend = ibegin + (iend - ibegin) / 2; } else if (diff < 0) { ibegin = ibegin + (iend - ibegin) / 2; iend = iend; } else { ibegin = begin_index; iend = end_index; } uint32 keyId = dichotomy(datetime, ibegin, iend, diff, io); if (keyId == 0) break; //exists if (diff == 0) { io->push(ZRESULT_OK); io->push(itostr(keyId)); return; } mfind.insert(make_pair(diff, keyId)); //no data find if (keyId == ibegin || keyId == iend) { break; } } for (map<int, uint32>::iterator pos = mfind.begin(); pos != mfind.end(); ++pos) { if (pos->first > 0 || pos == --mfind.end()) { io->push(ZRESULT_OK); io->push(itostr(pos->second)); return; } } io->push(ZRESULT_ERROR); }