[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

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

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

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

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: https://github.com/jishnub/MultiplesOfPi.jl.git

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


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ℯ.


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


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.