Skip to content

Commit

Permalink
Show rationals without //1 when typeinfo <: Rational (#45396)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff Bezanson <[email protected]>
Co-authored-by: Elliot Saba <[email protected]>
Co-authored-by: Lilith Orion Hafner <[email protected]>
  • Loading branch information
4 people committed Mar 9, 2023
1 parent 2ed1cfe commit 5b2240c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Standard library changes
------------------------

* `startswith` now supports seekable `IO` streams ([#43055])
* printing integral `Rational`s will skip the denominator in `Rational`-typed IO context (e.g. in `Arrays`) ([#45396])

#### Package Manager

Expand Down
5 changes: 5 additions & 0 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ end

function show(io::IO, x::Rational)
show(io, numerator(x))

if isone(denominator(x)) && get(io, :typeinfo, Any) <: Rational
return
end

print(io, "//")
show(io, denominator(x))
end
Expand Down
4 changes: 4 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ end
rational2 = Rational(-4500, 9000)
@test sprint(show, rational1) == "1465//8593"
@test sprint(show, rational2) == "-1//2"
@test sprint(show, -2//2) == "-1//1"
@test sprint(show, [-2//2,]) == "Rational{$Int}[-1]"
@test sprint(show, MIME"text/plain"(), Union{Int, Rational{Int}}[7 3//6; 6//3 2]) ==
"2×2 Matrix{Union{Rational{$Int}, $Int}}:\n 7 1//2\n 2//1 2"
let
io1 = IOBuffer()
write(io1, rational1)
Expand Down

2 comments on commit 5b2240c

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected.
A full report can be found here.

Please sign in to comment.