D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20708 - result of cast not specified when value is out of range
Summary: result of cast not specified when value is out of range
Status: RESOLVED DUPLICATE of issue 21279
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-03-29 15:00 UTC by johanengelen
Modified: 2022-08-15 13:20 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description johanengelen 2020-03-29 15:00:39 UTC
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 Nick Treleaven 2022-08-15 13:17:00 UTC
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 Nick Treleaven 2022-08-15 13:20:16 UTC
> Issue 22527 for float -> int was fixed.

See:
https://dlang.org/spec/expression.html#cast_floating