# Complexifying pi

**URL:** <https://discourse.julialang.org/t/complexifying-pi/23885>\
**Category:** General Usage\
**Created:** [May 5, 2019, 9:37pm UTC](https://discourse.julialang.org/t/complexifying-pi/23885 "2019-05-05T21:37:21Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 6, 2019, 4:50pm UTC](https://discourse.julialang.org/t/complexifying-pi/23885/21 "2019-05-06T16:50:57Z")

</div>

> [@tobydriscoll](#):
>
> (Also, why `complex(1)` , `Complex(1)` , `float(1)` , but not `Float(1)` …never mind, I probably don’t want to know.)

In general you shouldn’t use `Complex`, that’s the type, it’s easier to use the constructor `complex`. `Complex` could be useful only in the case in which you want to force the parameter:

```julia
julia> Complex{Float64}(3, -2)
3.0 - 2.0im

```

In any other case, go with `complex`. `float` converts the number to the most appropriate “floating-point number”, but there is no `Float` type associated. Note: as convention, in Julia type names starts with capital letter, while function names are all lower case.

> [@tobydriscoll](#):
>
> I do still find a language that offers `pi` and `2pi` by default

Well, in Julia there is no Irrational `2pi` 😉 However there is `tau` from [GitHub - JuliaMath/Tau.jl: A Julia module providing the definition of the circle constant Tau (2π)](https://github.com/JuliaMath/Tau.jl) which serves that goal

> [@tobydriscoll](#):
>
> There is probably no use of an `Irrational` type that doesn’t end in either full symbolic math or bailing out to a float type.

Yes, that’s exactly the point. Given their nature, irrational numbers can’t be represented numerically by a finite-memory computer, so anyway you end up with a float number with _any_ operations you do. Julia doesn’t do symbolic algebra. Just keep in mind this. The convenience of the `Irrational` type in Julia is that they’re automatically converted to the most appropriate precision the _first time_ they get in contact with another number:

```julia
julia> pi * 1
3.141592653589793

julia> pi * big(1)
3.141592653589793238462643383279502884197169399375105820974944592307816406286198

```

You can check the second one has the correct expansion of pi (the very last digit may be wrong), instead the first one is precise up to the 53-bit significand of a `Float64`, in fact

```julia
julia> big(pi * 1)
3.141592653589793115997963468544185161590576171875

```

gets soon wrong after the 16-ish significant digit

> [@tobydriscoll](#):
>
> ended up getting burned in the case of the single input `pi`

In that case you should consider to treat irrational numbers in a special way: either do `float(x)` (effectively convert to `Float64`), or `big(x)` in case you want the maximum precision possible or do something else. In case your function has another argument, consider using a promotion rule.

---

<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:** [May 6, 2019, 5:33pm UTC](https://discourse.julialang.org/t/complexifying-pi/23885/22 "2019-05-06T17:33:31Z")

</div>

> [@tobydriscoll](#):
>
> I still don’t see the payoff that it provides, aside from modest convenience in showing off VPA.

Since Julia has many float types (and users can define new ones), providing a value of \pi in all of them makes sense. One could imagine an interface like

```julia
get_π(::Type{T}) where {T <: AbstractFloat} = ...

```

The `Irrational` type provides an analogous solution: in most cases

```julia
op(π, x::T)

```

is equivalent to

```julia
op(get_π(T), x)

```

ie the type is resolved on demand.

Of course the analogy breaks when there is no other type that can be used to resolve the desired type. Nevertheless, it is still a neat interface when you think about it.

---

<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:** [May 6, 2019, 5:54pm UTC](https://discourse.julialang.org/t/complexifying-pi/23885/23 "2019-05-06T17:54:48Z")

</div>

I don’t see why there would ever be a reason to convert `π` to a `Complex`.

The original post mentions "a type that internally stores values in a `Complex` form, but if this type is concrete (e.g. `Complex{Float64}`) then it will already work, and if it is not concrete, then why not use `Number` instead of `Complex`?

---

<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:** [May 6, 2019, 6:03pm UTC](https://discourse.julialang.org/t/complexifying-pi/23885/24 "2019-05-06T18:03:19Z")

</div>

Related to this discussion: I just noticed that `imag(pi)` throws the exact same error as the one in the original post. (This happens in every Julia version that I have installed, including the latest nightly.)

I will file an issue on github, unless there is one already.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 6, 2019, 6:11pm UTC](https://discourse.julialang.org/t/complexifying-pi/23885/25 "2019-05-06T18:11:57Z")

</div>

There is  
[https://github.com/JuliaLang/julia/issues/22878](https://github.com/JuliaLang/julia/issues/22878)  
for `complex(pi)`

_Edit_: I see what you mean, `imag(pi)` should be easy to define.

[Previous page](https://discourse.julialang.org/t/complexifying-pi/23885.md?page=1)
