From 8d3d3dd02242b7f23c90b07127e557efd0e0757f Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Fri, 20 May 2022 18:22:11 +0200 Subject: [PATCH 1/9] skip show when denominator is one --- base/rational.jl | 7 +++++++ test/rational.jl | 2 ++ 2 files changed, 9 insertions(+) diff --git a/base/rational.jl b/base/rational.jl index 9e887bdaefa91..e2d1a91159db9 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -83,6 +83,13 @@ end function show(io::IO, x::Rational) show(io, numerator(x)) + + if isone(denominator(x)) && ( + ((:compact=>true) in io) || get(io, :typeinfo, Number) <: Rational + ) + return + end + print(io, "//") show(io, denominator(x)) end diff --git a/test/rational.jl b/test/rational.jl index 1618156212af7..903d702b2c092 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -253,6 +253,8 @@ 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{Int64}[-1]" let io1 = IOBuffer() write(io1, rational1) From 53b52188ad40220442b5b10a30b48d61d046b26a Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Fri, 10 Jun 2022 17:23:54 +0200 Subject: [PATCH 2/9] use get instead of in Co-authored-by: Jeff Bezanson --- base/rational.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index e2d1a91159db9..0e81ff802609e 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -85,7 +85,7 @@ function show(io::IO, x::Rational) show(io, numerator(x)) if isone(denominator(x)) && ( - ((:compact=>true) in io) || get(io, :typeinfo, Number) <: Rational + (get(io, :compact, false) || get(io, :typeinfo, Number) <: Rational ) return end From 1e958fac49e5b6cdd0c94bd0008d3dbbf2f4431a Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Wed, 25 Jan 2023 07:38:36 -0800 Subject: [PATCH 3/9] Update base/rational.jl --- base/rational.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index 0e81ff802609e..1102be3b3a150 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -85,7 +85,7 @@ function show(io::IO, x::Rational) show(io, numerator(x)) if isone(denominator(x)) && ( - (get(io, :compact, false) || get(io, :typeinfo, Number) <: Rational + get(io, :compact, false) || get(io, :typeinfo, Number) <: Rational ) return end From 6264b0bb9146feb992868ce63982df49aafdd004 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Sun, 5 Mar 2023 11:57:51 +0100 Subject: [PATCH 4/9] don't use :compact to determine when skipping denominator Co-authored-by: Lilith Orion Hafner --- base/rational.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 1102be3b3a150..027b7f5122c8b 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -84,9 +84,7 @@ end function show(io::IO, x::Rational) show(io, numerator(x)) - if isone(denominator(x)) && ( - get(io, :compact, false) || get(io, :typeinfo, Number) <: Rational - ) + if isone(denominator(x)) && get(io, :typeinfo, Any) <: Rational return end From 024b45405ac94ead7e4c15bf71715fd1a85a5c26 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Sun, 5 Mar 2023 12:06:28 +0100 Subject: [PATCH 5/9] add test: show matrix of Union{Int, Rational{Int}} --- test/rational.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/rational.jl b/test/rational.jl index 903d702b2c092..7425a25b01376 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -255,6 +255,8 @@ end @test sprint(show, rational2) == "-1//2" @test sprint(show, -2//2) == "-1//1" @test sprint(show, [-2//2,]) == "Rational{Int64}[-1]" + @test sprint(show, MIME"text/plain"(), Union{Int, Rational{Int}}[7 3//6; 6//3 2]) == + "2×2 Matrix{Union{Rational{Int64}, Int64}}:\n 7 1//2\n 2//1 2" let io1 = IOBuffer() write(io1, rational1) From 30ad4499fb85a8d17cfe58dfa89158cdd90375b4 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Mon, 6 Mar 2023 22:17:35 +0100 Subject: [PATCH 6/9] add NEWS on printing denominators --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 9c2468e229861..eade36cad6d53 100644 --- a/NEWS.md +++ b/NEWS.md @@ -73,6 +73,8 @@ Library changes Standard library changes ------------------------ +* integral entries in containers of `Rational`s will not print their denominator ([#45396]) + #### Package Manager #### LinearAlgebra From 2ff626dcc0f89f160630328dd1c0b2aa7e3c3a53 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Tue, 7 Mar 2023 10:57:16 +0100 Subject: [PATCH 7/9] add NEWS entry --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index eade36cad6d53..950d570aa641b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -73,7 +73,7 @@ Library changes Standard library changes ------------------------ -* integral entries in containers of `Rational`s will not print their denominator ([#45396]) +* printing integral `Rational`s will try to avoid printing denominators ([#45396]) #### Package Manager From 7bfa59c18552d72066fa59d9c5659f320ef2e4a0 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Wed, 8 Mar 2023 12:10:40 +0100 Subject: [PATCH 8/9] disambiguate NEWS entry --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 5b67af2203816..2fb5e6a687550 100644 --- a/NEWS.md +++ b/NEWS.md @@ -40,7 +40,7 @@ Standard library changes ------------------------ * `startswith` now supports seekable `IO` streams ([#43055]) -* printing integral `Rational`s will try to avoid printing denominators ([#45396]) +* printing integral `Rational`s will skip the denominator in `Rational`-typed IO context (e.g. in `Arrays`) ([#45396]) #### Package Manager From d04a32bae946fcc2063dcbebde0f5ea490611863 Mon Sep 17 00:00:00 2001 From: Marek Kaluba Date: Wed, 8 Mar 2023 16:58:33 +0100 Subject: [PATCH 9/9] fix tests for printing rationals on 32-bit systems Co-authored-by: Lilith Orion Hafner --- test/rational.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rational.jl b/test/rational.jl index a9fe16532980a..a0833d08eb218 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -254,9 +254,9 @@ end @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{Int64}[-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{Int64}, Int64}}:\n 7 1//2\n 2//1 2" + "2×2 Matrix{Union{Rational{$Int}, $Int}}:\n 7 1//2\n 2//1 2" let io1 = IOBuffer() write(io1, rational1)