Skip to content

Commit

Permalink
Throw error for div etc. with affine quantities (#354)
Browse files Browse the repository at this point in the history
* Throw error for div etc. with affine quantities

* Fix tests on Julia 1.0
  • Loading branch information
sostock authored Jun 15, 2020
1 parent 41bf6eb commit 1bfdad7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Base: complex, widen, reim # handled in complex.jl
import Base: exp, exp10, exp2, expm1, log, log10, log1p, log2
import Base: sin, cos, tan, cot, sec, csc, atan, cis

import Base: eps, mod, rem, div, fld, cld, trunc, round, sign, signbit
import Base: eps, mod, rem, div, fld, cld, divrem, trunc, round, sign, signbit
import Base: isless, isapprox, isinteger, isreal, isinf, isfinite, isnan
import Base: copysign, flipsign
import Base: prevfloat, nextfloat, maxintfloat, rat, step
Expand Down
21 changes: 21 additions & 0 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ for f in (:mod, :rem)
end
end

_affineerror(f, args...) =
throw(AffineError("an invalid operation was attempted with affine quantities: $f($(join(args, ", ")))"))

for f in (:div, :rem, :divrem)
for r = (RoundNearest, RoundNearestTiesAway, RoundNearestTiesUp,
RoundToZero, RoundUp, RoundDown)
@eval begin
$f(x::AffineQuantity, y::AffineQuantity, ::typeof($r)) = _affineerror($f, x, y, $r)
$f(x::AffineQuantity, y::AbstractQuantity, ::typeof($r)) = _affineerror($f, x, y, $r)
$f(x::AbstractQuantity, y::AffineQuantity, ::typeof($r)) = _affineerror($f, x, y, $r)
end
end
end
for f = (:div, :cld, :fld, :rem, :mod)
@eval begin
$f(x::AffineQuantity, y::AffineQuantity) = _affineerror($f, x, y)
$f(x::AffineQuantity, y::AbstractQuantity) = _affineerror($f, x, y)
$f(x::AbstractQuantity, y::AffineQuantity) = _affineerror($f, x, y)
end
end

Base.mod2pi(x::DimensionlessQuantity) = mod2pi(uconvert(NoUnits, x))
Base.mod2pi(x::AbstractQuantity{S, NoDims, <:Units{(Unitful.Unit{:Degree, NoDims}(0, 1//1),),
NoDims}}) where S = mod(x, 360°)
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ end
@test_throws AffineError °C*°C
@test_throws AffineError °C*K
@test_throws AffineError (0°C)*(0°C)
@test_throws AffineError (1°C)/(1°C)
@test_throws AffineError °C^2
let x = 2
@test_throws AffineError °C^x
Expand All @@ -217,6 +218,22 @@ end
@test_throws AffineError (32°F) / 2
@test_throws AffineError 2 / (32°F)

for f = (:div, :rem, :divrem)
@eval for r = (RoundNearest, RoundNearestTiesAway, RoundNearestTiesUp,
RoundToZero, RoundUp, RoundDown)
@test_throws AffineError $f(32°F, 2°F, r)
@test_throws AffineError $f(32°F, 2K, r)
@test_throws AffineError $f(32K, 2°F, r)
end
end
for f = (:div, :cld, :fld, :rem, :mod, :divrem, :fldmod)
@eval begin
@test_throws AffineError $f(32°F, 2°F)
@test_throws AffineError $f(32°F, 2K)
@test_throws AffineError $f(32K, 2°F)
end
end

@test zero(100°C) === 0K
@test zero(typeof(100°C)) === 0K
@test oneunit(100°C) === 1K
Expand Down

0 comments on commit 1bfdad7

Please sign in to comment.