Closed vwwv closed 6 years ago
What is the result you expect?
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
.
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)
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.
This cause the program to get stuck: