Skip to content

Commit 185820a

Browse files
Fix sign of fractional units in Duration initializers from Double. (#66582)
* Fix sign of fractional units in Duration initializers from Double. When separating a double duration into integral and fractional parts, we got the sign wrong. This fixes that bug. * Fixup variable name.
1 parent 954d20b commit 185820a

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

stdlib/public/core/Duration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ extension Duration {
122122
// handle them slightly differently to ensure that integer values are
123123
// never rounded if `scale` is representable as Double.
124124
let integralPart = duration.rounded(.towardZero)
125-
let fractionalPart = integralPart - duration
125+
let fractionalPart = duration - integralPart
126126
self.init(_attoseconds:
127127
// This term may trap due to overflow, but it cannot round, so if the
128128
// input `seconds` is an exact integer, we get an exact integer result.

test/stdlib/Duration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ if #available(SwiftStdlib 5.7, *) {
1414
expectEqual(sec, Int64(integerValue))
1515
expectEqual(attosec, 0)
1616
}
17+
let quarterSecond = Duration.seconds(0.25)
18+
expectEqual(quarterSecond.components, (0, 250_000_000_000_000_000))
1719
// Value that overflows conversion from Double -> Int64, but should be
1820
// representable as a number of seconds:
1921
let huge: Double = 1.7e20

0 commit comments

Comments
 (0)