Skip to content

Commit 80cd5d3

Browse files
committed
Refactor definition of mod
1 parent b05cf1d commit 80cd5d3

File tree

1 file changed

+4
-26
lines changed

1 file changed

+4
-26
lines changed

src/math.jl

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ Base.:*(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(+, l, r)
5656
Base.:/(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(-, l, r)
5757

5858
# Defines + and -
59-
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, op in (:+, :-)
59+
for (type, true_base_type, _) in ABSTRACT_QUANTITY_TYPES, op in (:+, :-, :mod)
60+
# Only define `mod` on `Number` types:
61+
base_type = (op == :mod && !(true_base_type <: Number)) ? Number : true_base_type
6062
@eval begin
6163
function Base.$op(l::$type, r::$type)
6264
l, r = promote_except_value(l, r)
@@ -77,7 +79,7 @@ end
7779
Base.:-(l::UnionAbstractQuantity) = new_quantity(typeof(l), -ustrip(l), dimension(l))
7880

7981
# Combining different abstract types
80-
for op in (:*, :/, :+, :-, :atan, :atand, :copysign, :flipsign, :div),
82+
for op in (:*, :/, :+, :-, :atan, :atand, :copysign, :flipsign, :div, :mod),
8183
(t1, _, _) in ABSTRACT_QUANTITY_TYPES,
8284
(t2, _, _) in ABSTRACT_QUANTITY_TYPES
8385

@@ -234,30 +236,6 @@ for (type, true_base_type, _) in ABSTRACT_QUANTITY_TYPES, rounding_mode in (Roun
234236
end
235237
end
236238
end
237-
# Define :mod
238-
for (type, true_base_type, _) in ABSTRACT_QUANTITY_TYPES
239-
240-
# We don't want to go more generic than `Number` for mod and rem
241-
base_type = true_base_type <: Number ? true_base_type : Number
242-
243-
for (type2, _, _) in ABSTRACT_QUANTITY_TYPES
244-
@eval function Base.mod(x::$type, y::$type2)
245-
x, y = promote_except_value(x, y)
246-
dimension(x) == dimension(y) || throw(DimensionError(x, y))
247-
return new_quantity(typeof(x), mod(ustrip(x), ustrip(y)), dimension(x))
248-
end
249-
end
250-
@eval begin
251-
function Base.mod(x::$type, y::$base_type)
252-
iszero(dimension(x)) || throw(DimensionError(x))
253-
return new_quantity(typeof(x), mod(ustrip(x), y), dimension(x))
254-
end
255-
function Base.mod(x::$base_type, y::$type)
256-
iszero(dimension(y)) || throw(DimensionError(y))
257-
return new_quantity(typeof(y), mod(x, ustrip(y)), dimension(y))
258-
end
259-
end
260-
end
261239
function Base.ldexp(x::UnionAbstractQuantity, n::Integer)
262240
return new_quantity(typeof(x), ldexp(ustrip(x), n), dimension(x))
263241
end

0 commit comments

Comments
 (0)