Confused about something in the D book relating to precision

Ali Çehreli acehreli at yahoo.com
Mon Dec 5 03:04:05 UTC 2022


On 12/4/22 18:57, thebluepandabear wrote:

 > I am not understanding why Ali said there is a decimal mark if precision
 > is nonzero?
 >
 > How can a number have zero precision?

That "precision" is referring to how many digits are printed after the 
decimal mark in the formatted output.

 > "the required digits after the decimal mark, the number of which is
 > determined
 > by precision (default precision is 6)"

So, if we print with %e, we get 6 digits:

     enum f = 1.23456789;
     writefln!"%e"(f);

Prints

1.234568e+00

There are 6 digits after the decimal point.

Now 3 digits of precision:

     writefln!"%.3e"(f);

Prints

1.235e+00

Now 0 precision, where the decimal point will disappear:

     writefln!"%.0e"(f);

Prints

1e+00

 > Well double has a precision of 15

Different meanings for the same word...

 > I feel like this section was explained poorly and it's confusing.

I have to agree. Nobody really knows these by heart. Once you know 
what's available, you just come back and pick what you need for that 
occasion.

Ali



More information about the Digitalmars-d-learn mailing list