corsis / clock

High-resolution clock functions: monotonic, realtime, cputime.
Other
58 stars 25 forks source link

Fix `RealFrac Seconds` instance #89

Closed amesgen closed 1 year ago

amesgen commented 1 year ago

Closes #87, see https://github.com/corsis/clock/issues/87#issuecomment-1576119653 for an analysis.

Consider the law of properFraction:

The function properFraction takes a real fractional number x and returns a pair (n,f) such that x = n+f, and:

  • n is an integral number with the same sign as x; and
  • f is a fraction with the same type and sign as x, and with absolute value less than 1.

Before this change, we had

 Λ properFraction (-1 :: Seconds)
(0,Seconds {toTimeSpec = TimeSpec {sec = -1, nsec = 0}})

which violates the second property above, as f = Seconds {toTimeSpec = TimeSpec {sec = -1, nsec = 0}} has absolute value 1, which is not less than 1.

With this change, we correctly get:

 Λ properFraction (-1 :: Seconds)
(-1,Seconds {toTimeSpec = TimeSpec {sec = 0, nsec = 0}})