Skip to content

"Seconds" RealFrac instance#68

Merged
CetinSert merged 2 commits intocorsis:masterfrom
Mathnerd314:seconds
Jul 13, 2021
Merged

"Seconds" RealFrac instance#68
CetinSert merged 2 commits intocorsis:masterfrom
Mathnerd314:seconds

Conversation

@Mathnerd314
Copy link
Copy Markdown
Contributor

So my PR #67 makes the Num instance consistent in the following way:

  • fromInteger interprets the integral as nanoseconds
  • the unit element of multiplication (*) is "one nanosecond" (TimeSpec 0 1)
  • TimeSpec is an instance of Integral

IMO this is the simpler and more useful approach. But there is another Num instance defined in the style of Fixed in the following way:

  • fromInteger interprets the integral as seconds
  • the unit element of multiplication (*) is "one second" (TimeSpec 1 0)
  • It is an instance of RealFrac

I wrote this as a wrapper newtype Seconds here in this PR. The behavior is chosen to be similar to Float/Double, so #49 should be solved by using this newtype instead of TimeSpec.

The name "Seconds" is inspired by extra, I don't know if it's really a good name for it.

These instances are based on using the nanosecond as the underlying unit,
i.e. the value of a TimeSpec is the integer s * 1e9 + ns.
Before it was based on representing the rational s + (ns / 1e9).
But the integer valuation satisfies the ring
laws and provides much more intuitive behavior, e.g.
(2 :: TimeSpec)*(2 :: TimeSpec) = (4 :: TimeSpec)
Before, this gave 0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants