-3

I've made a question yesterday that got taken as a duplicate, I think by mistake. I need to store 10e-16 in a variable. This alone works, but as soon as I need to subtract it from another double value, the number doesn't change at all. It only starts working if I change the value from 10e-16 to somothing like 10e-4. Any lower numbers don't seem to work. I've tried using long double, but to no avail. I don't need cout.precision() because I don't need to print the number yet, I need it for a loop.

long double x1 = -400, epsilon = 0.000000000000001;
long double x2 = -400 + epsilon;
vector <vector <double> > intervals;
vector <double> interval;


while(x1<=650){
    interval.clear();
    if(f1(x1)<0){
        while(f1(x2) <= 0){
            x2 += epsilon;
        }
        while(f1(x1)<0){
            x1 += epsilon;
        }
        x1 -= epsilon;
        interval.push_back(x1);
        interval.push_back(x2);
        intervals.push_back(interval);
        x1=x2;
        x2=x1 + epsilon;
    }

This is what the loop looks like. The function f1 returns the value of a nonlinear equation. I've tried using GMP, but I've got problems with installing it, which I addressed in a different question. Is there any way to make this work without using GMP?

eilchner
  • 103
  • 1
  • 2
  • 8
  • 1
    Well, you can always perform all the operations by yourself in binary using as many bits as you want... – XCS Nov 27 '14 at 20:57
  • You were told a `double` can hold only a fixed number of digits. So it wasn't about output options. You can try to create your own fixed decimal number type though. – πάντα ῥεῖ Nov 27 '14 at 20:58
  • You might find this reference more approachable: http://blog.reverberate.org/2014/09/what-every-computer-programmer-should.html I urge you to stop what you are doing and really understand that – David Heffernan Nov 27 '14 at 21:15

1 Answers1

4

Suppose you represent a large integer, say 123,456,123,456, to six significant figures. You get:

123,456,000,000

Then you add 1 to it, and get the result 123,456,000,001. If you represent this to six significant figures, you get:

123,456,000,000

If you add 1 again to 123,456,000,000 then the result, to six significant figures, is:

123,456,000,000

and so on forever.

If the number you're adding is below the threshold of the numbers you consider significant, you can see you can add that number as many times as you like, and your result will never change. This is essentially what's happening with your attempt to subtract a very small double from a very large one.

Crowman
  • 25,242
  • 5
  • 48
  • 56