www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Confused about something in the D book relating to precision

reply thebluepandabear <therealbluepandabear protonmail.com> writes:
Hello guys,

(Noob question.)

I would appreciate some help.

I am reading Ali's book on D language, and I am up to page 127 -- 
talking about format specifiers.

He says the following about the '%e' (exponent) specifier:

"e: A floating point argument is printed according to the 
following rules.
  - a single digit before the decimal mark
  - a decimal mark if precision is nonzero"

I am not understanding why Ali said there is a decimal mark if 
precision is nonzero?

How can a number have zero precision? I thought all numbers have 
a precision of greater than 0. I am confused what this means :/

Then he says:

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

Well double has a precision of 15, and when I print the following 
there aren't 15 digits after the decimal mark:

```D
	double value = 123.456789;
	writeln("precision: ", double.dig);
	writefln("with e: %e", value);
```

Output:

```
precision: 15
with e: 1.234568e+02
```

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




Help would be appreciated.
Dec 04 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Dec 04 2022
parent thebluepandabear <therealbluepandabear protonmail.com> writes:
 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
Thanks for your effort :-) It helped clear things up.
Dec 04 2022