0

Hi guys I'm stuck with this homework where I need to find the root of equation using Bisection method with precision 10^-20 aka 0.00000000000000000001 so at first I though it was cause I wasn't using long double and also L at the end of the numbers, however even when I use it my last 3 digits are not correct, for the code that is given below ask you to give the number for a in my case is 5 , so I get 2.3227751229355622087

while the correct answer should be 2.3227751229355622988, I really can't find my mistake , will be happy if some1 assist me with this problem.

For your reference, here's a description and illustration of the Bisection method.

Here's my code:

#include<iostream>
#include<cmath>
#include<math.h>
#include<iomanip>

using namespace std;

long double f(long double   x, long double a);
long double F = 123456L % 100L;

long double f(long double  x, long double a)
{
    long double  sum = pow(x, 5) - a*x - F;
    return sum;
}

int main()
{
    cout.setf(ios::fixed);
    long double a, b, c, fa, fb, fc;
    long double e;
    long double aa;
    bool flag = true;
    while (cin >> aa)
    {
        cout.precision(19);
        flag = true;
        a = 0L;
        b = 10L;
        e = 0.00000000000000000001L;

        if (f(a, aa)*f(b, aa)>0)
        {
            flag = false;
        }

        while(fabs(a-b)>=e){
            c = (a + b) / 2.0L;
            fa = f(a, aa);
            fb = f(b, aa);
            fc = f(c, aa);

            if (fc == 0)
            {       
                break;
            }

            if (fa*fc>0)
            {
                a = c;
            }
            else if (fa*fc<0)
            {
                b = c;
            }
        } 

        if (flag == true)
        {
            cout  << c << endl;
        }
        else 
        {
            cout << "NO SOLUTION" << endl;
        }
    }
    return 0;
 }
sg7
  • 6,108
  • 2
  • 32
  • 40
DavidsAmause
  • 57
  • 1
  • 9
  • 6
    You need to know how IEEE floating point numbers work. Double precision only has 17 digits of precision. – duffymo Dec 06 '16 at 16:15
  • Isn't teacher supposed to explain in the class how the problem should be solved? – Dialecticus Dec 06 '16 at 16:20
  • related: http://stackoverflow.com/questions/588004/is-floating-point-math-broken – drescherjm Dec 06 '16 at 16:29
  • Also possibly: http://stackoverflow.com/questions/476212/what-is-the-precision-of-long-double-in-c – drescherjm Dec 06 '16 at 16:32
  • 1
    See also: [What Every Computer Scientist Should Know About Floating Point](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Thomas Matthews Dec 06 '16 at 16:34
  • 1
    Note that [even `long double` only has 18 decimal digits of precision](http://coliru.stacked-crooked.com/a/491a974597ef079c) on most machines. – mindriot Dec 06 '16 at 17:11
  • Yep he explained and said use long double ... , so its not working now how to solve it .. :( – DavidsAmause Dec 06 '16 at 17:18
  • 2
    I would say it is working perfectly. Your precision requirement is wrong headed. – duffymo Dec 06 '16 at 17:34
  • What you mean will you make it more clear? – DavidsAmause Dec 06 '16 at 18:03
  • what is the target range? if too far from your 10^-20 then doubles will not be enough. There are ways to boost the performance. For example you can use 2 or more doubles per single value so one is used for big values and the other for small and their sum will be the final value. See edit1 in: [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](http://stackoverflow.com/a/28020934/2521214) you need to rewrite all used math operations to match this number representation. Another option is to use high precision number lib ... – Spektre Dec 07 '16 at 09:07
  • if you go with first option then you can forget about `pow` and printing the values will be a bit tricky but not too much if you use as barrier value `10^n` then just write digits from each double separately (do not use the sum) – Spektre Dec 07 '16 at 09:10
  • Zombie. Zombie, ie, ie, ie. What's in your head, Zombie... – Michael Dorgan Mar 09 '18 at 18:41

1 Answers1

-3

The problem was that I was using the wrong compiler (Visual Studio).

As soon as installed another compiler, there wasn't any problem with the 20 digit precision. As I investigated more, it appears that some compilers have a limit after the decimal to 14 or 15. If c++ refuses to round your number to higher precision change the compiler :)

Juan Leni
  • 6,982
  • 5
  • 55
  • 87
DavidsAmause
  • 57
  • 1
  • 9
  • Your problem has nothing to do with C++ or compilers, and everything to do with how numbers are represented. – Daniel Kamil Kozar Mar 13 '18 at 19:41
  • Maybe but it still a VS problem, because after I compile it with another program that doesn't automatically open the app , and then I run the exe everything was working as it suppose to do. – DavidsAmause Mar 21 '18 at 13:11