Bug 20708 – result of cast not specified when value is out of range

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-03-29T15:00:39Z
Last change time
2022-08-15T13:20:16Z
Assigned to
No Owner
Creator
johanengelen
See also
https://issues.dlang.org/show_bug.cgi?id=21359

Comments

Comment #0 by johanengelen — 2020-03-29T15:00:39Z
The specification says nothing about what the result of a numeric cast is when the value is outside the receiving type's range. For example: ``` uint a = 260; auto b = cast(ubyte) a; float f = 1.203125f * 255.0f; auto zz = cast(ubyte) f; ``` Not having the result specified currently means that it is treated as UB by LDC and GDC.
Comment #1 by nick — 2022-08-15T13:17:00Z
Issue 21279 for integers now has a pull request. Issue 22527 for float -> int was fixed. Closing in favour of those. *** This issue has been marked as a duplicate of issue 21279 ***
Comment #2 by nick — 2022-08-15T13:20:16Z
> Issue 22527 for float -> int was fixed. See: https://dlang.org/spec/expression.html#cast_floating