Skip to content

Commit 68e2f4f

Browse files
committed
ensure that product handles zero correctly (Fixes #3)
Ignore-this: 19899f073dfc438d38545e3e617e67b4 darcs-hash:66281faaee61cbbd04cb694587edf10e131621b7
1 parent 5f51503 commit 68e2f4f

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/Data/Number/LogFloat.hs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ newtype LogFloat = LogFloat Double
162162

163163
#if __GLASGOW_HASKELL__ >= 710
164164
-- TODO: this version should also work for NHC and Hugs, I think...
165-
-- HACK: we should be able to just unsafeCoerce the functions themselves, instead of coercing the inputs and the outputs; but, GHC 7.10 seems to get confused about trying to coerce the index types too... To fix this we give explicit signatures, as below, but this requires both ScopedTypeVariables and InstanceSigs; and I'm not sure when InstanceSigs was introduced.
165+
-- HACK: we should be able to just unsafeCoerce the functions
166+
-- themselves, instead of coercing the inputs and the outputs; but,
167+
-- GHC 7.10 seems to get confused about trying to coerce the index
168+
-- types too... To fix this we give explicit signatures, as below,
169+
-- but this requires both ScopedTypeVariables and InstanceSigs; and
170+
-- I'm not sure when InstanceSigs was introduced.
166171

167172
instance IArray UArray LogFloat where
168173
{-# INLINE bounds #-}
@@ -606,12 +611,17 @@ product = kahan 0 0
606611
where
607612
kahan t c _ | t `seq` c `seq` False = undefined
608613
kahan t _ [] = LogFloat t
609-
kahan t c (LogFloat x : xs) =
610-
-- Beware this getting incorrectly optimized away by constant folding!
611-
let y = x - c
612-
t' = t + y
613-
c' = (t' - t) - y
614-
in kahan t' c' xs
614+
kahan t c (LogFloat x : xs)
615+
-- Avoid NaN when there's a negInfty in the list. N.B.,
616+
-- this causes zero to annihilate infinity.
617+
| x == negativeInfinity = LogFloat negativeInfinity
618+
| otherwise =
619+
-- Beware this getting incorrectly optimized away by
620+
-- constant folding!
621+
let y = x - c
622+
t' = t + y
623+
c' = (t' - t) - y
624+
in kahan t' c' xs
615625

616626
-- This version *completely* eliminates rounding errors and loss
617627
-- of significance due to catastrophic cancellation during summation.

0 commit comments

Comments
 (0)