digitalmars.D.learn - Confused about something in the D book relating to precision
- thebluepandabear (32/32) Dec 04 2022 Hello guys,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/30) Dec 04 2022 That "precision" is referring to how many digits are printed after the
- thebluepandabear (1/5) Dec 04 2022 Thanks for your effort :-) It helped clear things up.
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
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+00Well double has a precision of 15Different 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
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. AliThanks for your effort :-) It helped clear things up.
Dec 04 2022