10.0014 + 0.0000123456 = 10.0014?

Hi everyone,

I have a question, for example.
we define real a = 10.0014 and real b = 0.0000123456
then we compute a+b by using FreeFem
we obtain a+b = 10.0014, however the ture is 10.0014123456

so how can we avoid such error during the use of FreeFem. I encounter such error. if t = 10.0014 and dt = 0.0000123456, then the time step cannt be moved into a new time.

I get 10.0014123456.

Hi, thanks for your replying. I run the code, and obtian 10.0012

You are not using a high enough precision for cout.

Hi, thanks a lot.
i define real d = 10.0012 and find that d != c. So when performing operations, you mean the real value of c is 10.0012123456 not 10.0012 ?

Yes, that is correct.

Thanks again for your explanation. I get it and need to consider other direction to correct my code.

Please see Freefem default precision

real a=10.0012;
real b=0.0000123456;
cout << a+b << endl;
cout.precision(16);
cout << a+b << endl;

Hi, thanks for your replying. I get it.

In relation to this question, here is a way to check the internal precision used by FF on any platform

// integer size
// ------------

int i=1;
int n=1;

// Stop when the value is so big that it overflows into the sign bit

while(i>0){
  i*=2;
  n++;
}

cout<<"int size: "<<n<<endl;

// number of decimal places
// ------------------------

real e=1.;
real sum=1.+e;
n=0;

// Stop when the increment is so small that the sum does not change

while(sum!=1.){
  n++;
  e=e/10.;
  sum=1.+e;
}

cout<<"decimal digits: "<<n<<endl;

// maximum power of 10 in a floating-point number
// ----------------------------------------------

real a=1;
n=0;

// a becomes inf when the exponent is too big, so the test becomes inf==inf

while(a*10!=a){
  a*=10;
  n++;
}

cout<<"max exponent: "<<n-1<<endl;

https://www.ljll.fr/lehyaric/ffcs/src/ffref/Basic/precision.m4.htm