I am working in Haskell and have had an error where, for really large floats z, sin(z) returns some value outside of the range [-1, 1].
I’m learning Haskell for the first time, so I have had very little luck debugging and the program just crashes when sin(z) does return a value outside of the above range as sin(z) is an input into another function that only accepts values inside the range [-1, 1].
Additionally, I don’t have access to the other function, I only can send in a value, but it keeps crashing when sin(z) returns a number either greater than 1 or less than -1.
Is there any way to figure out why sin(z) is doing this?
>Solution :
It seems like this is a floating point error; see this similar post. So for very large values, the sin function is returning a value slightly above 1, due to rounding errors.
To solve your problem, I would cap the return value at 1. Specifically, return the min 1 (sin z) instead of just the sin z directly.
Edit: replaced max with min.