# Displaying Rationals

**URL:** <https://discourse.julialang.org/t/displaying-rationals/9475>\
**Category:** General Usage\
**Tags:** numbers, io\
**Created:** [March 3, 2018, 1:50pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475 "2018-03-03T13:50:27Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 23, 2020, 9:21pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/21 "2020-03-23T21:21:11Z")

</div>

> [@chakravala](#):
>
> In my post I am talking about also changing the behavior of `/` function to make rationals

Oh, yeah, that would be very breaking indeed 🙂

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [March 23, 2020, 9:21pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/22 "2020-03-23T21:21:33Z")

</div>

In theory, avoiding //1 would be nice, but the problem with that is you no longer have a homogeneous type when parsing those numbers, since some will appear to be integers and some as rational when parsing.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 23, 2020, 9:25pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/23 "2020-03-23T21:25:52Z")

</div>

> [@kristoffer.carlsson](#):
>
> Just to be clear, tweaking the printed output in the REPL for some Base type is not considered a breaking change (and tends to happen in every minor release)…

Is it the consensus of Julia core dev? Can it be documented it in the 3-arg `show` docstring? (I was suspecting it is the case but I wasn’t sure if it was within “minor change.”)

Once 3-arg `show` spec is properly defined it’d be nice to warn it in [Doctests · Documenter.jl](https://juliadocs.github.io/Documenter.jl/dev/man/doctests/) too.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 23, 2020, 9:26pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/24 "2020-03-23T21:26:48Z")

</div>

> [@tkf](#):
>
> Is it the consensus of Julia core dev?

I don’t know. It is de facto the case at least.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 23, 2020, 9:33pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/25 "2020-03-23T21:33:22Z")

</div>

I see. I guess I’ll try a PR/RFC at some point.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [March 23, 2020, 9:48pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/26 "2020-03-23T21:48:40Z")

</div>

> [@chakravala](#):
>
> avoiding //1 would be nice, but the problem with that is you no longer have a homogeneous type when parsing those numbers,

This seems to work OK:

```julia
[1, 1//2, 2] isa Vector{Rational{Int}}

```

Whether that alone is too weird or not I don’t know. We don’t print `[0, 0.5, 1]` of course.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 24, 2020, 1:30am UTC](https://discourse.julialang.org/t/displaying-rationals/9475/27 "2020-03-24T01:30:34Z")

</div>

> [@improbable22](#):
>
> This seems to work OK:
> 
> ```julia
> [1, 1//2, 2] isa Vector{Rational{Int}}
> 
> ```

That’s just the usual conversion to make `Vector`s have a common element type.

---

<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:** [March 24, 2020, 6:44am UTC](https://discourse.julialang.org/t/displaying-rationals/9475/28 "2020-03-24T06:44:07Z")

</div>

> [@chakravala](#):
>
> it will mess things up, since parsing a rational number with a single slash will become a float when evaluated

FWIW, `show(::IO, ::MIME"text/plain", x)` and friends are not required to output something that you can parse anyway (eg `5×5 Array{Rational{Int64},2}:` etc). So, basically, anything goes.

Personally I don’t work with rationals that much to mind `//`. But as for

> [@sijo](#):
>
> Isn’t 1⧸2 is quite distinct from 1/2?

I don’t think they are visually distinct enough. Side by side one can say which is which, but in isolation I doubt many people would identify them.

Generally, instead of tweaking plain text displays, I think that users who want these things to look nice should focus on other formats that can handle this better, eg HTML output.

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [March 24, 2020, 9:42am UTC](https://discourse.julialang.org/t/displaying-rationals/9475/29 "2020-03-24T09:42:15Z")

</div>

> Generally, instead of tweaking plain text displays, I think that users who want these things to look nice should focus on other formats that can handle this better, eg HTML output.

I do not agree. There are good reasons to use the REPL instead of fancier environments (jupyter, etc…) and having the nicest possible display at the REPL is a worthwile goal.

I need Rationals a lot because I do group theory/arithmetic, thus I do care.

---

<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:** [March 24, 2020, 9:56am UTC](https://discourse.julialang.org/t/displaying-rationals/9475/30 "2020-03-24T09:56:20Z")

</div>

> [@Jean\_Michel](#):
>
> having the nicest possible display at the REPL is a worthwile goal

Possibly. But, as this discussion demonstrates, also a highly subjective one, with very heterogeneous objectives across Julia users (valid expressions in cells, compact representation, using Unicode, etc).

Maybe it would make sense to just work on a customizable framework that hooks into the `display` mechanism (falling back to `show` for `text/plain`, in a way that users can extend) and package that.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [March 19, 2023, 9:49pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/31 "2023-03-19T21:49:41Z")

</div>

Relevant here: Unicode has U+2044, which allows for displaying inline fractions (like ¼). It could be used to make rationals in general look a lot nicer.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [March 19, 2023, 11:43pm UTC](https://discourse.julialang.org/t/displaying-rationals/9475/32 "2023-03-19T23:43:45Z")

</div>

just for future information (since somebody did necrobump 😉

> <https://github.com/JuliaLang/julia/pull/45396>
>
> following https://github.com/JuliaLang/julia/issues/42626 with this pull we have…:
> 
> \`\`\`julia
> julia\> -2//2
> \-1//1
> 
> julia\> \[-2//2,\]
> 1-element Vector{Rational{Int64}}:
> -1
> 
> julia\> rand(-6:6, 4,4).//2
> 4×4 Matrix{Rational{Int64}}:
> -3 1 3//2 1//2
> -3//2 3 2 1
> 2 0 -2 1//2
> 0 -3//2 -1 -2
> \`\`\`

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 20, 2023, 9:44am UTC](https://discourse.julialang.org/t/displaying-rationals/9475/33 "2023-03-20T09:44:10Z")

</div>

Very nice! And I’m quite happy this doesn’t use my bad idea of using `∕` or `⧸` 🙂

[Previous page](https://discourse.julialang.org/t/displaying-rationals/9475.md?page=1)
