Skip to content

Commit 9702aab

Browse files
committed
Fix method ambiguities
1 parent f3fe3d4 commit 9702aab

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/math.jl

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
22
@eval begin
33
Base.:*(l::$type, r::$type) = new_quantity(typeof(l), ustrip(l) * ustrip(r), dimension(l) * dimension(r))
44
Base.:/(l::$type, r::$type) = new_quantity(typeof(l), ustrip(l) / ustrip(r), dimension(l) / dimension(r))
5+
Base.div(x::$type, y::$type, r::RoundingMode=RoundToZero) = new_quantity(typeof(x), div(ustrip(x), ustrip(y), r), dimension(x) / dimension(y))
56

67
Base.:*(l::$type, r::$base_type) = new_quantity(typeof(l), ustrip(l) * r, dimension(l))
78
Base.:/(l::$type, r::$base_type) = new_quantity(typeof(l), ustrip(l) / r, dimension(l))
9+
Base.div(x::$type, y::$base_type, r::RoundingMode=RoundToZero) = new_quantity(typeof(x), div(ustrip(x), y, r), dimension(x))
810

911
Base.:*(l::$base_type, r::$type) = new_quantity(typeof(r), l * ustrip(r), dimension(r))
1012
Base.:/(l::$base_type, r::$type) = new_quantity(typeof(r), l / ustrip(r), inv(dimension(r)))
13+
Base.div(x::$base_type, y::$type, r::RoundingMode=RoundToZero) = new_quantity(typeof(y), div(x, ustrip(y), r), inv(dimension(y)))
1114

1215
Base.:*(l::$type, r::AbstractDimensions) = new_quantity(typeof(l), ustrip(l), dimension(l) * r)
1316
Base.:/(l::$type, r::AbstractDimensions) = new_quantity(typeof(l), ustrip(l), dimension(l) / r)
@@ -41,7 +44,7 @@ end
4144
Base.:-(l::UnionAbstractQuantity) = new_quantity(typeof(l), -ustrip(l), dimension(l))
4245

4346
# Combining different abstract types
44-
for op in (:*, :/, :+, :-),
47+
for op in (:*, :/, :+, :-, :div),
4548
(t1, _, _) in ABSTRACT_QUANTITY_TYPES,
4649
(t2, _, _) in ABSTRACT_QUANTITY_TYPES
4750

@@ -50,17 +53,6 @@ for op in (:*, :/, :+, :-),
5053
@eval Base.$op(l::$t1, r::$t2) = $op(promote(l, r)...)
5154
end
5255

53-
function Base.div(x::UnionAbstractQuantity, y::UnionAbstractQuantity, r::RoundingMode=RoundToZero)
54-
x, y = promote(x, y)
55-
return new_quantity(typeof(x), div(ustrip(x), ustrip(y), r), dimension(x) / dimension(y))
56-
end
57-
function Base.div(x::UnionAbstractQuantity, y, r::RoundingMode=RoundToZero)
58-
return new_quantity(typeof(x), div(ustrip(x), y, r), dimension(x))
59-
end
60-
function Base.div(x, y::UnionAbstractQuantity, r::RoundingMode=RoundToZero)
61-
return new_quantity(typeof(y), div(x, ustrip(y), r), inv(dimension(y)))
62-
end
63-
6456
# We don't promote on the dimension types:
6557
function Base.:^(l::AbstractDimensions{R}, r::Integer) where {R}
6658
return map_dimensions(Base.Fix1(*, r), l)
@@ -113,36 +105,35 @@ Base.angle(q::UnionAbstractQuantity{T}) where {T<:Complex} = angle(ustrip(q))
113105

