# Irrational numbers are unique, so which package gets to define them?

**URL:** <https://discourse.julialang.org/t/irrational-numbers-are-unique-so-which-package-gets-to-define-them/44798>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [August 12, 2020, 11:16am UTC](https://discourse.julialang.org/t/irrational-numbers-are-unique-so-which-package-gets-to-define-them/44798 "2020-08-12T11:16:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 12, 2020, 11:16am UTC](https://discourse.julialang.org/t/irrational-numbers-are-unique-so-which-package-gets-to-define-them/44798/1 "2020-08-12T11:16:39Z")

</div>

Irrational numbers are singleton types and are unique, so we have something like

```julia
julia> Irrational{:pi}() === Irrational{:pi}()
true

```

Not many irrational numbers are defined in `Base`, therefore packages are free to define their own numbers. However the macro `Base.@irrational` that may be used to define a new irrational number `a` simultaneously defines `Base.Float64(a)`. This therefore means that a second package that also defines the same number `a` overwrites the original method definition of `Float64(a)` with the new – albeit identical – type. For example:

```julia
julia> module A
       Base.@irrational a 1.0 float(big(1))
       end
Main.A

julia> module B
       Base.@irrational a 1.0 float(big(1))
       end
Main.B

julia> @which Float64(A.a)
Float64(::Irrational{:a}) in Main.B at irrationals.jl:189

```

Now this isn’t really a problem for the most part (aside from the unexpected type piracy), since

```julia
julia> A.a === B.a
true

```

and the results would be identical. However when functions are overwritten like this, package precompilation gets broken. One solution appears to be to import `A.a` inside `B`, but this means that `B` needs to depend on `A` that might otherwise be unrelated. Another solution is to rename the number in `B` and call it something other than `a`. However this is undesirable, eg. you wouldn’t name `ℯ` as something else. So which package gets to define the number without precompilation hurdles?

For example, [StatsFuns.jl](https://github.com/JuliaStats/StatsFuns.jl/blob/master/src/StatsFuns.jl) defines quite a few irrationals. How to avoid conflicts with `StatsFuns` if eg. we want to define `sqrt2`?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 12, 2020, 12:11pm UTC](https://discourse.julialang.org/t/irrational-numbers-are-unique-so-which-package-gets-to-define-them/44798/2 "2020-08-12T12:11:27Z")

</div>

While `Base` uses a symbol for the type parameter, this is merely a convention. You can define your own type (possibly parametrizing it by a symbol, too) and use that as a parameter. Eg

```julia
struct MyIrrational{T} end

Base.Float64(::Irrational{MyIrrational{:sqrt2}}) = √2

```

From that point on, just [avoid type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy-1) and you will be fine.
