Skip to content

Commit

Permalink
Remove unnecessary Bool-AbstractQuantity multiplication methods (#491)
Browse files Browse the repository at this point in the history
* made Bool-Quantity multiplication symmetric

* added tests for Bool-Quantity multiplication

* Remove unnecessary multiplication methods for Bool, AbstractQuantity

Co-authored-by: Sebastian Stock <[email protected]>

* Remove comment from previous Bool, AbstractQuantity methods

Co-authored-by: Sebastian Stock <[email protected]>

* Add NaN, -Inf tests for multiplication by false

Co-authored-by: Sebastian Stock <[email protected]>

Co-authored-by: Sebastian Stock <[email protected]>
  • Loading branch information
SBuercklin and sostock authored Oct 23, 2021
1 parent a529861 commit 7a36d7b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7a36d7b

Please sign in to comment.