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