std.conv.to vs. casting

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Jul 4 10:24:17 PDT 2013


On Thu, Jul 04, 2013 at 06:43:16PM +0200, Joseph Rushton Wakeling wrote:
> On 07/04/2013 06:16 PM, Ali Çehreli wrote:
> > I am commenting without fully understanding the context: Both size_t
> > and double are 64 bit types on a 64-bit system. double.mant_dig
> > being 53, converting from size_t to double loses information for
> > many values.
> 
> Oh, bugger.  You mean that because it needs space to store the
> exponent, it has a reduced number of significant figures compared to
> size_t?
[...]

Yes. See:

http://en.wikipedia.org/wiki/Double-precision_floating-point_format

Of the 64 bits, only 53 are available for storing the mantissa (well,
actually 52, but the first bit is always 1 except when the exponent is
zero so it's not stored -- you get it for free). Of the remaining bits,
11 are reserved for storing the exponent, and the last for storing the
sign.

So the maximum precision a double can have is 53 bits. If you have a
value that requires more than that, the representation will be inexact.


T

-- 
Programming is not just an act of telling a computer what to do: it is
also an act of telling other programmers what you wished the computer to
do. Both are important, and the latter deserves care. -- Andrew Morton


More information about the Digitalmars-d-learn mailing list