basvandijk / scientific

Arbitrary-precision floating-point numbers represented using scientific notation
BSD 3-Clause "New" or "Revised" License
73 stars 40 forks source link

Make toIntegral safe again #66

Closed basvandijk closed 6 years ago

basvandijk commented 6 years ago

round and the other RealFrac methods use:

toIntegral :: (Num a) => Scientific -> a
toIntegral (Scientific c e) = fromInteger c * fromInteger (magnitude e)

This has the unsafe fromInteger (magnitude e) operation which can fill up all space and crash your program.

This didn't use to be the case. In the past magnitude :: (Num a) => Int -> a so the resulting value was represented in the target type (for example Int) which could be bounded in size which means no space blowup.

I changed this in https://github.com/basvandijk/scientific/commit/7d1859bee6c2a2fb24fa636d42a580de67b13095#diff-f161ee633ba321f16b84ed96bcc15e4eL596 which I now realize was a mistake.

Lets fix this.

mrkkrp commented 5 years ago

So, maybe it makes sense to expose the toIntegral function?