Line | Exclusive | Inclusive | Code |
---|---|---|---|
1 | # This file is a part of Julia. License is MIT: https://julialang.org/license | ||
2 | |||
3 | ## generic operations on numbers ## | ||
4 | |||
5 | # Numbers are convertible | ||
6 | convert(::Type{T}, x::T) where {T<:Number} = x | ||
7 | convert(::Type{T}, x::Number) where {T<:Number} = T(x) | ||
8 | |||
9 | """ | ||
10 | isinteger(x) -> Bool | ||
11 | |||
12 | Test whether `x` is numerically equal to some integer. | ||
13 | |||
14 | # Examples | ||
15 | ```jldoctest | ||
16 | julia> isinteger(4.0) | ||
17 | true | ||
18 | ``` | ||
19 | """ | ||
20 | isinteger(x::Integer) = true | ||
21 | |||
22 | """ | ||
23 | iszero(x) | ||
24 | |||
25 | Return `true` if `x == zero(x)`; if `x` is an array, this checks whether | ||
26 | all of the elements of `x` are zero. | ||
27 | |||
28 | # Examples | ||
29 | ```jldoctest | ||
30 | julia> iszero(0.0) | ||
31 | true | ||
32 | |||
33 | julia> iszero([1, 9, 0]) | ||
34 | false | ||
35 | |||
36 | julia> iszero([false, 0, 0]) | ||
37 | true | ||
38 | ``` | ||
39 | """ | ||
40 | 4 (0 %) |
4 (100 %)
samples spent calling
==
iszero(x) = x == zero(x) # fallback method
|
|
41 | |||
42 | """ | ||
43 | isone(x) | ||
44 | |||
45 | Return `true` if `x == one(x)`; if `x` is an array, this checks whether | ||
46 | `x` is an identity matrix. | ||
47 | |||
48 | # Examples | ||
49 | ```jldoctest | ||
50 | julia> isone(1.0) | ||
51 | true | ||
52 | |||
53 | julia> isone([1 0; 0 2]) | ||
54 | false | ||
55 | |||
56 | julia> isone([1 0; 0 true]) | ||
57 | true | ||
58 | ``` | ||
59 | """ | ||
60 | isone(x) = x == one(x) # fallback method | ||
61 | |||
62 | size(x::Number) = () | ||
63 | size(x::Number, d::Integer) = d < 1 ? throw(BoundsError()) : 1 | ||
64 | axes(x::Number) = () | ||
65 | axes(x::Number, d::Integer) = d < 1 ? throw(BoundsError()) : OneTo(1) | ||
66 | eltype(::Type{T}) where {T<:Number} = T | ||
67 | ndims(x::Number) = 0 | ||
68 | ndims(::Type{<:Number}) = 0 | ||
69 | length(x::Number) = 1 | ||
70 | firstindex(x::Number) = 1 | ||
71 | lastindex(x::Number) = 1 | ||
72 | IteratorSize(::Type{<:Number}) = HasShape{0}() | ||
73 | keys(::Number) = OneTo(1) | ||
74 | |||
75 | getindex(x::Number) = x | ||
76 | function getindex(x::Number, i::Integer) | ||
77 | @_inline_meta | ||
78 | @boundscheck i == 1 || throw(BoundsError()) | ||
79 | x | ||
80 | end | ||
81 | function getindex(x::Number, I::Integer...) | ||
82 | @_inline_meta | ||
83 | @boundscheck all(isone, I) || throw(BoundsError()) | ||
84 | x | ||
85 | end | ||
86 | first(x::Number) = x | ||
87 | last(x::Number) = x | ||
88 | copy(x::Number) = x # some code treats numbers as collection-like | ||
89 | |||
90 | """ | ||
91 | signbit(x) | ||
92 | |||
93 | Returns `true` if the value of the sign of `x` is negative, otherwise `false`. | ||
94 | |||
95 | # Examples | ||
96 | ```jldoctest | ||
97 | julia> signbit(-4) | ||
98 | true | ||
99 | |||
100 | julia> signbit(5) | ||
101 | false | ||
102 | |||
103 | julia> signbit(5.5) | ||
104 | false | ||
105 | |||
106 | julia> signbit(-4.1) | ||
107 | true | ||
108 | ``` | ||
109 | """ | ||
110 | signbit(x::Real) = x < 0 | ||
111 | |||
112 | """ | ||
113 | sign(x) | ||
114 | |||
115 | Return zero if `x==0` and ``x/|x|`` otherwise (i.e., ±1 for real `x`). | ||
116 | """ | ||
117 | sign(x::Number) = x == 0 ? x/abs(oneunit(x)) : x/abs(x) | ||
118 | sign(x::Real) = ifelse(x < 0, oftype(one(x),-1), ifelse(x > 0, one(x), typeof(one(x))(x))) | ||
119 | sign(x::Unsigned) = ifelse(x > 0, one(x), oftype(one(x),0)) | ||
120 | abs(x::Real) = ifelse(signbit(x), -x, x) | ||
121 | |||
122 | """ | ||
123 | abs2(x) | ||
124 | |||
125 | Squared absolute value of `x`. | ||
126 | |||
127 | # Examples | ||
128 | ```jldoctest | ||
129 | julia> abs2(-3) | ||
130 | 9 | ||
131 | ``` | ||
132 | """ | ||
133 | abs2(x::Real) = x*x | ||
134 | |||
135 | """ | ||
136 | flipsign(x, y) | ||
137 | |||
138 | Return `x` with its sign flipped if `y` is negative. For example `abs(x) = flipsign(x,x)`. | ||
139 | |||
140 | # Examples | ||
141 | ```jldoctest | ||
142 | julia> flipsign(5, 3) | ||
143 | 5 | ||
144 | |||
145 | julia> flipsign(5, -3) | ||
146 | -5 | ||
147 | ``` | ||
148 | """ | ||
149 | flipsign(x::Real, y::Real) = ifelse(signbit(y), -x, +x) # the + is for type-stability on Bool | ||
150 | |||
151 | """ | ||
152 | copysign(x, y) -> z | ||
153 | |||
154 | Return `z` which has the magnitude of `x` and the same sign as `y`. | ||
155 | |||
156 | # Examples | ||
157 | ```jldoctest | ||
158 | julia> copysign(1, -2) | ||
159 | -1 | ||
160 | |||
161 | julia> copysign(-1, 2) | ||
162 | 1 | ||
163 | ``` | ||
164 | """ | ||
165 | copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, +x) | ||
166 | |||
167 | conj(x::Real) = x | ||
168 | transpose(x::Number) = x | ||
169 | adjoint(x::Number) = conj(x) | ||
170 | angle(z::Real) = atan(zero(z), z) | ||
171 | |||
172 | """ | ||
173 | inv(x) | ||
174 | |||
175 | Return the multiplicative inverse of `x`, such that `x*inv(x)` or `inv(x)*x` | ||
176 | yields [`one(x)`](@ref) (the multiplicative identity) up to roundoff errors. | ||
177 | |||
178 | If `x` is a number, this is essentially the same as `one(x)/x`, but for | ||
179 | some types `inv(x)` may be slightly more efficient. | ||
180 | |||
181 | # Examples | ||
182 | ```jldoctest | ||
183 | julia> inv(2) | ||
184 | 0.5 | ||
185 | |||
186 | julia> inv(1 + 2im) | ||
187 | 0.2 - 0.4im | ||
188 | |||
189 | julia> inv(1 + 2im) * (1 + 2im) | ||
190 | 1.0 + 0.0im | ||
191 | |||
192 | julia> inv(2//3) | ||
193 | 3//2 | ||
194 | ``` | ||
195 | |||
196 | !!! compat "Julia 1.2" | ||
197 | `inv(::Missing)` requires at least Julia 1.2. | ||
198 | """ | ||
199 | inv(x::Number) = one(x)/x | ||
200 | |||
201 | |||
202 | """ | ||
203 | widemul(x, y) | ||
204 | |||
205 | Multiply `x` and `y`, giving the result as a larger type. | ||
206 | |||
207 | # Examples | ||
208 | ```jldoctest | ||
209 | julia> widemul(Float32(3.), 4.) | ||
210 | 12.0 | ||
211 | ``` | ||
212 | """ | ||
213 | widemul(x::Number, y::Number) = widen(x)*widen(y) | ||
214 | |||
215 | iterate(x::Number) = (x, nothing) | ||
216 | iterate(x::Number, ::Any) = nothing | ||
217 | isempty(x::Number) = false | ||
218 | in(x::Number, y::Number) = x == y | ||
219 | |||
220 | map(f, x::Number, ys::Number...) = f(x, ys...) | ||
221 | |||
222 | """ | ||
223 | zero(x) | ||
224 | |||
225 | Get the additive identity element for the type of `x` (`x` can also specify the type itself). | ||
226 | |||
227 | # Examples | ||
228 | ```jldoctest | ||
229 | julia> zero(1) | ||
230 | 0 | ||
231 | |||
232 | julia> zero(big"2.0") | ||
233 | 0.0 | ||
234 | |||
235 | julia> zero(rand(2,2)) | ||
236 | 2×2 Array{Float64,2}: | ||
237 | 0.0 0.0 | ||
238 | 0.0 0.0 | ||
239 | ``` | ||
240 | """ | ||
241 | zero(x::Number) = oftype(x,0) | ||
242 | zero(::Type{T}) where {T<:Number} = convert(T,0) | ||
243 | |||
244 | """ | ||
245 | one(x) | ||
246 | one(T::type) | ||
247 | |||
248 | Return a multiplicative identity for `x`: a value such that | ||
249 | `one(x)*x == x*one(x) == x`. Alternatively `one(T)` can | ||
250 | take a type `T`, in which case `one` returns a multiplicative | ||
251 | identity for any `x` of type `T`. | ||
252 | |||
253 | If possible, `one(x)` returns a value of the same type as `x`, | ||
254 | and `one(T)` returns a value of type `T`. However, this may | ||
255 | not be the case for types representing dimensionful quantities | ||
256 | (e.g. time in days), since the multiplicative | ||
257 | identity must be dimensionless. In that case, `one(x)` | ||
258 | should return an identity value of the same precision | ||
259 | (and shape, for matrices) as `x`. | ||
260 | |||
261 | If you want a quantity that is of the same type as `x`, or of type `T`, | ||
262 | even if `x` is dimensionful, use [`oneunit`](@ref) instead. | ||
263 | |||
264 | # Examples | ||
265 | ```jldoctest | ||
266 | julia> one(3.7) | ||
267 | 1.0 | ||
268 | |||
269 | julia> one(Int) | ||
270 | 1 | ||
271 | |||
272 | julia> import Dates; one(Dates.Day(1)) | ||
273 | 1 | ||
274 | ``` | ||
275 | """ | ||
276 | one(::Type{T}) where {T<:Number} = convert(T,1) | ||
277 | one(x::T) where {T<:Number} = one(T) | ||
278 | # note that convert(T, 1) should throw an error if T is dimensionful, | ||
279 | # so this fallback definition should be okay. | ||
280 | |||
281 | """ | ||
282 | oneunit(x::T) | ||
283 | oneunit(T::Type) | ||
284 | |||
285 | Returns `T(one(x))`, where `T` is either the type of the argument or | ||
286 | (if a type is passed) the argument. This differs from [`one`](@ref) for | ||
287 | dimensionful quantities: `one` is dimensionless (a multiplicative identity) | ||
288 | while `oneunit` is dimensionful (of the same type as `x`, or of type `T`). | ||
289 | |||
290 | # Examples | ||
291 | ```jldoctest | ||
292 | julia> oneunit(3.7) | ||
293 | 1.0 | ||
294 | |||
295 | julia> import Dates; oneunit(Dates.Day) | ||
296 | 1 day | ||
297 | ``` | ||
298 | """ | ||
299 | oneunit(x::T) where {T} = T(one(x)) | ||
300 | oneunit(::Type{T}) where {T} = T(one(T)) | ||
301 | |||
302 | """ | ||
303 | big(T::Type) | ||
304 | |||
305 | Compute the type that represents the numeric type `T` with arbitrary precision. | ||
306 | Equivalent to `typeof(big(zero(T)))`. | ||
307 | |||
308 | # Examples | ||
309 | ```jldoctest | ||
310 | julia> big(Rational) | ||
311 | Rational{BigInt} | ||
312 | |||
313 | julia> big(Float64) | ||
314 | BigFloat | ||
315 | |||
316 | julia> big(Complex{Int}) | ||
317 | Complex{BigInt} | ||
318 | ``` | ||
319 | """ | ||
320 | big(::Type{T}) where {T<:Number} = typeof(big(zero(T))) |