integral to floating point conversion

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Sat Jul 2 14:35:31 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


bool isConvertible(T) (long n) if (is(T == float) || is(T == 
double)) {
   pragma(inline, true);
   static if (is(T == float)) {
     return (((n+(n>>63))^(n>>63))&0xffffffffff000000UL) == 0;
   } else {
     return (((n+(n>>63))^(n>>63))&0xffe0000000000000UL) == 0;
   }
}


More information about the Digitalmars-d mailing list