Why are some casts from floating point to integral done differently from others?

Don nospam at nospam.com
Mon Mar 15 01:10:05 PDT 2010


Aldo Nunez wrote:
> I was testing evaluating cast expressions for a debugger I'm working on, when I came across this bit of behavior that hopefully Walter or someone else can explain.
> 
> Most conversions in D from floating point to integer are done with the help of the x87 FP instructions: load FP number, store as integer. This is understandable.
> 
> The following conversions are done differently:
> - float, double, real -> uint
> - float, double -> ulong
> 
> They are done with the help of runtime functions called DBLULNG and DBLULLNG respectively.

This happens because there are no built-in functions for converting from 
float to unsigned. Values between int.max and uint.max need to be 
treated specially.

What I don't like about them is that they produce results that are 
inconsistent with the way the other conversions are handled. For 
example, (cast(uint) -1.0) becomes 0, whereas (cast(ulong) -1.0L) 
becomes 0xFFFFFFFFFFFFFFFF and (cast(ushort) -1.0L) becomes 0xFFFF.

The cast(uint) case is clearly a bug. Please file it in Bugzilla.



More information about the Digitalmars-d mailing list