On 07/03/2016 05:52 AM, Guillaume Boucher wrote:
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.

Well to be more precise here's what I'm looking for. When you compare an integral with a floating point number, the integral is first converted to floating point format. I.e. for long x and double y, x == y is the same as double(x) == y.

Now, say we want to eliminate the "bad" cases of this comparison. Those would make it non-transitive. Consider two distinct longs x1 and x2. If they convert to the same double y, then x1 == y and x2 == y are true, which is contradictory with x1 != x2.


Andrei

Reply via email to