## "Fossies" - the Fresh Open Source Software Archive ### Member "julia-1.1.1/share/julia/base/number.jl" (16 May 2019, 6699 Bytes) of package /linux/misc/julia-1.1.1-linux-i686.tar.gz:

As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Julia source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

```    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 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) = convert(Int,d)<1 ? throw(BoundsError()) : 1
64 axes(x::Number) = ()
65 axes(x::Number,d) = convert(Int,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([i == 1 for i in 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     divrem(x, y)
92
93 The quotient and remainder from Euclidean division. Equivalent to `(div(x,y), rem(x,y))` or
94 `(x÷y, x%y)`.
95
96 # Examples
97 ```jldoctest
98 julia> divrem(3,7)
99 (0, 3)
100
101 julia> divrem(7,3)
102 (2, 1)
103 ```
104 """
105 divrem(x,y) = (div(x,y),rem(x,y))
106
107 """
108     fldmod(x, y)
109
110 The floored quotient and modulus after division. Equivalent to `(fld(x,y), mod(x,y))`.
111 """
112 fldmod(x,y) = (fld(x,y),mod(x,y))
113
114 """
115     signbit(x)
116
117 Returns `true` if the value of the sign of `x` is negative, otherwise `false`.
118
119 # Examples
120 ```jldoctest
121 julia> signbit(-4)
122 true
123
124 julia> signbit(5)
125 false
126
127 julia> signbit(5.5)
128 false
129
130 julia> signbit(-4.1)
131 true
132 ```
133 """
134 signbit(x::Real) = x < 0
135
136 """
137     sign(x)
138
139 Return zero if `x==0` and ``x/|x|`` otherwise (i.e., ±1 for real `x`).
140 """
141 sign(x::Number) = x == 0 ? x/abs(oneunit(x)) : x/abs(x)
142 sign(x::Real) = ifelse(x < 0, oftype(one(x),-1), ifelse(x > 0, one(x), typeof(one(x))(x)))
143 sign(x::Unsigned) = ifelse(x > 0, one(x), oftype(one(x),0))
144 abs(x::Real) = ifelse(signbit(x), -x, x)
145
146 """
147     abs2(x)
148
149 Squared absolute value of `x`.
150
151 # Examples
152 ```jldoctest
153 julia> abs2(-3)
154 9
155 ```
156 """
157 abs2(x::Real) = x*x
158
159 """
160     flipsign(x, y)
161
162 Return `x` with its sign flipped if `y` is negative. For example `abs(x) = flipsign(x,x)`.
163
164 # Examples
165 ```jldoctest
166 julia> flipsign(5, 3)
167 5
168
169 julia> flipsign(5, -3)
170 -5
171 ```
172 """
173 flipsign(x::Real, y::Real) = ifelse(signbit(y), -x, +x) # the + is for type-stability on Bool
174
175 """
176     copysign(x, y) -> z
177
178 Return `z` which has the magnitude of `x` and the same sign as `y`.
179
180 # Examples
181 ```jldoctest
182 julia> copysign(1, -2)
183 -1
184
185 julia> copysign(-1, 2)
186 1
187 ```
188 """
189 copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, +x)
190
191 conj(x::Real) = x
192 transpose(x::Number) = x
194 angle(z::Real) = atan(zero(z), z)
195
196 """
197     inv(x)
198
199 Return the multiplicative inverse of `x`, such that `x*inv(x)` or `inv(x)*x`
200 yields [`one(x)`](@ref) (the multiplicative identity) up to roundoff errors.
201
202 If `x` is a number, this is essentially the same as `one(x)/x`, but for
203 some types `inv(x)` may be slightly more efficient.
204
205 # Examples
206 ```jldoctest
207 julia> inv(2)
208 0.5
209
210 julia> inv(1 + 2im)
211 0.2 - 0.4im
212
213 julia> inv(1 + 2im) * (1 + 2im)
214 1.0 + 0.0im
215
216 julia> inv(2//3)
217 3//2
218 ```
219 """
220 inv(x::Number) = one(x)/x
221
222
223 """
224     widemul(x, y)
225
226 Multiply `x` and `y`, giving the result as a larger type.
227
228 # Examples
229 ```jldoctest
230 julia> widemul(Float32(3.), 4.)
231 12.0
232 ```
233 """
234 widemul(x::Number, y::Number) = widen(x)*widen(y)
235
236 iterate(x::Number) = (x, nothing)
237 iterate(x::Number, ::Any) = nothing
238 isempty(x::Number) = false
239 in(x::Number, y::Number) = x == y
240
241 map(f, x::Number, ys::Number...) = f(x, ys...)
242
243 """
244     zero(x)
245
246 Get the additive identity element for the type of `x` (`x` can also specify the type itself).
247
248 # Examples
249 ```jldoctest
250 julia> zero(1)
251 0
252
253 julia> zero(big"2.0")
254 0.0
255
256 julia> zero(rand(2,2))
257 2×2 Array{Float64,2}:
258  0.0  0.0
259  0.0  0.0
260 ```
261 """
262 zero(x::Number) = oftype(x,0)
263 zero(::Type{T}) where {T<:Number} = convert(T,0)
264
265 """
266     one(x)
267     one(T::type)
268
269 Return a multiplicative identity for `x`: a value such that
270 `one(x)*x == x*one(x) == x`.  Alternatively `one(T)` can
271 take a type `T`, in which case `one` returns a multiplicative
272 identity for any `x` of type `T`.
273
274 If possible, `one(x)` returns a value of the same type as `x`,
275 and `one(T)` returns a value of type `T`.  However, this may
276 not be the case for types representing dimensionful quantities
277 (e.g. time in days), since the multiplicative
278 identity must be dimensionless.  In that case, `one(x)`
279 should return an identity value of the same precision
280 (and shape, for matrices) as `x`.
281
282 If you want a quantity that is of the same type as `x`, or of type `T`,
283 even if `x` is dimensionful, use [`oneunit`](@ref) instead.
284
285 # Examples
286 ```jldoctest
287 julia> one(3.7)
288 1.0
289
290 julia> one(Int)
291 1
292
293 julia> import Dates; one(Dates.Day(1))
294 1
295 ```
296 """
297 one(::Type{T}) where {T<:Number} = convert(T,1)
298 one(x::T) where {T<:Number} = one(T)
299 # note that convert(T, 1) should throw an error if T is dimensionful,
300 # so this fallback definition should be okay.
301
302 """
303     oneunit(x::T)
304     oneunit(T::Type)
305
306 Returns `T(one(x))`, where `T` is either the type of the argument or
307 (if a type is passed) the argument.  This differs from [`one`](@ref) for
308 dimensionful quantities: `one` is dimensionless (a multiplicative identity)
309 while `oneunit` is dimensionful (of the same type as `x`, or of type `T`).
310
311 # Examples
312 ```jldoctest
313 julia> oneunit(3.7)
314 1.0
315
316 julia> import Dates; oneunit(Dates.Day)
317 1 day
318 ```
319 """
320 oneunit(x::T) where {T} = T(one(x))
321 oneunit(::Type{T}) where {T} = T(one(T))
322
323 """
324     big(T::Type)
325
326 Compute the type that represents the numeric type `T` with arbitrary precision.
327 Equivalent to `typeof(big(zero(T)))`.
328
329 # Examples
330 ```jldoctest
331 julia> big(Rational)
332 Rational{BigInt}
333
334 julia> big(Float64)
335 BigFloat
336
337 julia> big(Complex{Int})
338 Complex{BigInt}
339 ```
340 """
341 big(::Type{T}) where {T<:Number} = typeof(big(zero(T)))
```