integral to floating point conversion
Guillaume Boucher via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jul 3 02:52:38 PDT 2016
On Sunday, 3 July 2016 at 09:08:14 UTC, Ola Fosheim Grøstad wrote:
> 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))
This is the correct answer for another definition of "precisely
convertible", not the one Andrei gave.
More information about the Digitalmars-d
mailing list