@@ -2,12 +2,15 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
2
2
@eval begin
3
3
Base.:* (l:: $type , r:: $type ) = new_quantity (typeof (l), ustrip (l) * ustrip (r), dimension (l) * dimension (r))
4
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))
5
6
6
7
Base.:* (l:: $type , r:: $base_type ) = new_quantity (typeof (l), ustrip (l) * r, dimension (l))
7
8
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))
8
10
9
11
Base.:* (l:: $base_type , r:: $type ) = new_quantity (typeof (r), l * ustrip (r), dimension (r))
10
12
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)))
11
14
12
15
Base.:* (l:: $type , r:: AbstractDimensions ) = new_quantity (typeof (l), ustrip (l), dimension (l) * r)
13
16
Base.:/ (l:: $type , r:: AbstractDimensions ) = new_quantity (typeof (l), ustrip (l), dimension (l) / r)
41
44
Base.:- (l:: UnionAbstractQuantity ) = new_quantity (typeof (l), - ustrip (l), dimension (l))
42
45
43
46
# Combining different abstract types
44
- for op in (:* , :/ , :+ , :- ),
47
+ for op in (:* , :/ , :+ , :- , :div ),
45
48
(t1, _, _) in ABSTRACT_QUANTITY_TYPES,
46
49
(t2, _, _) in ABSTRACT_QUANTITY_TYPES
47
50
@@ -50,17 +53,6 @@ for op in (:*, :/, :+, :-),
50
53
@eval Base.$ op (l:: $t1 , r:: $t2 ) = $ op (promote (l, r)... )
51
54
end
52
55
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
-
64
56
# We don't promote on the dimension types:
65
57
function Base.:^ (l:: AbstractDimensions{R} , r:: Integer ) where {R}
66
58
return map_dimensions (Base. Fix1 (* , r), l)
@@ -113,36 +105,35 @@ Base.angle(q::UnionAbstractQuantity{T}) where {T<:Complex} = angle(ustrip(q))
113
105
114
106
# ############################# Require dimensionless input ##############################
115
107
# Note that :clamp, :cmp, :sign already work
108
+ # We skip :rad2deg, :deg2rad in case the user defines a rad or deg unit
116
109
for f in (
117
110
:sin , :cos , :tan , :sinh , :cosh , :tanh , :asin , :acos ,
118
111
:asinh , :acosh , :atanh , :sec , :csc , :cot , :asec , :acsc , :acot , :sech , :csch ,
119
112
: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 ,
123
115
)
124
116
@eval function Base. $f (q:: UnionAbstractQuantity )
125
117
iszero (dimension (q)) || throw (DimensionError (q))
126
118
return $ f (ustrip (q))
127
119
end
128
120
end
129
-
130
- for f in (:atan , :atand )
121
+ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:atan , :atand )
131
122
@eval begin
132
- function Base. $f (x:: UnionAbstractQuantity )
123
+ function Base. $f (x:: $type )
133
124
iszero (dimension (x)) || throw (DimensionError (x))
134
125
return $ f (ustrip (x))
135
126
end
136
- function Base. $f (y:: UnionAbstractQuantity , x:: UnionAbstractQuantity )
127
+ function Base. $f (y:: $type , x:: $type )
137
128
dimension (y) == dimension (x) || throw (DimensionError (y, x))
138
129
y, x = promote (y, x)
139
130
return $ f (ustrip (y), ustrip (x))
140
131
end
141
- function Base. $f (y:: UnionAbstractQuantity , x)
132
+ function Base. $f (y:: $type , x:: $base_type )
142
133
iszero (dimension (y)) || throw (DimensionError (y))
143
134
return $ f (ustrip (y), x)
144
135
end
145
- function Base. $f (y, x:: UnionAbstractQuantity )
136
+ function Base. $f (y:: $base_type , x:: $type )
146
137
iszero (dimension (x)) || throw (DimensionError (x))
147
138
return $ f (y, ustrip (x))
148
139
end
@@ -152,24 +143,24 @@ end
152
143
153
144
# ############################# Same dimension as input ##################################
154
145
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 ,
156
148
)
157
149
@eval function Base. $f (q:: UnionAbstractQuantity )
158
150
return new_quantity (typeof (q), $ f (ustrip (q)), dimension (q))
159
151
end
160
152
end
161
- for f in (:copysign , :flipsign , :mod )
153
+ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, f in (:copysign , :flipsign , :mod )
162
154
# 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.
164
156
@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))
168
159
end
169
- function Base. $f (x:: UnionAbstractQuantity , y)
160
+ function Base. $f (x:: $type , y:: $base_type )
170
161
return new_quantity (typeof (x), $ f (ustrip (x), y), dimension (x))
171
162
end
172
- function Base. $f (x, y:: UnionAbstractQuantity )
163
+ function Base. $f (x:: $base_type , y:: $type )
173
164
return $ f (x, ustrip (y))
174
165
end
175
166
end
0 commit comments