4

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?

Chris
  • 44,602
  • 16
  • 137
  • 156
iluvatar
  • 872
  • 10
  • 21
  • 1
    What's exactly is a problem here? I can't see any difference between before and after. 1.0e-318 actually `IS` 9.99999e-319 – GreenScape Sep 30 '11 at 14:20
  • @GreenScape: The problem seems to be that normal doubles only go down to ~2.2e-308, and that all other results are denormalized floats, going down to ~4.9e-324. A lot of programs see to not understand denormalized floats. – PlasmaHH Sep 30 '11 at 14:28

1 Answers1

11

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.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
thiton
  • 35,651
  • 4
  • 70
  • 100
  • Yes, I think that could be the solution, by making a comparison between the actual double minimum and the current number. Thanks for pointing out the info about subnormals numbers. – iluvatar Sep 30 '11 at 19:37