Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
martinr448 opened this issue Apr 19, 2017 · 3 comments
Closed

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

martinr448 opened this issue Apr 19, 2017 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@martinr448
Copy link

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:

  • SR-4800 Float.init?(exactly: UInt32) does not return nil for UInt32.max or other un-representable values
  • SR-7056 Float(exactly: Int32) and Double(exactly: Int64) do not work properly

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
Copy link
Member

@swift-ci create

@swift-ci
Copy link
Collaborator

Comment by Ian Partridge (JIRA)

#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
Copy link
Collaborator

xwu commented Feb 14, 2018

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

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants