formatting a float or double in a string with all significant digits kept

Jon Degenhardt jond at noreply.com
Thu Oct 10 21:13:05 UTC 2019


On Thursday, 10 October 2019 at 17:12:25 UTC, dan wrote:
> Thanks also berni44 for the information about the dig attribute,
> Jon for the neat packaging into one line using the attribute on 
> the type.
> Unfortunately, the version of gdc that comes with the version 
> of debian
> that i am using does not have the dig attribute yet, but 
> perhaps i can
> upgrade, and eventually i think gdc will have it.

Glad these ideas helped. The value of the 'double.dig' property 
is not going to change between compilers/versions/etc. It's 
really a property of IEEE 754 floating point for 64 bit floats. 
(D specified the size of double as 64).  So, if you are using 
double, then it's pretty safe to use 15 until the compiler you're 
using is further along on versions. Declare an enum or const 
variable to give it a name so you can track it down later.

Also, don't get thrown off by the PI is a real, not a double. D 
supports 80 bit floats as real, so constants like PI are defined 
as real. But if you convert PI to a double, it'll then have 15 
significant bits of precision.

--Jon


More information about the Digitalmars-d-learn mailing list