Skip to content

Commit ee41310

Browse files
LiozouKristofferC
authored andcommitted
Promote on Rational binary operations (#36279)
(cherry picked from commit 6c760d2)
1 parent 8dec8f1 commit ee41310

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

base/rational.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@ julia> (3 // 5) // (2 // 1)
6161
//(n::Integer, d::Integer) = Rational(n,d)
6262

6363
function //(x::Rational, y::Integer)
64-
xn, yn = divgcd(x.num,y)
64+
xn, yn = divgcd(promote(x.num, y)...)
6565
checked_den(xn, checked_mul(x.den, yn))
6666
end
6767
function //(x::Integer, y::Rational)
68-
xn, yn = divgcd(x,y.num)
68+
xn, yn = divgcd(promote(x, y.num)...)
6969
checked_den(checked_mul(xn, y.den), yn)
7070
end
7171
function //(x::Rational, y::Rational)
72-
xn,yn = divgcd(x.num,y.num)
73-
xd,yd = divgcd(x.den,y.den)
72+
xn,yn = divgcd(promote(x.num, y.num)...)
73+
xd,yd = divgcd(promote(x.den, y.den)...)
7474
checked_den(checked_mul(xn, yd), checked_mul(xd, yn))
7575
end
7676

@@ -280,7 +280,7 @@ end
280280
for (op,chop) in ((:+,:checked_add), (:-,:checked_sub), (:rem,:rem), (:mod,:mod))
281281
@eval begin
282282
function ($op)(x::Rational, y::Rational)
283-
xd, yd = divgcd(x.den, y.den)
283+
xd, yd = divgcd(promote(x.den, y.den)...)
284284
Rational(($chop)(checked_mul(x.num,yd), checked_mul(y.num,xd)), checked_mul(x.den,yd))
285285
end
286286

@@ -305,16 +305,16 @@ for (op,chop) in ((:rem,:rem), (:mod,:mod))
305305
end
306306

307307
function *(x::Rational, y::Rational)
308-
xn, yd = divgcd(x.num, y.den)
309-
xd, yn = divgcd(x.den, y.num)
308+
xn, yd = divgcd(promote(x.num, y.den)...)
309+
xd, yn = divgcd(promote(x.den, y.num)...)
310310
unsafe_rational(checked_mul(xn, yn), checked_mul(xd, yd))
311311
end
312312
function *(x::Rational, y::Integer)
313-
xd, yn = divgcd(x.den, y)
313+
xd, yn = divgcd(promote(x.den, y)...)
314314
unsafe_rational(checked_mul(x.num, yn), xd)
315315
end
316316
function *(y::Integer, x::Rational)
317-
yn, xd = divgcd(y, x.den)
317+
yn, xd = divgcd(promote(y, x.den)...)
318318
unsafe_rational(checked_mul(yn, x.num), xd)
319319
end
320320
/(x::Rational, y::Union{Rational, Integer, Complex{<:Union{Integer,Rational}}}) = x//y

test/rational.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,22 @@ end
575575
@test -Int32(1) // typemax(Int32) - Int32(1) == typemin(Int32) // typemax(Int32)
576576
@test 1 // (typemax(Int128) + BigInt(1)) - 2 == (1 + BigInt(2)*typemin(Int128)) // (BigInt(1) + typemax(Int128))
577577
end
578+
579+
@testset "Promotions on binary operations with Rationals (#36277)" begin
580+
inttypes = (Base.BitInteger_types..., BigInt)
581+
for T in inttypes, S in inttypes
582+
U = Rational{promote_type(T, S)}
583+
@test typeof(one(Rational{T}) + one(S)) == typeof(one(S) + one(Rational{T})) == typeof(one(Rational{T}) + one(Rational{S})) == U
584+
@test typeof(one(Rational{T}) - one(S)) == typeof(one(S) - one(Rational{T})) == typeof(one(Rational{T}) - one(Rational{S})) == U
585+
@test typeof(one(Rational{T}) * one(S)) == typeof(one(S) * one(Rational{T})) == typeof(one(Rational{T}) * one(Rational{S})) == U
586+
@test typeof(one(Rational{T}) // one(S)) == typeof(one(S) // one(Rational{T})) == typeof(one(Rational{T}) // one(Rational{S})) == U
587+
end
588+
@test (-40//3) // 0x5 == 0x5 // (-15//8) == -8//3
589+
@test (-4//7) // (0x1//0x3) == (0x4//0x7) // (-1//3) == -12//7
590+
@test -3//2 + 0x1//0x1 == -3//2 + 0x1 == 0x1//0x1 + (-3//2) == 0x1 + (-3//2) == -1//2
591+
@test 0x3//0x5 - 2//3 == 3//5 - 0x2//0x3 == -1//15
592+
@test rem(-12//5, 0x2//0x1) == rem(-12//5, 0x2) == -2//5
593+
@test mod(0x3//0x1, -4//7) == mod(0x3, -4//7) == -3//7
594+
@test -1//5 * 0x3//0x2 == 0x3//0x2 * -1//5 == -3//10
595+
@test -2//3 * 0x1 == 0x1 * -2//3 == -2//3
596+
end

0 commit comments

Comments
 (0)