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

toSeconds is sometimes inaccurate #20

Open
liyang opened this issue May 2, 2014 · 1 comment
Open

toSeconds is sometimes inaccurate #20

liyang opened this issue May 2, 2014 · 1 comment
Assignees

Comments

@liyang
Copy link
Member

liyang commented May 2, 2014

Currently implemented as:

toSeconds :: (TimeDiff t, Fractional n) => t -> n
toSeconds = (* recip 1000000) . fromIntegral . view microseconds

rather than

toSeconds = (/ 1000000) . fromIntegral . view microseconds

such that

> toSeconds (microseconds # 5 :: DiffTime) :: Double 
4.9999999999999996e-6

even though a more accurate Double representation exists:

> 5 / 1000000 :: Double
5.0e-6

This can result in an error larger than 0.5us when calling toSeconds with durations of greater than microseconds # (2^32 * 1000000) (assuming 64-bit Double with 52 bits of mantissa), which is a little over 136 years. (Not entirely unreasonable; the first discrepancy is at 11us later.)

Won't fix for now as FDIV is significantly slower than FMUL: check the instruction tables PDF. Comment below if this causes any problems.

@liyang liyang added bug labels May 2, 2014
@liyang
Copy link
Member Author

liyang commented May 7, 2014

A quick benchmark gives:

benchmarking toSeconds/thyme
mean: 2.338436 us, lb 2.327430 us, ub 2.347876 us, ci 0.950
std dev: 52.19640 ns, lb 45.55714 ns, ub 58.20550 ns, ci 0.950
variance introduced by outliers: 15.197%
variance is moderately inflated by outliers

benchmarking toSeconds/mul
mean: 2.273287 us, lb 2.267481 us, ub 2.280542 us, ci 0.950
std dev: 33.16264 ns, lb 28.24934 ns, ub 41.85263 ns, ci 0.950
found 3 outliers among 100 samples (3.0%)
  3 (3.0%) high mild
variance introduced by outliers: 7.530%
variance is slightly inflated by outliers

benchmarking toSeconds/div
mean: 5.557317 us, lb 5.528379 us, ub 5.586427 us, ci 0.950
std dev: 148.7692 ns, lb 133.1797 ns, ub 174.2370 ns, ci 0.950
variance introduced by outliers: 20.945%
variance is moderately inflated by outliers

@liyang liyang self-assigned this Jul 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant