Bug 17437 – Incorrect range check when implicitly converting integer literals to float/double

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-05-25T23:35:06Z
Last change time
2018-01-25T18:21:44Z
Assigned to
No Owner
Creator
kinke

Comments

Comment #0 by kinke — 2017-05-25T23:35:06Z
The integer value is cast to float/double, and that value is then compared for equality against the original integer value. The comparison is thus performed incorrectly as floating-point type with a potentially smaller integer range. `long.max` (2^63-1) cannot be represented as double, it's rounded up to 2^63, but a `double f = 0x1.0p+63` equals a `long i = long.max` as the comparison is performed as double (and thus `i` rounded up too). See https://github.com/dlang/dmd/pull/6831.
Comment #1 by github-bugzilla — 2018-01-25T18:21:42Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/ab9e5e2dc67d33552091185910fe98d12ddb312c Fix issue 17437 - convertibility of integer literals to float/double After casting the integer value to the floating-point type, make sure to backcast it to an integer for the equality check, so that the comparison is performed as integer instead of the fp type with potentially smaller integer range. This prevents inexact implicit convertibility. https://github.com/dlang/dmd/commit/5f25f05f538053b1c9e260db3fe237e2a258ada8 Merge pull request #6831 from kinke/int2fp Fix Issue 17437 - Incorrect range check when implicitly converting integer literals to float/double merged-on-behalf-of: Rainer Schuetze <[email protected]>