swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.68k stars 10.38k forks source link

[SR-4634] Error in exact floating point initializers #47211

Closed martinr448 closed 6 years ago

martinr448 commented 7 years ago
Previous ID SR-4634
Radar rdar://problem/31836766
Original Reporter @martinr448
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 1 | |Component/s | Standard Library | |Labels | Bug | |Assignee | @natecook1000 | |Priority | Medium | md5: e3cafae41d33e915edd76d83f2a2b2b5

is duplicated by:

Issue Description:

The exact conversion from integers to floating point types succeeds even if precision is lost. Example:

if let f = Float(exactly: Int64(9000000000000000001)) {
    print(f, Int64(f))
    // 9e+18 9000000202358128640
}

The reason is a wrong preprocessor condition at FloatingPointTypes.swift.gyb:L840:

%   if srcBits < SignificandBitCount:
    if ${That}(self) != value {
      return nil
    }
%   end

which means that the test it not performed if the integer has more bits than the mantissa of the floating point type.

It should be if srcBits >= SignificandBitCount: or if srcBits > SignificandBitCount: instead.

airspeedswift commented 7 years ago

@swift-ci create

swift-ci commented 7 years ago

Comment by Ian Partridge (JIRA)

https://github.com/apple/swift/pull/11311 is open to fix this.

Looking forward to getting this fixed as it blocks improving the test coverage for NSNumber bridging in swift-corelibs-foundation.

xwu commented 6 years ago

Looks like this was resolved in #12739 (by Nate).