From 7a36d7b1fd71ea6269ee7dfe7db975bbc46b5cba Mon Sep 17 00:00:00 2001 From: Samuel Buercklin Date: Sat, 23 Oct 2021 03:26:38 -0400 Subject: [PATCH] Remove unnecessary Bool-AbstractQuantity multiplication methods (#491) * made Bool-Quantity multiplication symmetric * added tests for Bool-Quantity multiplication * Remove unnecessary multiplication methods for Bool, AbstractQuantity Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> * Remove comment from previous Bool, AbstractQuantity methods Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> * Add NaN, -Inf tests for multiplication by false Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- src/quantities.jl | 5 ----- test/runtests.jl | 6 ++++++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/quantities.jl b/src/quantities.jl index a25863d4..7fde2b46 100644 --- a/src/quantities.jl +++ b/src/quantities.jl @@ -29,11 +29,6 @@ Quantity(x::Number, y::Units{()}) = x *(x::AbstractQuantity, y::Units, z::Units...) = Quantity(x.val, *(unit(x),y,z...)) *(x::AbstractQuantity, y::AbstractQuantity) = Quantity(x.val*y.val, unit(x)*unit(y)) -# Next two lines resolves some method ambiguity: -*(x::Bool, y::T) where {T <: AbstractQuantity} = - ifelse(x, y, ifelse(signbit(y), -zero(y), zero(y))) -*(x::AbstractQuantity, y::Bool) = Quantity(x.val*y, unit(x)) - *(y::Number, x::AbstractQuantity) = *(x,y) function *(x::AbstractQuantity, y::Number) x isa AffineQuantity && diff --git a/test/runtests.jl b/test/runtests.jl index d2012bc6..9cd426a3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -586,6 +586,12 @@ end @test @inferred((3m)*m) === 3*(m*m) # Associative multiplication @test @inferred(true*1kg) === 1kg # Boolean multiplication (T) @test @inferred(false*1kg) === 0kg # Boolean multiplication (F) + @test @inferred(true*(1+im)kg) === (1+im)kg # Boolean-complex multiplication (T) + @test @inferred(false*(1+im)kg) === (0+0im)kg # Boolean-complex multiplication (F) + @test @inferred((1+im)kg*true) === (1+im)kg # Complex-boolean multiplication (T) + @test @inferred((1+im)kg*false) === (0+0im)kg # Complex-boolean multiplication (F) + @test @inferred((NaN*kg)*false) === 0.0kg # `false` acts as "strong zero" + @test @inferred(false*(-Inf*kg)) === -0.0kg # `false` acts as "strong zero" @test typeof(one(eltype([1.0s, 1kg]))) <: Float64 # issue 159, multiplicative identity end @testset "> Division" begin