114106
############################## Require dimensionless input ##############################
115107
# Note that :clamp, :cmp, :sign already work
108+
# We skip :rad2deg, :deg2rad in case the user defines a rad or deg unit
116109
for f in (
117110
:sin, :cos, :tan, :sinh, :cosh, :tanh, :asin, :acos,
118111
:asinh, :acosh, :atanh, :sec, :csc, :cot, :asec, :acsc, :acot, :sech, :csch,
119112
:coth, :asech, :acsch, :acoth, :sinc, :cosc, :cosd, :cotd, :cscd, :secd,
120-
:sind, :tand, :acosd, :acotd, :acscd, :asecd, :asind, :rad2deg, :deg2rad,
121-
:log, :log2, :log10, :log1p, :exp, :exp2, :expm1, :frexp,
122-
:sinpi, :cospi, :exp10, :transpose, :exponent,
113+
:sinpi, :cospi, :sind, :tand, :acosd, :acotd, :acscd, :asecd, :asind,
114+
:log, :log2, :log10, :log1p, :exp, :exp2, :exp10, :expm1, :frexp, :exponent,
123115
)
124116
@eval function Base.$f(q::UnionAbstractQuantity)
125117
iszero(dimension(q)) || throw(DimensionError(q))
126118
return $f(ustrip(q))
127119
end
128120
end
129-
130-
for f in (:atan, :atand)
121+
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:atan, :atand)
131122
@eval begin
132-
function Base.$f(x::UnionAbstractQuantity)
123+
function Base.$f(x::$type)
133124
iszero(dimension(x)) || throw(DimensionError(x))
134125
return $f(ustrip(x))
135126
end
136-
function Base.$f(y::UnionAbstractQuantity, x::UnionAbstractQuantity)
127+
function Base.$f(y::$type, x::$type)
137128
dimension(y) == dimension(x) || throw(DimensionError(y, x))
138129
y, x = promote(y, x)
139130
return $f(ustrip(y), ustrip(x))
140131
end
141-
function Base.$f(y::UnionAbstractQuantity, x)
132+
function Base.$f(y::$type, x::$base_type)
142133
iszero(dimension(y)) || throw(DimensionError(y))
143134
return $f(ustrip(y), x)
144135
end
145-
function Base.$f(y, x::UnionAbstractQuantity)
136+
function Base.$f(y::$base_type, x::$type)
146137
iszero(dimension(x)) || throw(DimensionError(x))
147138
return $f(y, ustrip(x))
148139
end
@@ -152,24 +143,24 @@ end
152143

153144
############################## Same dimension as input ##################################
154145
for f in (
155-
:abs, :real, :imag, :conj, :adjoint, :unsigned, :nextfloat, :prevfloat, :identity,
146+
:abs, :real, :imag, :conj, :adjoint, :unsigned, :nextfloat, :prevfloat,
147+
:identity, :transpose,
156148
)
157149
@eval function Base.$f(q::UnionAbstractQuantity)
158150
return new_quantity(typeof(q), $f(ustrip(q)), dimension(q))
159151
end
160152
end
161-
for f in (:copysign, :flipsign, :mod)
153+
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:copysign, :flipsign, :mod)
162154
# These treat the x as the magnitude, so we take the dimensions from there,
163-
# and ignore any dimensions on y
155+
# and ignore any dimensions on y, since those will cancel out.
164156
@eval begin
165-
function Base.$f(x::UnionAbstractQuantity, y::UnionAbstractQuantity)
166-
Q = promote_type(typeof(x), typeof(y))
167-
return new_quantity(Q, $f(ustrip(x), ustrip(y)), dimension(x))
157+
function Base.$f(x::$type, y::$type)
158+
return new_quantity(typeof(x), $f(ustrip(x), ustrip(y)), dimension(x))
168159
end
169-
function Base.$f(x::UnionAbstractQuantity, y)
160+
function Base.$f(x::$type, y::$base_type)
170161
return new_quantity(typeof(x), $f(ustrip(x), y), dimension(x))
171162
end
172-
function Base.$f(x, y::UnionAbstractQuantity)
163+
function Base.$f(x::$base_type, y::$type)
173164
return $f(x, ustrip(y))
174165
end
175166
end

0 commit comments

Comments
 (0)