Skip to content

Commit 05b4722

Browse files
authored
Make false a strong zero against 1//0 (#57409)
Make false a strong zero against rationals and add tests. Also adds tests for `*(::Bool, ::Float64)` because I didn't find tests for that anywhere. Implements #57068
1 parent 94486f1 commit 05b4722

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

base/rational.jl

+6
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ function *(y::Integer, x::Rational)
415415
yn, xd = divgcd(promote(y, x.den)...)
416416
unsafe_rational(checked_mul(yn, x.num), xd)
417417
end
418+
# make `false` a "strong zero": false*1//0 == 0//1 #57409
419+
# This is here instead of in bool.jl with the AbstractFloat method for bootstrapping
420+
function *(x::Bool, y::T)::promote_type(Bool,T) where T<:Rational
421+
return ifelse(x, y, copysign(zero(y), y))
422+
end
423+
*(y::Rational, x::Bool) = x * y
418424
/(x::Rational, y::Union{Rational, Integer, Complex{<:Union{Integer,Rational}}}) = x//y
419425
/(x::Union{Integer, Complex{<:Union{Integer,Rational}}}, y::Rational) = x//y
420426
inv(x::Rational{T}) where {T} = checked_den(x.den, x.num)

test/numbers.jl

+26-1
Original file line numberDiff line numberDiff line change
@@ -2192,11 +2192,36 @@ for T = (UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64,UInt128,Int128)
21922192
end
21932193
end
21942194

2195-
@testset "Irrational/Bool multiplication" begin
2195+
@testset "Bool multiplication" begin
21962196
@test false*pi === 0.0
21972197
@test pi*false === 0.0
21982198
@test true*pi === Float64(pi)
21992199
@test pi*true === Float64(pi)
2200+
2201+
@test false*Inf === 0.0
2202+
@test Inf*false === 0.0
2203+
@test true*Inf === Inf
2204+
@test Inf*true === Inf
2205+
2206+
@test false*NaN === 0.0
2207+
@test NaN*false === 0.0
2208+
@test true*NaN === NaN
2209+
@test NaN*true === NaN
2210+
2211+
@test false*-Inf === -0.0
2212+
@test -Inf*false === -0.0
2213+
@test true*-Inf === -Inf
2214+
@test -Inf*true === -Inf
2215+
2216+
@test false*1//0 === 0//1
2217+
@test 1//0*false === 0//1
2218+
@test true*1//0 === 1//0
2219+
@test 1//0*true === 1//0
2220+
2221+
@test false*-1//0 === 0//1
2222+
@test -1//0*false === 0//1
2223+
@test true*-1//0 === -1//0
2224+
@test -1//0*true === -1//0
22002225
end
22012226
# issue #5492
22022227
@test -0.0 + false === -0.0

0 commit comments

Comments
 (0)