Skip to content

Fix sign of fractional units in Duration initializers from Double. #66582

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/Duration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension Duration {
// handle them slightly differently to ensure that integer values are
// never rounded if `scale` is representable as Double.
let integralPart = duration.rounded(.towardZero)
let fractionalPart = integralPart - duration
let fractionalPart = duration - integralPart
self.init(_attoseconds:
// This term may trap due to overflow, but it cannot round, so if the
// input `seconds` is an exact integer, we get an exact integer result.
Expand Down
2 changes: 2 additions & 0 deletions test/stdlib/Duration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ if #available(SwiftStdlib 5.7, *) {
expectEqual(sec, Int64(integerValue))
expectEqual(attosec, 0)
}
let quarterSecond = Duration.seconds(0.25)
expectEqual(quarterSecond.components, (0, 250_000_000_000_000_000))
// Value that overflows conversion from Double -> Int64, but should be
// representable as a number of seconds:
let huge: Double = 1.7e20
Expand Down