Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output precision is higher than double precision

I am printing some data from a C++ program to be processed/visualized by ParaView, but I am having a problem with floating point numbers. Paraview supports both Float32 and Float64 data types. Float64 is equivalent to double with the typical limits +/-1.7e +/- 308. But, my code is printing numbers like 6.5e-318. This is throwing errors in ParaView when reading the data. I have verified that rounding those smalls numbers to zero make the errors in ParaView disappear. I am not sure why I have such "high precision" output, maybe is because some numbers are stored in higher precision than double. For example, the following code reproduces the same behavior on my system:

#include <iostream>
int main(void)
{
  const double var1 = 1.0e-318, var2 = 1.5e-318;
  std::cout << 1.0e-318 << std::endl; 
  std::cout << var1 << std::endl; 
  std::cout << var1 - var2 << std::endl; 
  std::cout.setf(std::ios_base::fixed | std::ios_base::scientific, std::ios_base::floatfield);
  std::cout << 1.0e-318 << std::endl; 
  std::cout << var1 << std::endl; 
  std::cout << var1 - var2 << std::endl; 

  return 0;
}

My output is:

9.99999e-319
9.99999e-319
-4.99999e-319
9.99999e-319
9.99999e-319
-4.99999e-319

My system is a Mac OS X Snow Leopard and I tested the above with GCC 4.2 and GCC 4.6 with the flags -m32, -m64 and -ffloat-store (not sure if this is useful).

Actually the output for me is fine, but for ParaView is not. I just want to know why I have this difference. I am very likely ignoring something related with floating point numbers which could be important. Could you please please give me some clue about this output/numerical behavior for doubles?

like image 262
iluvatar Avatar asked Dec 21 '22 08:12

iluvatar


1 Answers

Subnormal numbers, i.e. numbers with the smallest-possible exponent and leading zeros in the fraction, can be smaller than 1E-308, down to 1E-324. You can probably filter them out using std::numeric_limits.

like image 161
thiton Avatar answered Jan 07 '23 23:01

thiton