[ANN] MultiplesOfPi.jl

Hi everyone!

This is a small package to solve a specific problem: to avoid floating-point errors while using multiples of pi in arithmetic or trigonometric functions. The package exports the constant Pi that is numerically equivalent to pi, except it delays the conversion to float. Using Pi, or a multiple of it, as an argument to trigonometric functions such as sin and cos would lead to the functions sinpi and cospi being used under the hood, so the result might end up being exact.

This can help, for example, in obtaining exact results such as

julia> (1//3)pi + (4//3)pi == (5//3)pi
false

julia> (1//3)Pi + (4//3)Pi == (5//3)Pi
true

# Euler's identity
julia> exp(im*pi) + 1 == 0
false

julia> exp(im*Pi) + 1 == 0
true

The concept is not unknown to the community, in fact it was discussed in 2013 in the PR that introduced sinpi and cospi, but I didn’t know of an implementation so I made my own.

Github: GitHub - jishnub/MultiplesOfPi.jl: Numbers that produce accurate results when used as arguments to trigonometric functions

Please let me know what you think, or if you know of any package that does this already.

13 Likes

Would it make sense to have a slightly more general package that also involved ℯ? (I guess more transendentals could be added too though I’d imagine there’s only a few for which this would be quite useful)

Edit: come to think of it, maybe the package is great just as it is: a nicely wrapped up functionality, clean and simple.

1 Like

It could be called MultiplesOfPiâ„Ż.

9 Likes

Not unrelated, UnitfulAngles.jl.

Thanks, this is indeed related, and I’ll add a link to this in the Readme.

1 Like

At that point just make a ScaledIrrational type that can do any constant

4 Likes

I do like the idea of pi being just a “unit” same goes for euler’s constant. Often in, hairy equations it cancels as a quantity anyways. But, many people also want that irrational approximate numeric result. Hm.