Skip to content

Commit fb6fb95

Browse files
committed
Merge pull request #2801 from natecook1000/nc-nextup
[stdlib] Fix error in nextUp and nextDown
2 parents 1b458c8 + 73811d6 commit fb6fb95

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,25 @@ public func expectOptionalEqual${Generic}(
238238

239239
%end
240240

241-
public func expectLT(_ lhs: Int, _ rhs: Int, ${TRACE}) {
241+
public func expectLT<T : Comparable>(_ lhs: T, _ rhs: T, ${TRACE}) {
242242
if !(lhs < rhs) {
243243
expectationFailure("\(lhs) < \(rhs)", trace: ${trace})
244244
}
245245
}
246246

247-
public func expectLE(_ lhs: Int, _ rhs: Int, ${TRACE}) {
247+
public func expectLE<T : Comparable>(_ lhs: T, _ rhs: T, ${TRACE}) {
248248
if !(lhs <= rhs) {
249249
expectationFailure("\(lhs) <= \(rhs)", trace: ${trace})
250250
}
251251
}
252252

253-
public func expectGT(_ lhs: Int, _ rhs: Int, ${TRACE}) {
253+
public func expectGT<T : Comparable>(_ lhs: T, _ rhs: T, ${TRACE}) {
254254
if !(lhs > rhs) {
255255
expectationFailure("\(lhs) > \(rhs)", trace: ${trace})
256256
}
257257
}
258258

259-
public func expectGE(_ lhs: Int, _ rhs: Int, ${TRACE}) {
259+
public func expectGE<T : Comparable>(_ lhs: T, _ rhs: T, ${TRACE}) {
260260
if !(lhs >= rhs) {
261261
expectationFailure("\(lhs) >= \(rhs)", trace: ${trace})
262262
}

stdlib/public/core/FloatingPointTypes.swift.gyb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ extension ${Self}: BinaryFloatingPoint {
474474
exponentBitPattern: exponentBitPattern - 1,
475475
significandBitPattern: ${Self}._significandMask)
476476
}
477+
return ${Self}(sign: .minus,
478+
exponentBitPattern: exponentBitPattern,
479+
significandBitPattern: significandBitPattern - 1)
477480
}
478481
if isInfinite { return self }
479482
if significandBitPattern == ${Self}._significandMask {

test/1_stdlib/FloatingPoint.swift.gyb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ FloatingPoint.test("Double/HashValueZero") {
171171
expectEqual(zero.hashValue, negativeZero.hashValue)
172172
}
173173

174+
let floatNextUpDownTests: [(Float, Float)] = [
175+
(.nan, .nan),
176+
(.greatestFiniteMagnitude, .infinity),
177+
(-.infinity, -.greatestFiniteMagnitude),
178+
(0x1.fffffep-1, 1.0), (1.0, 0x1.000002p+0),
179+
(-0x1p-149, -0.0), (0.0, 0x1p-149),
180+
(0x1.effffep-1, 0.96875), (0.96875, 0x1.f00002p-1),
181+
]
182+
183+
FloatingPoint.test("Float.nextUp, .nextDown")
184+
.forEach(in: floatNextUpDownTests) {
185+
(prev, succ) in
186+
expectEqual(succ.bitPattern, prev.nextUp.bitPattern)
187+
expectEqual(prev.bitPattern, succ.nextDown.bitPattern)
188+
expectEqual((-succ).bitPattern, (-prev).nextDown.bitPattern)
189+
expectEqual((-prev).bitPattern, (-succ).nextUp.bitPattern)
190+
}
191+
192+
let doubleNextUpDownTests: [(Double, Double)] = [
193+
(.nan, .nan),
194+
(.greatestFiniteMagnitude, .infinity),
195+
(-.infinity, -.greatestFiniteMagnitude),
196+
(0x1.fffffffffffffp-1, 1.0), (1.0, 0x1.0000000000001p+0),
197+
(-0x1p-1074, -0.0), (0.0, 0x1p-1074),
198+
(0x1.effffffffffffp-1, 0.96875), (0.96875, 0x1.f000000000001p-1),
199+
]
200+
201+
FloatingPoint.test("Double.nextUp, .nextDown")
202+
.forEach(in: doubleNextUpDownTests) {
203+
(prev, succ) in
204+
expectEqual(succ.bitPattern, prev.nextUp.bitPattern)
205+
expectEqual(prev.bitPattern, succ.nextDown.bitPattern)
206+
expectEqual((-succ).bitPattern, (-prev).nextDown.bitPattern)
207+
expectEqual((-prev).bitPattern, (-succ).nextUp.bitPattern)
208+
}
209+
174210
#if arch(i386) || arch(x86_64)
175211

176212
FloatingPoint.test("Float80/IntegerLiteralConvertible") {

0 commit comments

Comments
 (0)