-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[SR-3887] Decimal(n) fails to initialise properly for many integers within range. #4110
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
Comments
Irksome. Possibly because it's being initialized via init(_: Double). Will check when I have some spare time. |
I only really tested the numbers around @xwu SR-3125 is about it being |
Comment by Jay Abbott (JIRA) I think this probably is a duplicate of SR-3130, or at least has the same internal cause. That report is about the Code to generate the heatmap: import Foundation
import CoreGraphics
let samplesPerPixel = 1024
let width = 256
let height = 256
var heatmap = UnsafeMutablePointer<UInt8>.allocate(capacity: width * height)
var counter = 0
for y in 0 ..< height {
for x in 0 ..< width {
var sum = 0
for i in 0 ..< samplesPerPixel {
let a = Decimal.init(counter)
if a.description.range(of: ".") != nil {
sum += 1
}
counter += 1
}
let heat = UInt8(255.0 * Double(sum) / Double(samplesPerPixel - 1))
heatmap[y * height + x] = heat
}
}
let provider = CGDataProvider(dataInfo: nil, data: heatmap, size: width * height, releaseData: { (a, b, c) in })!
let img = CGImage(width: width, height: height,
bitsPerComponent: 8, bitsPerPixel: 8,
bytesPerRow: width,
space: CGColorSpaceCreateDeviceGray(),
bitmapInfo: .byteOrderMask,
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: .defaultIntent)!
let url = NSURL(fileURLWithPath: "Decimal-failure-heatmap.png")
let dest = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, nil)!
CGImageDestinationAddImage(dest, img, nil)
CGImageDestinationFinalize(dest) |
j-h-a (JIRA User) As documented in SR-3125, the integral initializers all internally call |
Comment by Jay Abbott (JIRA) I see - yeah almost certainly a duplicate then. |
This has been fixed in both macOS and Linux versions as the initialiser now avoids the conversion to For sclf it was fixed in: #1550 and for macOS the fix was in swiftlang/swift#25745 Running the above test program on both macOS and Linux outputs: Starting at 0: Found 0 fails in 10000000 (0.0%)
Starting at 59386023: Found 0 fails in 10000000 (0.0%)
Starting at 264109865: Found 0 fails in 10000000 (0.0%)
Starting at 364109865: Found 0 fails in 10000000 (0.0%)
Starting at 1642385017: Found 0 fails in 10000000 (0.0%) |
The macOS fix was actually swiftlang/swift#18486 |
Attachment: Download
Environment
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1).
Additional Detail from JIRA
md5: 3592da22040f9427c02f2ef22b03d3ed
Issue Description:
For many numbers easily representable by Decimal, the initialiser fails.
Example 1:
Example 2:
The following code calculates how many integers get initialised incorrectly in a few different ranges, all within the range of
Int32
:And the output is:
The text was updated successfully, but these errors were encountered: