11for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
22 @eval begin
3- Base.:* (l:: $type , r:: $type ) = new_quantity (typeof (l), ustrip (l) * ustrip (r), dimension (l) * dimension (r))
4- 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))
3+ function Base.:* (l:: $type , r:: $type )
4+ l, r = promote_except_value (l, r)
5+ new_quantity (typeof (l), ustrip (l) * ustrip (r), dimension (l) * dimension (r))
6+ end
7+ function Base.:/ (l:: $type , r:: $type )
8+ l, r = promote_except_value (l, r)
9+ new_quantity (typeof (l), ustrip (l) / ustrip (r), dimension (l) / dimension (r))
10+ end
11+ function Base. div (x:: $type , y:: $type , r:: RoundingMode = RoundToZero)
12+ x, y = promote_except_value (x, y)
13+ new_quantity (typeof (x), div (ustrip (x), ustrip (y), r), dimension (x) / dimension (y))
14+ end
615
7- Base.:* (l:: $type , r:: $base_type ) = new_quantity (typeof (l), ustrip (l) * r, dimension (l))
8- Base.:/ (l:: $type , r:: $base_type ) = new_quantity (typeof (l), ustrip (l) / r, dimension (l))
9- Base. div (x:: $type , y:: Number , r:: RoundingMode = RoundToZero) = new_quantity (typeof (x), div (ustrip (x), y, r), dimension (x))
16+ # The rest of the functions are unchanged because they do not operate on two variables of the custom type
17+ function Base.:* (l:: $type , r:: $base_type )
18+ new_quantity (typeof (l), ustrip (l) * r, dimension (l))
19+ end
20+ function Base.:/ (l:: $type , r:: $base_type )
21+ new_quantity (typeof (l), ustrip (l) / r, dimension (l))
22+ end
23+ function Base. div (x:: $type , y:: Number , r:: RoundingMode = RoundToZero)
24+ new_quantity (typeof (x), div (ustrip (x), y, r), dimension (x))
25+ end
1026
11- Base.:* (l:: $base_type , r:: $type ) = new_quantity (typeof (r), l * ustrip (r), dimension (r))
12- Base.:/ (l:: $base_type , r:: $type ) = new_quantity (typeof (r), l / ustrip (r), inv (dimension (r)))
13- Base. div (x:: Number , y:: $type , r:: RoundingMode = RoundToZero) = new_quantity (typeof (y), div (x, ustrip (y), r), inv (dimension (y)))
27+ function Base.:* (l:: $base_type , r:: $type )
28+ new_quantity (typeof (r), l * ustrip (r), dimension (r))
29+ end
30+ function Base.:/ (l:: $base_type , r:: $type )
31+ new_quantity (typeof (r), l / ustrip (r), inv (dimension (r)))
32+ end
33+ function Base. div (x:: Number , y:: $type , r:: RoundingMode = RoundToZero)
34+ new_quantity (typeof (y), div (x, ustrip (y), r), inv (dimension (y)))
35+ end
1436
15- Base.:* (l:: $type , r:: AbstractDimensions ) = new_quantity (typeof (l), ustrip (l), dimension (l) * r)
16- Base.:/ (l:: $type , r:: AbstractDimensions ) = new_quantity (typeof (l), ustrip (l), dimension (l) / r)
37+ function Base.:* (l:: $type , r:: AbstractDimensions )
38+ new_quantity (typeof (l), ustrip (l), dimension (l) * r)
39+ end
40+ function Base.:/ (l:: $type , r:: AbstractDimensions )
41+ new_quantity (typeof (l), ustrip (l), dimension (l) / r)
42+ end
1743
18- Base.:* (l:: AbstractDimensions , r:: $type ) = new_quantity (typeof (r), ustrip (r), l * dimension (r))
19- Base.:/ (l:: AbstractDimensions , r:: $type ) = new_quantity (typeof (r), inv (ustrip (r)), l / dimension (r))
44+ function Base.:* (l:: AbstractDimensions , r:: $type )
45+ new_quantity (typeof (r), ustrip (r), l * dimension (r))
46+ end
47+ function Base.:/ (l:: AbstractDimensions , r:: $type )
48+ new_quantity (typeof (r), inv (ustrip (r)), l / dimension (r))
49+ end
2050 end
2151end
2252
@@ -27,6 +57,7 @@ Base.:/(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(-, l, r)
2757for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, op in (:+ , :- )
2858 @eval begin
2959 function Base. $op (l:: $type , r:: $type )
60+ l, r = promote_except_value (l, r)
3061 dimension (l) == dimension (r) || throw (DimensionError (l, r))
3162 return new_quantity (typeof (l), $ op (ustrip (l), ustrip (r)), dimension (l))
3263 end
@@ -50,7 +81,7 @@ for op in (:*, :/, :+, :-, :div, :atan, :atand, :copysign, :flipsign, :mod),
5081
5182 t1 == t2 && continue
5283
53- @eval Base.$ op (l:: $t1 , r:: $t2 ) = $ op (promote (l, r)... )
84+ @eval Base.$ op (l:: $t1 , r:: $t2 ) = $ op (promote_except_value (l, r)... )
5485end
5586
5687# We don't promote on the dimension types:
@@ -125,6 +156,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:atan, :atand)
125156 return $ f (ustrip (x))
126157 end
127158 function Base. $f (y:: $type , x:: $type )
159+ y, x = promote_except_value (y, x)
128160 dimension (y) == dimension (x) || throw (DimensionError (y, x))
129161 return $ f (ustrip (y), ustrip (x))
130162 end
@@ -154,6 +186,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:copysign, :flipsign,
154186 # and ignore any dimensions on y, since those will cancel out.
155187 @eval begin
156188 function Base. $f (x:: $type , y:: $type )
189+ x, y = promote_except_value (x, y)
157190 return new_quantity (typeof (x), $ f (ustrip (x), ustrip (y)), dimension (x))
158191 end
159192 function Base. $f (x:: $type , y:: $base_type )
0 commit comments