# Using print on pi produces π rather than 3.141592653589793

**URL:** <https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 15, 2023, 6:25pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682 "2023-12-15T18:25:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevinMoerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevinmoerman/32/44947_2.png) [@KevinMoerman](https://discourse.julialang.org/u/KevinMoerman)\
**Post date:** [December 15, 2023, 6:25pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/1 "2023-12-15T18:25:41Z")

</div>

Is it normal that Julia’s `print` prints constants as their character or name rather than an interpretation of the numerical value?

For instance

```julia
julia> print(pi)
π

```

Multiply it by something though, e.g. 1, forces the interpretation as a number:

```julia
julia> print(1.0*pi)
3.141592653589793

```

But… not all things change it:

```julia
julia> print(abs(pi))
π

```

Here is another:

```julia
julia> print(Base.MathConstants.catalan)
catalan

```

Rather than:

```julia
julia> print(1.0*Base.MathConstants.catalan)
0.915965594177219

```

Similarly:

```julia
julia> print(nothing)
nothing

```

Which I would expect would print… nothing rather than the word “nothing”.  
The `string` function, and perhaps others, seems to behave similarly e.g.:

```julia
julia> string(pi)
"π"

```

And obviously using `spintf` or similar functions, we can avoid these issues.

```julia
julia> using Printf
julia> @sprintf("%.15f",pi)
"3.141592653589793"

```

This is an issue for me when I want to `print` things in a function which has optional inputs, the optional input is `nothing` when not provided,… leading to Julia now printing the word “nothing”. The context is [this GitHub issue](https://github.com/JuliaComputing/XML.jl/issues/22) for those who want to learn more about that I am trying to do. I have a workaround, it just seems annoying that a thing I tend to use as a number, e.g. `pi` doesn’t get converted to a number here.

Thanks!

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [December 15, 2023, 6:43pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/2 "2023-12-15T18:43:14Z")

</div>

The constant pi is indeed a number, but it has the type irrational if I am not mistaken. And an irrational number can not be losslessly converted to a string as required for print. Except for, or course, convertion to the name of the constant, which is the behaviour you are seeing. The reason multiplication does what you want is that it promotes the irrational factor to a floating point product.

So you want to promote two different types: nothing, and number (which is an abstract type). Would it make sense to convert any input to a common type before printing?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 15, 2023, 7:00pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/3 "2023-12-15T19:00:08Z")

</div>

Notice that π is of type `Irrational`.

```julia
julia> π
π = 3.1415926535897...

julia> typeof(π)
Irrational{:π}

```

If you want a specific representation, you can ask for it.

```julia
julia> Float16(π)
Float16(3.14)

julia> Float32(π)
3.1415927f0

julia> Float64(π)
3.141592653589793

julia> rationalize(Float64(π))
165707065//52746197

```

I need to think about `nothing` for a little bit longer.

---

<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:** [December 15, 2023, 7:54pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/4 "2023-12-15T19:54:49Z")

</div>

XML.jl does a form of serialization by expanding their XML type to an XML string representing the document, and probably should specialize that serialization of `nothing` to the empty string and `Irrational` to a numeric representation of the number, instead of relying on `print` directly.

As for why `print` has that behavior - it’s for human readable output/canonical representation of a value, not a machine readable specific format.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 15, 2023, 8:07pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/5 "2023-12-15T20:07:27Z")

</div>

> [@mkitti](#):
>
> I need to think about `nothing` for a little bit longer.

Printing `nothing` has indeed taken a good amount of thought: see [Interpolation of `nothing` should be an error (#27352)](https://github.com/JuliaLang/julia/issues/27352) and [Allow nothing to be printed (#32148)](https://github.com/JuliaLang/julia/pull/32148) for two breadcrumbs if you’re interested.

---

<div class="post-metadata">

**Author:** ![tverho](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tverho](https://discourse.julialang.org/u/tverho)\
**Post date:** [December 15, 2023, 9:30pm UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/6 "2023-12-15T21:30:00Z")

</div>

> [@mkitti](#):
>
> I need to think about `nothing` for a little bit longer.

Thanks, this will be my new motto!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 16, 2023, 12:48am UTC](https://discourse.julialang.org/t/using-print-on-pi-produces-rather-than-3-141592653589793/107682/7 "2023-12-16T00:48:48Z")

</div>

What gives me pause at the moment is that this behavior is not consistent with the REPL display. That is the REPL seems ro have a [special case for `nothing`](https://github.com/JuliaLang/julia/blob/e207c106ba7eaffcb521140baccc7795664a4d53/stdlib/REPL/src/REPL.jl#L327) and does not display it.

```julia-repl
julia> nothing

julia> display(nothing)
nothing

julia> show(nothing)
nothing

```
