Displaying Rationals

Oh, yeah, that would be very breaking indeed :slight_smile:

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.

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 too.

1 Like

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

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

This seems to work OK:

[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.

1 Like

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

2 Likes

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

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.

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.

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.

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.

just for future information (since somebody did necrobump :wink:

3 Likes

Very nice! And I’m quite happy this doesn’t use my bad idea of using or :slight_smile: