# Typeof(π+1) is not Irrational

**URL:** <https://discourse.julialang.org/t/typeof-1-is-not-irrational/81186>\
**Category:** New to Julia\
**Tags:** numbers\
**Created:** [May 17, 2022, 9:49am UTC](https://discourse.julialang.org/t/typeof-1-is-not-irrational/81186 "2022-05-17T09:49:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fa-bien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fa-bien/32/5559_2.png) [@fa-bien](https://discourse.julialang.org/u/fa-bien)\
**Post date:** [May 17, 2022, 9:49am UTC](https://discourse.julialang.org/t/typeof-1-is-not-irrational/81186/1 "2022-05-17T09:49:02Z")

</div>

I have not used irrational numbers in Julia so far and was surprised to discover that while π is indeed Irrational, π+1 is Float64. I would like to understand why.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [May 17, 2022, 10:04am UTC](https://discourse.julialang.org/t/typeof-1-is-not-irrational/81186/2 "2022-05-17T10:04:29Z")

</div>

See [Numbers · The Julia Language](https://docs.julialang.org/en/v1/base/numbers/#Base.AbstractIrrational)

In particular:

> Number type representing an exact irrational value, which is automatically rounded to the correct precision in arithmetic operations with other numeric quantities.

The reason for this behavior is that you can’t really just add something to an irrational and keep the same genericness that the original type had (you could have a tape recording all operations, but that would slow down arithmetic tremendously). Internally, `\pi` etc. are defined in [`base/mathconstants.jl`](https://github.com/JuliaLang/julia/blob/master/base/mathconstants.jl), with fairly high precision.

You may also be interested in this article from NASA, about how many digits of pi are actually required for (just about) all computations you’d usually be interested in:

> **[How Many Decimals of Pi Do We Really Need? - Edu News | NASA/JPL Edu](https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimals-of-pi-do-we-really-need/)**
>
> While world record holders may have memorized more than 70,000 digits of pi, a JPL engineer explains why you really only need a tiny fraction of that for most calculations – even at NASA.

---

<div class="post-metadata">

**Author:** ![fa-bien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fa-bien/32/5559_2.png) [@fa-bien](https://discourse.julialang.org/u/fa-bien)\
**Post date:** [May 17, 2022, 11:04am UTC](https://discourse.julialang.org/t/typeof-1-is-not-irrational/81186/3 "2022-05-17T11:04:49Z")

</div>

Thanks for the explanation!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 17, 2022, 11:44am UTC](https://discourse.julialang.org/t/typeof-1-is-not-irrational/81186/4 "2022-05-17T11:44:04Z")

</div>

> [@Sukera](#):
>
> Internally, `\pi` etc. are defined in [`base/mathconstants.jl`](https://github.com/JuliaLang/julia/blob/master/base/mathconstants.jl), with fairly high precision.

To be clear, they are defined to _arbitrarily high_ precision. When you do arithmetic to combine `π` with some other quantity, it is replaced by a finite-precision approximation (typically determined at compile time) depending on the types of the other operands. e.g. `π + 1` and `0.5π` both evaluate `π` to double precision, but `0.5f0 * π` evaluates `π/2` to single precision.

The point of a special type for a constant like `π`, rather than simply defining it as a double-precision constant as in e.g. [numpy](https://numpy.org/doc/stable/reference/constants.html#numpy.pi) or [Matlab](https://www.mathworks.com/help/matlab/ref/pi.html?searchHighlight=pi&s_tid=srchtitle_pi_1), is to allow you to write type-generic code (if you are careful). That is, you can write an expression like `exp(-(π*x))` and it will work and use the full precision of `x` if `x` is single-precision, double-precision, `BigFloat` arbitrary precision, quad precision etcetera. (This requires careful attention to the parentheses, because the precision of `π` is determined by the _first_ arithmetic operation you do on it.)

The same thing applies to rational constants. If you have a constant like `2/3` in your code, then it is evaluated in double precision (because that’s what `/` does with `Int` arguments), which would be bad if you want it to use a context-dependent precision. Instead, you can write the rational constant `2//3` and it will then be converted to the precision of whatever type you combine it with. (This conversion is done at compile-time in Julia 1.8, so [rational constants are finally fast](https://github.com/JuliaLang/julia/issues/32024).)
