Skip to content

Commit 6216ccd

Browse files
authored
[stdlib] Fix a Float16-to-integer conversion bug (#33893)
* [stdlib] Fix a Float16-to-integer conversion bug * [stdlib] Add a test for 'Int(exactly: Float16.infinity)'
1 parent c80477b commit 6216ccd

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,9 +1195,11 @@ public struct ${Self}
11951195
// undefined if it overflows.
11961196
% if not (FloatBits == 16 and bits >= 32): # Float16 is always in-range for 32- and 64-bit ints.
11971197
guard source > ${str(lower)}.0 && source < ${str(upper)}.0 else {
1198+
% else:
1199+
guard source.isFinite else {
1200+
% end
11981201
return nil
11991202
}
1200-
% end
12011203
guard source == source.rounded(.towardZero) else {
12021204
return nil
12031205
}

test/stdlib/FloatingPoint.swift.gyb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,16 @@ FloatingPoint.test("Float/staticProperties") {
260260
}
261261

262262
// Tests the float and int conversions work correctly. Each case is special.
263+
264+
#if !os(macOS) && !(os(iOS) && targetEnvironment(macCatalyst))
265+
if #available(iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
266+
FloatingPoint.test("Float16/Int") {
267+
expectNil(Int(exactly: Float16.infinity))
268+
expectNil(Int(exactly: -Float16.infinity))
269+
}
270+
}
271+
#endif
272+
263273
FloatingPoint.test("Float/UInt8") {
264274
expectEqual(UInt8.min, UInt8(Float(UInt8.min)))
265275
expectEqual(UInt8.max, UInt8(Float(UInt8.max)))

0 commit comments

Comments
 (0)