1
1
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
2
2
@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
6
15
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
10
26
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
14
36
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
17
43
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
20
50
end
21
51
end
22
52
@@ -27,6 +57,7 @@ Base.:/(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(-, l, r)
27
57
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, op in (:+ , :- )
28
58
@eval begin
29
59
function Base. $op (l:: $type , r:: $type )
60
+ l, r = promote_except_value (l, r)
30
61
dimension (l) == dimension (r) || throw (DimensionError (l, r))
31
62
return new_quantity (typeof (l), $ op (ustrip (l), ustrip (r)), dimension (l))
32
63
end
@@ -50,7 +81,7 @@ for op in (:*, :/, :+, :-, :div, :atan, :atand, :copysign, :flipsign, :mod),
50
81
51
82
t1 == t2 && continue
52
83
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)... )
54
85
end
55
86
56
87
# We don't promote on the dimension types:
@@ -125,6 +156,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:atan, :atand)
125
156
return $ f (ustrip (x))
126
157
end
127
158
function Base. $f (y:: $type , x:: $type )
159
+ y, x = promote_except_value (y, x)
128
160
dimension (y) == dimension (x) || throw (DimensionError (y, x))
129
161
return $ f (ustrip (y), ustrip (x))
130
162
end
@@ -154,6 +186,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:copysign, :flipsign,
154
186
# and ignore any dimensions on y, since those will cancel out.
155
187
@eval begin
156
188
function Base. $f (x:: $type , y:: $type )
189
+ x, y = promote_except_value (x, y)
157
190
return new_quantity (typeof (x), $ f (ustrip (x), ustrip (y)), dimension (x))
158
191
end
159
192
function Base. $f (x:: $type , y:: $base_type )
0 commit comments