www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - precision test results

Maybe interesting for other people
%.8e format holds full precision information for floats
making it a good %a contender

float have max 24bit precision

2^-24 = 0.000000059604644775390625
2^-23 = 0.00000011920928955078125

to distinguish between these two you only need a precision of 8.

Stupid test:

//slightly arbitrarily chosen starting points
float[8] fr = [ 0f, 0.0001f, 0.1f, 0.5f, 1f, 10f, 100_000f, 
1_000_000_000_000_000f ];
for(int ii=0;ii<fr.length;ii++)
  float f=fr[ii];
  //loop over all possibilities
  for (long i=0; i<pow(cast(real)2,24); i++)
    string s = format("%.8e",f);
    if(i%1_000_000 == 0)writefln(i,` `,s);
    float f2 = to!(float)(s);
    if(f != f2) {writefln(`!!!`, i,` `,s," %a %.100g",f,f); return;}

No exclamation marks in the output.
Thus format "%.8e" seems to hold full precision information
This format is only one char longer than %a format,

btw is there a float hex string to float converter? 
Jul 26 2009