Spec on casting to integer with smaller range than value

kinke noone at nowhere.com
Sat Mar 28 15:33:35 UTC 2020


On Saturday, 28 March 2020 at 12:09:49 UTC, Johan wrote:
> float f = 1.203125f  * 255.0f;
> auto zz =  cast(ubyte) f;
> // https://github.com/ldc-developers/ldc/issues/3237

The 2nd cast, float to int, should be 
https://dlang.org/spec/expression.html#cast_expressions, §7: 
'Casting a floating point value to an integral type is the 
equivalent of converting to an integer using truncation.' It 
doesn't specify the behavior if that integer doesn't fit; that 
should probably be explicitly mentioned as undefined behavior, to 
account for the observed LLVM/gcc behavior.


More information about the Digitalmars-d mailing list