Skip to content
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

"Seconds" RealFrac instance #68

Merged
merged 2 commits into from
Jul 13, 2021
Merged

"Seconds" RealFrac instance #68

merged 2 commits into from
Jul 13, 2021

Conversation

Mathnerd314
Copy link
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