# Irrational optimized away?

**URL:** <https://discourse.julialang.org/t/irrational-optimized-away/25577>\
**Category:** General Usage\
**Created:** [June 23, 2019, 4:43pm UTC](https://discourse.julialang.org/t/irrational-optimized-away/25577 "2019-06-23T16:43:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [June 23, 2019, 4:43pm UTC](https://discourse.julialang.org/t/irrational-optimized-away/25577/1 "2019-06-23T16:43:25Z")

</div>

I found this a little interesting. It seems that the add operation has been optimized away for irrational. How does it do that?

```julia
julia> koo(v) = v + 10
koo (generic function with 1 method)

julia> @code_llvm koo(1)

; @ REPL[1]:1 within `koo'
define i64 @julia_koo_12177(i64) {
top:
; ┌ @ int.jl:53 within `+'
   %1 = add i64 %0, 10
; └
  ret i64 %1
}

julia> @code_llvm koo(π)

; @ REPL[1]:1 within `koo'
define double @julia_koo_12184() {
top:
  ret double 0x402A487ED5110B46
}

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 23, 2019, 4:45pm UTC](https://discourse.julialang.org/t/irrational-optimized-away/25577/2 "2019-06-23T16:45:06Z")

</div>

Each Irrational has its own type so the function is specialized on each one so yes, this is correct.

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [June 23, 2019, 4:49pm UTC](https://discourse.julialang.org/t/irrational-optimized-away/25577/3 "2019-06-23T16:49:42Z")

</div>

Does the magic sit inside the compiler? If I create my own type then I can’t do the same, can I?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 23, 2019, 4:55pm UTC](https://discourse.julialang.org/t/irrational-optimized-away/25577/4 "2019-06-23T16:55:57Z")

</div>

There’s nothing special about the type in the compiler, it’s all just a consequence of its definition. Irrational has a type parameter which is the symbol of the constant, and no fields. There is a different Irrational type for each symbol, and a unique singleton instance of that type. You can define the same thing yourself.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 23, 2019, 7:38pm UTC](https://discourse.julialang.org/t/irrational-optimized-away/25577/5 "2019-06-23T19:38:09Z")

</div>

The `StaticNumbers` package lets you do this for any number.

```julia
julia> using StaticNumbers

julia> @code_llvm koo(static(32))

; @ REPL[11]:1 within `koo'
define i64 @julia_koo_12291() {
top:
  ret i64 42
}

```

However, In most situations Julia’s built-in constant-folding is enough.
