Skip to content

Commit cd782c4

Browse files
authored
Merge branch 'more-functions-3' into fixed-promotion
2 parents 8a69a9e + 40521a1 commit cd782c4

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/disambiguities.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,3 @@ Base.:*(l::AbstractDimensions, r::Number) = error("Please use an `UnionAbstractQ
1212
Base.:*(l::Number, r::AbstractDimensions) = error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
1313
Base.:/(l::AbstractDimensions, r::Number) = error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
1414
Base.:/(l::Number, r::AbstractDimensions) = error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
15-
16-
if VERSION < v"1.7"
17-
for I in (Base.MultiplicativeInverses.UnsignedMultiplicativeInverse, Base.MultiplicativeInverses.SignedMultiplicativeInverse)
18-
@eval function Base.div(x::T, y::$I{T}, r::RoundingMode=RoundToZero) where {T<:AbstractGenericQuantity}
19-
return new_quantity(typeof(x), div(ustrip(x), y, r), dimension(x))
20-
end
21-
end
22-
end

src/math.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
2020
function Base.:/(l::$type, r::$base_type)
2121
new_quantity(typeof(l), ustrip(l) / r, dimension(l))
2222
end
23-
function Base.div(x::$type, y::$base_type, r::RoundingMode=RoundToZero)
23+
function Base.div(x::$type, y::Number, r::RoundingMode=RoundToZero)
2424
new_quantity(typeof(x), div(ustrip(x), y, r), dimension(x))
2525
end
2626

@@ -30,7 +30,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
3030
function Base.:/(l::$base_type, r::$type)
3131
new_quantity(typeof(r), l / ustrip(r), inv(dimension(r)))
3232
end
33-
function Base.div(x::$base_type, y::$type, r::RoundingMode=RoundToZero)
33+
function Base.div(x::Number, y::$type, r::RoundingMode=RoundToZero)
3434
new_quantity(typeof(y), div(x, ustrip(y), r), inv(dimension(y)))
3535
end
3636

@@ -175,7 +175,7 @@ end
175175
############################## Same dimension as input ##################################
176176
for f in (
177177
:float, :abs, :real, :imag, :conj, :adjoint, :unsigned,
178-
:nextfloat, :prevfloat, :identity, :transpose,
178+
:nextfloat, :prevfloat, :identity, :transpose, :significand
179179
)
180180
@eval function Base.$f(q::UnionAbstractQuantity)
181181
return new_quantity(typeof(q), $f(ustrip(q)), dimension(q))
@@ -206,7 +206,7 @@ end
206206
function Base.round(::Type{Ti}, q::UnionAbstractQuantity, r::RoundingMode=RoundNearest) where {Ti<:Integer}
207207
return new_quantity(typeof(q), round(Ti, ustrip(q), r), dimension(q))
208208
end
209-
for f in (:floor, :trunc, :ceil, :significand)
209+
for f in (:floor, :trunc, :ceil)
210210
@eval begin
211211
function Base.$f(q::UnionAbstractQuantity)
212212
return new_quantity(typeof(q), $f(ustrip(q)), dimension(q))

test/unittests.jl

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ using StaticArrays: SArray, MArray
99
using LinearAlgebra: norm
1010
using Test
1111

12+
function record_show(s, f=show)
13+
io = IOBuffer()
14+
f(io, s)
15+
return String(take!(io))
16+
end
17+
1218
@testset "Basic utilities" begin
1319

1420
for Q in [Quantity, GenericQuantity], T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
@@ -447,14 +453,8 @@ end
447453
# Conversion to Rational without specifying type
448454
@test convert(Rational, FixedRational{UInt8,6}(2)) === Rational{UInt8}(2)
449455

450-
# Showing rationals
451-
function show_string(i)
452-
io = IOBuffer()
453-
show(io, i)
454-
return String(take!(io))
455-
end
456-
@test show_string(FixedRational{Int,10}(2)) == "2"
457-
@test show_string(FixedRational{Int,10}(11//10)) == "11//10"
456+
@test record_show(FixedRational{Int,10}(2)) == "2"
457+
@test record_show(FixedRational{Int,10}(11//10)) == "11//10"
458458

459459
# Promotion rules
460460
@test promote_type(FixedRational{Int64,10},FixedRational{BigInt,10}) == FixedRational{BigInt,10}
@@ -1164,6 +1164,8 @@ end
11641164
for y in valid_inputs[end-3:end]
11651165
qy_dimensionless = Quantity(y, D)
11661166
qy_dimensions = Quantity(y, convert(D, dimension(u"m/s")))
1167+
@eval @test $f($y, $qx_dimensionless) == $f($y, $x)
1168+
@eval @test $f($qy_dimensionless, $x) == $f($y, $x)
11671169
@eval @test $f($qy_dimensionless, $qx_dimensionless) == $f($y, $x)
11681170
@eval @test $f($qy_dimensions, $qx_dimensions) == $f($y, $x)
11691171
@eval @test_throws DimensionError $f($qy_dimensions, $x)
@@ -1172,6 +1174,8 @@ end
11721174
end
11731175
end
11741176
end
1177+
s = record_show(DimensionError(u"km/s"), showerror)
1178+
@test occursin("not dimensionless", s)
11751179
end
11761180

11771181
@testset "Assorted dimensionful functions" begin
@@ -1180,7 +1184,7 @@ end
11801184
:nextfloat, :prevfloat, :identity, :transpose,
11811185
:copysign, :flipsign, :mod, :modf,
11821186
:floor, :trunc, :ceil, :significand,
1183-
:ldexp, :round
1187+
:ldexp, :round,
11841188
)
11851189
for Q in (Quantity, GenericQuantity), D in (Dimensions, SymbolicDimensions), f in functions
11861190
T = f in (:abs, :real, :imag, :conj) ? ComplexF64 : Float64
@@ -1200,6 +1204,11 @@ end
12001204
qx_dimensions = Q(x, dim)
12011205
qy_dimensions = Q(y, dim)
12021206
@eval @test $f($qx_dimensions, $qy_dimensions) == $Q($f($x, $y), $dim)
1207+
if f in (:copysign, :flipsign, :mod)
1208+
# Also do test without dimensions
1209+
@eval @test $f($x, $qy_dimensions) == $f($x, $y)
1210+
@eval @test $f($qx_dimensions, $y) == $Q($f($x, $y), $dim)
1211+
end
12031212
end
12041213
end
12051214
elseif f == :unsigned
@@ -1208,7 +1217,7 @@ end
12081217
qx_dimensions = Q(x, dim)
12091218
@eval @test $f($qx_dimensions) == $Q($f($x), $dim)
12101219
end
1211-
elseif f == :round
1220+
elseif f in (:round, :floor, :trunc, :ceil)
12121221
for x in 5rand(T, 3) .- 2.5
12131222
dim = convert(D, dimension(u"m/s"))
12141223
qx_dimensions = Q(x, dim)
@@ -1237,3 +1246,18 @@ end
12371246
end
12381247
end
12391248
end
1249+
1250+
@testset "Test div" begin
1251+
for Q in (Quantity, GenericQuantity)
1252+
x = Q{Int}(10, length=1)
1253+
y = Q{Int}(3, mass=-1)
1254+
@test div(x, y) == Q{Int}(3, length=1, mass=1)
1255+
@test div(x, 3) == Q{Int}(3, length=1)
1256+
@test div(10, y) == Q{Int}(3, mass=1)
1257+
if VERSION >= v"1.9"
1258+
@test div(x, y, RoundFromZero) == Q{Int}(4, length=1, mass=1)
1259+
@test div(x, 3, RoundFromZero) == Q{Int}(4, length=1)
1260+
@test div(10, y, RoundFromZero) == Q{Int}(4, mass=1)
1261+
end
1262+
end
1263+
end

0 commit comments

Comments
 (0)