0

I am trying to get the fixed points for the tent equation. With the given initial conditions, the solution must be 0.6. Everything works fine when I use float for x0, but when I define x0 as a double, the solution changes to 0.59999 in 55th iteration, which causes further changes in the next iteration and so on. Why is there such a difference while choosing the data types?

using namespace std;
#include <iostream>
main()

{
    double x0=.6;
    for (int i=0;i<100;i++)
    {
        if(x0<.5)
            x0=1.5*x0;
        else
            x0=1.5*(1-x0);

        cout << i << "\t" << x0 << endl;

    }

}

I have posted an image of the results. Comparison of solutions - Float and Double

Arjun
  • 3
  • 1
  • 1
    You're being tricked by rounding. Adding `cout << setprecision(100)` will print all of the digits. And visit the question [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken). – molbdnilo Feb 15 '17 at 11:58
  • 1
    It is better to add your textual results as text if it is possible. The image you attached could be written in a code block of text in your question. – Hamid Rouhani Feb 15 '17 at 12:13

1 Answers1

2

The real value of the 55-th iteration is 0.5999994832150543633275674437754787504673004150390625 when a double is used and 0.60000002384185791015625 for a float (on my system).

The difference between the two is in how precise the numbers are and the rounding is what throws you off.

BTW, neither of the two values is absolutely accurate, they are just close enough approximations and nothing more. With the double being "more precise".


UPDATE

After a few comments back and forth it turned out that there was no need for floating-point arithmetic to be gin with. Integers (slightly modified) do just fine and do not introduce any rounding.

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
  • Thank you. I am trying to simulate dynamical systems where chaos comes into play. The equations are very sensitive to initial conditions. A slight change can cause huge changes in the future iterations. Is there a way around this problem? – Arjun Feb 15 '17 at 12:10
  • Your question really depends on an **exact** definition of "this problem". Is the problem that the `float` behaves differently from `double`? Or is it that the precision of a `double` is not good enough for your needs? Or is the problem in the fact that binary numbers are not a good choice to represent decimal fractions? – YePhIcK Feb 15 '17 at 12:13
  • My concern is that the solutions are deviating from its exact value (0.6 in this case) at a certain iteration causing the next iterations to return wrong solutions. Is there a way to obtain the exact value (0.6) at all iterations ? – Arjun Feb 15 '17 at 12:20
  • Yes, there is a way. Do not use floating-point arithmetic. Multiply everything by `10` and use integers. Simple, fast, absolutely precise. – YePhIcK Feb 15 '17 at 12:23
  • Brilliant! Thank you. – Arjun Feb 15 '17 at 12:25
  • Keep in mind that since you are still multiplying by`1.5` (which is not an integer) that solution would work for the initial number `6` but not for, say, `5`. – YePhIcK Feb 15 '17 at 12:28
  • I get "4,6" from the backend. Only for a particular user. How could this happen? – Aman Verma Sep 23 '19 at 15:24