thyme icon indicating copy to clipboard operation
thyme copied to clipboard

toSeconds is sometimes inaccurate

Open liyang opened this issue 10 years ago • 1 comments

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 avatar May 02 '14 10:05 liyang

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 avatar May 07 '14 00:05 liyang