basvandijk / scientific

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

stuck on `/` #61

Closed vwwv closed 6 years ago

vwwv commented 6 years ago

This cause the program to get stuck:

(3.4 :: Scientific) / 2.1
basvandijk commented 6 years ago

What is the result you expect?

vwwv commented 6 years ago

I was expecting a result, but it was getting stuck; took me a while to realize there was the problem. For example:

main = print $ (3.4 :: Scientific) / 2.1

does not print anything.

Using stack resolver lts-8.11.

basvandijk commented 6 years ago

Note that 3.4 / 2.1 is the repeating decimal 1.(619047). Since a Scientific stores all the digits of a number evaluating (3.4 :: Scientific) / 2.1 will quickly fill up all your memory and eventually crash because of that.

You could use fromRationalRepetend:

λ fromRationalRepetend Nothing (3.4 / 2.1)
Right (1.619047,Just 0)
vwwv commented 6 years ago

Thanks for the info and thanks for the hint. I understand (3.4 :: Scientific) / 2.1 could be a problem the way Scientific works, and that maybe that operation doesn't even make sense to be made...but this could lead to silent bug to unaware users like me, who were using it through third-party libraries and not directly; As an example, when you divide 2 prices (:: Price) using the coinbase library, it might, sometimes, suddenly get blocked because, internally, it was using scientific.

If / is not defined for some values, maybe Scientific shouldn't be an instance of Fractional? Or at least, instead of getting blocked, it could be better to throw an exception.