Skip to content

Commit 96aa0c4

Browse files
authored
Add methods for iseven, isodd (#829)
1 parent 26cb2e6 commit 96aa0c4

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/Unitful.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Base: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, at
1010
sinpi, cospi, sinc, cosc, cis, cispi, sincos
1111
import Base: eps, mod, rem, div, fld, cld, divrem, trunc, round, sign, signbit
1212
import Base: isless, isapprox, isinteger, isreal, isinf, isfinite, isnan
13+
import Base: iseven, isodd
1314
import Base: copysign, flipsign
1415
import Base: prevfloat, nextfloat, maxintfloat, rat, step
1516
import Base: length, float, last, one, oneunit, zero, range

src/quantities.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ end
314314
_dimerr(f) = error("$f can only be well-defined for dimensionless ",
315315
"numbers. For dimensionful numbers, different input units yield physically ",
316316
"different results.")
317-
isinteger(x::AbstractQuantity) = _dimerr(isinteger)
318-
isinteger(x::DimensionlessQuantity) = isinteger(uconvert(NoUnits, x))
317+
for f in [:isinteger, :iseven, :isodd]
318+
@eval $f(x::AbstractQuantity) = _dimerr($f)
319+
@eval $f(x::DimensionlessQuantity) = $f(uconvert(NoUnits, x))
320+
end
319321

320322
_rounderr() = error("specify the type of the quantity to convert to ",
321323
"when rounding quantities. Example: round(typeof(1u\"m\"), 137u\"cm\").")

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,20 @@ Base.:(<=)(x::Issue399, y::Issue399) = x.num <= y.num
911911
@test_throws ErrorException isinteger(1.0m)
912912
@test isinteger(1.4m/mm)
913913
@test !isinteger(1.4mm/m)
914+
@test_throws ErrorException iseven(1.0m)
915+
@test iseven(2rad)
916+
@test !iseven(1rad)
917+
if hasmethod(iseven, (Float64,))
918+
@test iseven(0.5m/mm)
919+
@test !iseven(0.125m/mm)
920+
end
921+
@test_throws ErrorException isodd(1.0m)
922+
@test isodd(1rad)
923+
@test !isodd(2rad)
924+
if hasmethod(isodd, (Float64,))
925+
@test isodd(0.125m/mm)
926+
@test !isodd(0.5m/mm)
927+
end
914928
@test isfinite(1.0m)
915929
@test !isfinite(Inf*m)
916930
@test isnan(NaN*m)

0 commit comments

Comments
 (0)