Thread overview
Precision, new prices
Jul 25, 2009
Saaa
Jul 25, 2009
downs
Jul 25, 2009
Saaa
July 25, 2009
I thought my previous precision question was so retarded it didn't need any explanation; anybody here could easily tell me my fault in reasoning.

Here is my question again but this time with examples.

Is there some formatting which lets me print out a floating point in full precision?

Observation: 6 digits is not full precision:

float f=3.4028234; // 0x7f7f ffff
writefln("%.8g",f); // prints 3.4028234
writefln(f.dig); // prints 6
writefln(3.4028234f.dig); // prints 6

Shouldn't f.dig print out 8?
.dig =  number of decimal digits of precision

I still think I don't get something, somewhere.


July 25, 2009
Saaa wrote:
> I thought my previous precision question was so retarded it didn't need any explanation; anybody here could easily tell me my fault in reasoning.
>
> Here is my question again but this time with examples.
>
> Is there some formatting which lets me print out a floating point in full precision?
>

Here's an incredibly simple hack.

import std.stdio, std.string;

string ftoaFull(float f) {
  if (f < 0) return "-" ~ ftoaFull(-f);
  auto start = f;
  auto res = toString(cast(int) f) ~ ".";
  while (true) {
    f -= cast(int) f;
    f *= 10;
    res ~= "0123456789"[cast(int) f];
    // The critical step
    if (cast(float) res.atof() == start) return res;
  }
}

void main() {
  writefln(ftoaFull(0f));
  writefln(ftoaFull(-5.234));
  writefln(ftoaFull(1f / 3f)); // problematic
}

Output:

0.0
-5.234
0.33333334

And there you have it.
July 25, 2009
Ok it finally hit me :(
The 24 fraction bits in a float aren't used like an integer (2^24 giving max
7 decimal digits precision)
they are used by halving the value of the previous bit: 1, 0.5, 0.25 ...
like fractions !!!
this of course has a much wider decimal range (something like 20 or so)
because not all numbers are
represented.

Thus, the formatting %.100g and downs code both just give the correct answer .

Now, there are 2^24 different floats being represented (ignoring exponent)
so the question for me now is:
What is the minimal (decimal) representation of a float for which the
following holds:

float f; //for all possible floats
string s = format(f);
float f2 = to!(float)(s);
assert(f == f2);

Does your second code hold, downs?