Search code examples
haskellprecision

Evaluating trigonometric functions in Haskell


Is there a way in Haskell to specify the precision (say r bits) for the numerical calculation of trigonometric functions?


Solution

  • As far as Haskell goes, all trigonometric functions in specified in the language (and that are in base --- sin, cos, etc.) are overloaded/polymorphic using typeclasses, so every type gets to implement a one-time-implemented sin, cos, etc.

    So Float gets to implement sin, cos, and Double gets to implement it, etc.

    So customizing the precision of sin might be just a matter of finding a type somewhere that fits the precision you need. Every type gets to implement its own sin, so they're free to give whatever precision they want.

    You can also just write your own versions of sin and cos that "round" or truncate precision in the way you want, and name it sin' or cos' or something like that too :)

    sin' x = (/100) . realToFrac . round . (*100) . sin
    

    maybe, for instance