c++ - Interval for bisection method -


i've been assigned project determine square root of number without using division or math.h library. upon doing own research i've decided tackle problem using bisection method. used pseudo code portion bisection wikipedia page:

https://en.wikipedia.org/wiki/bisection_method#example:_finding_the_root_of_a_polynomial

to setup algorithm.

my code

#include <iostream> #include <cmath> #include <stdlib.h>   using namespace std;  void __attribute__((weak)) check(double alt_sqrt(double));  //default check function - definition may changed - not graded void __attribute__((weak)) check(double alt_sqrt(double)) {     if(alt_sqrt(123456789.0) == sqrt(123456789.0))cout << "pass\n";     else cout << "fail\n";     return; }  //change definition -  graded different check function double my_sqrt(double x) {     int = 0;     double = 0.0;         // lower bound     double b = x + 1;       // upper bound     double c = 0.0;         // guess square root     double error = 0.00001;     double fc = 0.0;     while(i < 10000)     {         c = (a+b)*0.5;         fc = c * c - x;         if(abs(fc) < error || (b-a)*0.5 < error)        // check solution         {             cout << "square root is: " << c << endl;             break;         }         if(fc < 0)      // setup new interval         {             = c;             cout << "a is: " << << endl;         }         else b = c;         cout << "b is: " << b << endl;         i++;     }     return c; }  //do not change function int main() {     check(my_sqrt);     return 0; } 

the output getting professor's test case in main is

square root is: 1.23457e+08 fail 

when correct output should be

square root is: 11,111.11106 pass 

i believe going wrong in way setup new intervals. thinking if difference between 2 values negative, need push lower bound up, , if difference positive, need bump upper bound down.

i appreciate advice y'all give me. thank time.

the condition fb - fa < 0 wrong because ignoring floating-point errors, fa < fb, a * - x < b * b < x true 0 <= < b.

changing condition fc < 0 improved accuracy, unfortunately improvement coundl't make program print "pass". improve accuracy have program print "pass", delete harmful breaking part

    if(abs(fc) < error || (b-a)*0.5 < error)        // check solution     {         cout << "square root is: " << c << endl;         break;     } 

removing harmful breaking , adding line

cout << "square root is: " << c << endl; 

just before

return c; 

gave me

square root is: 11111.1 pass 

but unfortunately not want. have want printed,

#include <iomanip> 

should added , printing part should be

std::cout.imbue(std::locale("")); cout << fixed << setprecision(5) << "square root is: " << c << endl; 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo