integral to floating point conversion

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 3 02:08:14 PDT 2016


On Saturday, 2 July 2016 at 20:17:59 UTC, Andrei Alexandrescu 
wrote:
> So what's the fastest way to figure that an integral is 
> convertible to a floating point value precisely (i.e. no other 
> integral converts to the same floating point value)? Thanks! -- 
> Andrei

If it is within what the mantissa can represent then it is easy. 
But you also have to consider the cases where the mantissa is 
shifted.

So the real answer is:

n is an unsigned 64 bit integer

mbits = representation bits for mantissa +1

tz = trailing_zero_bits(n)
lz = leading_zero_bits(n)

assert(mbits >= (64 - tz - lz))



More information about the Digitalmars-d mailing list