• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37497

pending completion
#37497

push

local

web-flow
<a href="https://github.com/JuliaLang/julia/commit/<a class=hub.com/JuliaLang/julia/commit/def2ddacc9a9b064d06c24d12885427fb0502465">def2ddacc<a href="https://github.com/JuliaLang/julia/commit/def2ddacc9a9b064d06c24d12885427fb0502465">&quot;&gt;make default worker pool an AbstractWorkerPool (#49101)

Changes [Distributed._default_worker_pool](https://github.com/JuliaLang/julia/blob/</a><a class="double-link" href="https://github.com/JuliaLang/julia/commit/<a class="double-link" href="https://github.com/JuliaLang/julia/commit/5f5d2040511b42ba74bd7529a0eac9cf817ad496">5f5d20405</a>">5f5d20405</a><a href="https://github.com/JuliaLang/julia/commit/def2ddacc9a9b064d06c24d12885427fb0502465">/stdlib/Distributed/src/workerpool.jl#L242) to hold an `AbstractWorkerPool` instead of `WorkerPool`. With this, alternate implementations can be plugged in as the default pool. Helps in cases where a cluster is always meant to use a certain custom pool. Lower level calls can then work without having to pass a custom pool reference with every call.

4 of 4 new or added lines in 2 files covered. (100.0%)

71044 of 82770 relevant lines covered (85.83%)

33857692.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.1
/base/float.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
const IEEEFloat = Union{Float16, Float32, Float64}
4

5
## floating point traits ##
6

7
"""
8
    Inf16
9

10
Positive infinity of type [`Float16`](@ref).
11
"""
12
const Inf16 = bitcast(Float16, 0x7c00)
13
"""
14
    NaN16
15

16
A not-a-number value of type [`Float16`](@ref).
17
"""
18
const NaN16 = bitcast(Float16, 0x7e00)
19
"""
20
    Inf32
21

22
Positive infinity of type [`Float32`](@ref).
23
"""
24
const Inf32 = bitcast(Float32, 0x7f800000)
25
"""
26
    NaN32
27

28
A not-a-number value of type [`Float32`](@ref).
29
"""
30
const NaN32 = bitcast(Float32, 0x7fc00000)
31
const Inf64 = bitcast(Float64, 0x7ff0000000000000)
32
const NaN64 = bitcast(Float64, 0x7ff8000000000000)
33

34
const Inf = Inf64
35
"""
36
    Inf, Inf64
37

38
Positive infinity of type [`Float64`](@ref).
39

40
See also: [`isfinite`](@ref), [`typemax`](@ref), [`NaN`](@ref), [`Inf32`](@ref).
41

42
# Examples
43
```jldoctest
44
julia> π/0
45
Inf
46

47
julia> +1.0 / -0.0
48
-Inf
49

50
julia> ℯ^-Inf
51
0.0
52
```
53
"""
54
Inf, Inf64
55

56
const NaN = NaN64
57
"""
58
    NaN, NaN64
59

60
A not-a-number value of type [`Float64`](@ref).
61

62
See also: [`isnan`](@ref), [`missing`](@ref), [`NaN32`](@ref), [`Inf`](@ref).
63

64
# Examples
65
```jldoctest
66
julia> 0/0
67
NaN
68

69
julia> Inf - Inf
70
NaN
71

72
julia> NaN == NaN, isequal(NaN, NaN), NaN === NaN
73
(false, true, true)
74
```
75
"""
76
NaN, NaN64
77

78
# bit patterns
79
reinterpret(::Type{Unsigned}, x::Float64) = reinterpret(UInt64, x)
11,179,057✔
80
reinterpret(::Type{Unsigned}, x::Float32) = reinterpret(UInt32, x)
605,834,663✔
81
reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16, x)
3,795,874✔
82
reinterpret(::Type{Signed}, x::Float64) = reinterpret(Int64, x)
600,259,604✔
83
reinterpret(::Type{Signed}, x::Float32) = reinterpret(Int32, x)
600,702,767✔
84
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16, x)
675,650✔
85

86
sign_mask(::Type{Float64}) =        0x8000_0000_0000_0000
×
87
exponent_mask(::Type{Float64}) =    0x7ff0_0000_0000_0000
×
88
exponent_one(::Type{Float64}) =     0x3ff0_0000_0000_0000
×
89
exponent_half(::Type{Float64}) =    0x3fe0_0000_0000_0000
90✔
90
significand_mask(::Type{Float64}) = 0x000f_ffff_ffff_ffff
×
91

92
sign_mask(::Type{Float32}) =        0x8000_0000
3,835,734✔
93
exponent_mask(::Type{Float32}) =    0x7f80_0000
×
94
exponent_one(::Type{Float32}) =     0x3f80_0000
×
95
exponent_half(::Type{Float32}) =    0x3f00_0000
1,000,025✔
96
significand_mask(::Type{Float32}) = 0x007f_ffff
×
97

98
sign_mask(::Type{Float16}) =        0x8000
1,679,633✔
99
exponent_mask(::Type{Float16}) =    0x7c00
×
100
exponent_one(::Type{Float16}) =     0x3c00
×
101
exponent_half(::Type{Float16}) =    0x3800
999,210✔
102
significand_mask(::Type{Float16}) = 0x03ff
×
103

104
mantissa(x::T) where {T} = reinterpret(Unsigned, x) & significand_mask(T)
1,032,958✔
105

106
for T in (Float16, Float32, Float64)
107
    @eval significand_bits(::Type{$T}) = $(trailing_ones(significand_mask(T)))
×
108
    @eval exponent_bits(::Type{$T}) = $(sizeof(T)*8 - significand_bits(T) - 1)
1,273✔
109
    @eval exponent_bias(::Type{$T}) = $(Int(exponent_one(T) >> significand_bits(T)))
×
110
    # maximum float exponent
111
    @eval exponent_max(::Type{$T}) = $(Int(exponent_mask(T) >> significand_bits(T)) - exponent_bias(T) - 1)
×
112
    # maximum float exponent without bias
113
    @eval exponent_raw_max(::Type{$T}) = $(Int(exponent_mask(T) >> significand_bits(T)))
2,488,792✔
114
end
115

116
"""
117
    exponent_max(T)
118

119
Maximum [`exponent`](@ref) value for a floating point number of type `T`.
120

121
# Examples
122
```jldoctest
123
julia> Base.exponent_max(Float64)
124
1023
125
```
126

127
Note, `exponent_max(T) + 1` is a possible value of the exponent field
128
with bias, which might be used as sentinel value for `Inf` or `NaN`.
129
"""
130
function exponent_max end
131

132
"""
133
    exponent_raw_max(T)
134

135
Maximum value of the [`exponent`](@ref) field for a floating point number of type `T` without bias,
136
i.e. the maximum integer value representable by [`exponent_bits(T)`](@ref) bits.
137
"""
138
function exponent_raw_max end
139

140
"""
141
    uabs(x::Integer)
142

143
Return the absolute value of `x`, possibly returning a different type should the
144
operation be susceptible to overflow. This typically arises when `x` is a two's complement
145
signed integer, so that `abs(typemin(x)) == typemin(x) < 0`, in which case the result of
146
`uabs(x)` will be an unsigned integer of the same size.
147
"""
148
uabs(x::Integer) = abs(x)
5✔
149
uabs(x::BitSigned) = unsigned(abs(x))
4,210,304✔
150

151
## conversions to floating-point ##
152

153
# TODO: deprecate in 2.0
154
Float16(x::Integer) = convert(Float16, convert(Float32, x)::Float32)
×
155

156
for t1 in (Float16, Float32, Float64)
157
    for st in (Int8, Int16, Int32, Int64)
158
        @eval begin
159
            (::Type{$t1})(x::($st)) = sitofp($t1, x)
260,516,655✔
160
            promote_rule(::Type{$t1}, ::Type{$st}) = $t1
39,592,985✔
161
        end
162
    end
163
    for ut in (Bool, UInt8, UInt16, UInt32, UInt64)
164
        @eval begin
165
            (::Type{$t1})(x::($ut)) = uitofp($t1, x)
85,919,937✔
166
            promote_rule(::Type{$t1}, ::Type{$ut}) = $t1
1,652,745✔
167
        end
168
    end
169
end
170

171
Bool(x::Real) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x))
16,396,809✔
172

173
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
46✔
174
promote_rule(::Type{Float64}, ::Type{Int128}) = Float64
693,607✔
175
promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32
23✔
176
promote_rule(::Type{Float32}, ::Type{Int128}) = Float32
23✔
177
promote_rule(::Type{Float16}, ::Type{UInt128}) = Float16
23✔
178
promote_rule(::Type{Float16}, ::Type{Int128}) = Float16
23✔
179

180
function Float64(x::UInt128)
21,180✔
181
    if x < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
21,202✔
182
        low_exp = 0x1p52
×
183
        high_exp = 0x1p104
×
184
        low_bits = (x % UInt64) & Base.significand_mask(Float64)
175✔
185
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
175✔
186
        high_bits = ((x >> 52) % UInt64)
175✔
187
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
175✔
188
        low_value + high_value
191✔
189
    else # Large enough that low bits only affect rounding, pack low bits
190
        low_exp = 0x1p76
×
191
        high_exp = 0x1p128
×
192
        low_bits = ((x >> 12) % UInt64) >> 12 | (x % UInt64) & 0xFFFFFF
21,011✔
193
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
21,011✔
194
        high_bits = ((x >> 76) % UInt64)
21,011✔
195
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
21,011✔
196
        low_value + high_value
21,011✔
197
    end
198
end
199

200
function Float64(x::Int128)
3,956,103✔
201
    sign_bit = ((x >> 127) % UInt64) << 63
3,304,843✔
202
    ux = uabs(x)
3,956,127✔
203
    if ux < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
3,956,127✔
204
        low_exp = 0x1p52
×
205
        high_exp = 0x1p104
×
206
        low_bits = (ux % UInt64) & Base.significand_mask(Float64)
3,284,814✔
207
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
3,284,814✔
208
        high_bits = ((ux >> 52) % UInt64)
3,284,814✔
209
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
3,284,814✔
210
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
3,936,098✔
211
    else # Large enough that low bits only affect rounding, pack low bits
212
        low_exp = 0x1p76
×
213
        high_exp = 0x1p128
×
214
        low_bits = ((ux >> 12) % UInt64) >> 12 | (ux % UInt64) & 0xFFFFFF
20,029✔
215
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
20,029✔
216
        high_bits = ((ux >> 76) % UInt64)
20,029✔
217
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
20,029✔
218
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
20,029✔
219
    end
220
end
221

222
function Float32(x::UInt128)
28✔
223
    x == 0 && return 0f0
28✔
224
    n = top_set_bit(x) # ndigits0z(x,2)
27✔
225
    if n <= 24
27✔
226
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
25✔
227
    else
228
        y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit
2✔
229
        y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent)
2✔
230
        y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even
2✔
231
    end
232
    d = ((n+126) % UInt32) << 23
27✔
233
    reinterpret(Float32, d + y)
27✔
234
end
235

236
function Float32(x::Int128)
30✔
237
    x == 0 && return 0f0
30✔
238
    s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit
29✔
239
    x = abs(x) % UInt128
29✔
240
    n = top_set_bit(x) # ndigits0z(x,2)
29✔
241
    if n <= 24
29✔
242
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
26✔
243
    else
244
        y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit
3✔
245
        y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent)
3✔
246
        y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even
3✔
247
    end
248
    d = ((n+126) % UInt32) << 23
29✔
249
    reinterpret(Float32, s | d + y)
29✔
250
end
251

252
# TODO: optimize
253
Float16(x::UInt128) = convert(Float16, Float64(x))
22✔
254
Float16(x::Int128)  = convert(Float16, Float64(x))
26✔
255

256
Float16(x::Float32) = fptrunc(Float16, x)
6,223,918✔
257
Float16(x::Float64) = fptrunc(Float16, x)
280,425✔
258
Float32(x::Float64) = fptrunc(Float32, x)
457,567,945✔
259

260
Float32(x::Float16) = fpext(Float32, x)
27,498,635✔
261
Float64(x::Float32) = fpext(Float64, x)
473,202,841✔
262
Float64(x::Float16) = fpext(Float64, x)
3,072,242✔
263

264
AbstractFloat(x::Bool)    = Float64(x)
448,143✔
265
AbstractFloat(x::Int8)    = Float64(x)
84✔
266
AbstractFloat(x::Int16)   = Float64(x)
59✔
267
AbstractFloat(x::Int32)   = Float64(x)
63,357✔
268
AbstractFloat(x::Int64)   = Float64(x) # LOSSY
20,440,273✔
269
AbstractFloat(x::Int128)  = Float64(x) # LOSSY
1,308,572✔
270
AbstractFloat(x::UInt8)   = Float64(x)
12,290✔
271
AbstractFloat(x::UInt16)  = Float64(x)
45✔
272
AbstractFloat(x::UInt32)  = Float64(x)
45✔
273
AbstractFloat(x::UInt64)  = Float64(x) # LOSSY
1,572✔
274
AbstractFloat(x::UInt128) = Float64(x) # LOSSY
2,058✔
275

276
Bool(x::Float16) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x))
5✔
277

278
"""
279
    float(x)
280

281
Convert a number or array to a floating point data type.
282

283
See also: [`complex`](@ref), [`oftype`](@ref), [`convert`](@ref).
284

285
# Examples
286
```jldoctest
287
julia> float(1:1000)
288
1.0:1.0:1000.0
289

290
julia> float(typemax(Int32))
291
2.147483647e9
292
```
293
"""
294
float(x) = AbstractFloat(x)
40,180,148✔
295

296
"""
297
    float(T::Type)
298

299
Return an appropriate type to represent a value of type `T` as a floating point value.
300
Equivalent to `typeof(float(zero(T)))`.
301

302
# Examples
303
```jldoctest
304
julia> float(Complex{Int})
305
ComplexF64 (alias for Complex{Float64})
306

307
julia> float(Int)
308
Float64
309
```
310
"""
311
float(::Type{T}) where {T<:Number} = typeof(float(zero(T)))
3,924✔
312
float(::Type{T}) where {T<:AbstractFloat} = T
23,505✔
313

314
"""
315
    unsafe_trunc(T, x)
316

317
Return the nearest integral value of type `T` whose absolute value is
318
less than or equal to the absolute value of `x`. If the value is not representable by `T`,
319
an arbitrary value will be returned.
320
See also [`trunc`](@ref).
321

322
# Examples
323
```jldoctest
324
julia> unsafe_trunc(Int, -2.2)
325
-2
326

327
julia> unsafe_trunc(Int, NaN)
328
-9223372036854775808
329
```
330
"""
331
function unsafe_trunc end
332

333
for Ti in (Int8, Int16, Int32, Int64)
334
    @eval begin
335
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptosi($Ti, x)
45,752,902✔
336
    end
337
end
338
for Ti in (UInt8, UInt16, UInt32, UInt64)
339
    @eval begin
340
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptoui($Ti, x)
40,745,755✔
341
    end
342
end
343

344
function unsafe_trunc(::Type{UInt128}, x::Float64)
652,411✔
345
    xu = reinterpret(UInt64,x)
652,411✔
346
    k = Int(xu >> 52) & 0x07ff - 1075
652,411✔
347
    xu = (xu & 0x000f_ffff_ffff_ffff) | 0x0010_0000_0000_0000
652,411✔
348
    if k <= 0
652,411✔
349
        UInt128(xu >> -k)
651,395✔
350
    else
351
        UInt128(xu) << k
1,016✔
352
    end
353
end
354
function unsafe_trunc(::Type{Int128}, x::Float64)
651,379✔
355
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
651,388✔
356
end
357

358
function unsafe_trunc(::Type{UInt128}, x::Float32)
9✔
359
    xu = reinterpret(UInt32,x)
9✔
360
    k = Int(xu >> 23) & 0x00ff - 150
9✔
361
    xu = (xu & 0x007f_ffff) | 0x0080_0000
9✔
362
    if k <= 0
9✔
363
        UInt128(xu >> -k)
9✔
364
    else
365
        UInt128(xu) << k
×
366
    end
367
end
368
function unsafe_trunc(::Type{Int128}, x::Float32)
5✔
369
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
5✔
370
end
371

372
unsafe_trunc(::Type{UInt128}, x::Float16) = unsafe_trunc(UInt128, Float32(x))
4✔
373
unsafe_trunc(::Type{Int128}, x::Float16) = unsafe_trunc(Int128, Float32(x))
5✔
374

375
# matches convert methods
376
# also determines floor, ceil, round
377
trunc(::Type{Signed}, x::IEEEFloat) = trunc(Int,x)
×
378
trunc(::Type{Unsigned}, x::IEEEFloat) = trunc(UInt,x)
×
379
trunc(::Type{Integer}, x::IEEEFloat) = trunc(Int,x)
394✔
380

381
# fallbacks
382
floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))
78,620✔
383
ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
698,073✔
384
round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))
10,325,342✔
385

386
# Bool
387
trunc(::Type{Bool}, x::AbstractFloat) = (-1 < x < 2) ? 1 <= x : throw(InexactError(:trunc, Bool, x))
8✔
388
floor(::Type{Bool}, x::AbstractFloat) = (0 <= x < 2) ? 1 <= x : throw(InexactError(:floor, Bool, x))
6✔
389
ceil(::Type{Bool}, x::AbstractFloat)  = (-1 < x <= 1) ? 0 < x : throw(InexactError(:ceil, Bool, x))
6✔
390
round(::Type{Bool}, x::AbstractFloat) = (-0.5 <= x < 1.5) ? 0.5 < x : throw(InexactError(:round, Bool, x))
8✔
391

392
round(x::IEEEFloat, r::RoundingMode{:ToZero})  = trunc_llvm(x)
24,079,157✔
393
round(x::IEEEFloat, r::RoundingMode{:Down})    = floor_llvm(x)
165,127✔
394
round(x::IEEEFloat, r::RoundingMode{:Up})      = ceil_llvm(x)
738,672✔
395
round(x::IEEEFloat, r::RoundingMode{:Nearest}) = rint_llvm(x)
11,037,700✔
396

397
## floating point promotions ##
398
promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
11,260,129✔
399
promote_rule(::Type{Float64}, ::Type{Float16}) = Float64
×
400
promote_rule(::Type{Float64}, ::Type{Float32}) = Float64
×
401

402
widen(::Type{Float16}) = Float32
11,246,627✔
403
widen(::Type{Float32}) = Float64
9,336,516✔
404

405
## floating point arithmetic ##
406
-(x::IEEEFloat) = neg_float(x)
412,878,373✔
407

408
+(x::T, y::T) where {T<:IEEEFloat} = add_float(x, y)
655,594,567✔
409
-(x::T, y::T) where {T<:IEEEFloat} = sub_float(x, y)
1,241,985,239✔
410
*(x::T, y::T) where {T<:IEEEFloat} = mul_float(x, y)
4,158,508,795✔
411
/(x::T, y::T) where {T<:IEEEFloat} = div_float(x, y)
890,817,133✔
412

413
muladd(x::T, y::T, z::T) where {T<:IEEEFloat} = muladd_float(x, y, z)
819,255,898✔
414

415
# TODO: faster floating point div?
416
# TODO: faster floating point fld?
417
# TODO: faster floating point mod?
418

419
function unbiased_exponent(x::T) where {T<:IEEEFloat}
1,498✔
420
    return (reinterpret(Unsigned, x) & exponent_mask(T)) >> significand_bits(T)
1,032,934✔
421
end
422

423
function explicit_mantissa_noinfnan(x::T) where {T<:IEEEFloat}
1,498✔
424
    m = mantissa(x)
1,032,934✔
425
    issubnormal(x) || (m |= significand_mask(T) + uinttype(T)(1))
2,065,844✔
426
    return m
1,032,934✔
427
end
428

429
function _to_float(number::U, ep) where {U<:Unsigned}
368✔
430
    F = floattype(U)
368✔
431
    S = signed(U)
368✔
432
    epint = unsafe_trunc(S,ep)
500,399✔
433
    lz::signed(U) = unsafe_trunc(S, Core.Intrinsics.ctlz_int(number) - U(exponent_bits(F)))
500,399✔
434
    number <<= lz
500,399✔
435
    epint -= lz
500,399✔
436
    bits = U(0)
368✔
437
    if epint >= 0
500,399✔
438
        bits = number & significand_mask(F)
500,383✔
439
        bits |= ((epint + S(1)) << significand_bits(F)) & exponent_mask(F)
500,383✔
440
    else
441
        bits = (number >> -epint) & significand_mask(F)
16✔
442
    end
443
    return reinterpret(F, bits)
500,399✔
444
end
445

446
@assume_effects :terminates_locally :nothrow function rem_internal(x::T, y::T) where {T<:IEEEFloat}
590,614✔
447
    xuint = reinterpret(Unsigned, x)
590,614✔
448
    yuint = reinterpret(Unsigned, y)
590,614✔
449
    if xuint <= yuint
590,614✔
450
        if xuint < yuint
74,147✔
451
            return x
68,457✔
452
        end
453
        return zero(T)
5,690✔
454
    end
455

456
    e_x = unbiased_exponent(x)
516,467✔
457
    e_y = unbiased_exponent(y)
516,467✔
458
    # Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
459
    if e_y > (significand_bits(T)) && (e_x - e_y) <= (exponent_bits(T))
516,467✔
460
        m_x = explicit_mantissa_noinfnan(x)
250,752✔
461
        m_y = explicit_mantissa_noinfnan(y)
250,752✔
462
        d = urem_int((m_x << (e_x - e_y)),  m_y)
125,376✔
463
        iszero(d) && return zero(T)
125,376✔
464
        return _to_float(d, e_y - uinttype(T)(1))
109,593✔
465
    end
466
    # Both are subnormals
467
    if e_x == 0 && e_y == 0
391,091✔
468
        return reinterpret(T, urem_int(xuint, yuint) & significand_mask(T))
×
469
    end
470

471
    m_x = explicit_mantissa_noinfnan(x)
782,182✔
472
    e_x -= uinttype(T)(1)
391,091✔
473
    m_y = explicit_mantissa_noinfnan(y)
782,158✔
474
    lz_m_y = uinttype(T)(exponent_bits(T))
44✔
475
    if e_y > 0
391,091✔
476
        e_y -= uinttype(T)(1)
391,067✔
477
    else
478
        m_y = mantissa(y)
24✔
479
        lz_m_y = Core.Intrinsics.ctlz_int(m_y)
24✔
480
    end
481

482
    tz_m_y = Core.Intrinsics.cttz_int(m_y)
391,091✔
483
    sides_zeroes_cnt = lz_m_y + tz_m_y
391,091✔
484

485
    # n>0
486
    exp_diff = e_x - e_y
391,091✔
487
    # Shift hy right until the end or n = 0
488
    right_shift = min(exp_diff, tz_m_y)
391,091✔
489
    m_y >>= right_shift
391,091✔
490
    exp_diff -= right_shift
391,091✔
491
    e_y += right_shift
391,091✔
492
    # Shift hx left until the end or n = 0
493
    left_shift = min(exp_diff, uinttype(T)(exponent_bits(T)))
391,091✔
494
    m_x <<= left_shift
391,091✔
495
    exp_diff -= left_shift
391,091✔
496

497
    m_x = urem_int(m_x, m_y)
391,091✔
498
    iszero(m_x) && return zero(T)
391,091✔
499
    iszero(exp_diff) && return _to_float(m_x, e_y)
390,806✔
500

501
    while exp_diff > sides_zeroes_cnt
378,582✔
502
        exp_diff -= sides_zeroes_cnt
1,077✔
503
        m_x <<= sides_zeroes_cnt
1,077✔
504
        m_x = urem_int(m_x, m_y)
1,077✔
505
    end
1,077✔
506
    m_x <<= exp_diff
377,505✔
507
    m_x = urem_int(m_x, m_y)
377,505✔
508
    return _to_float(m_x, e_y)
377,505✔
509
end
510

511
function rem(x::T, y::T) where {T<:IEEEFloat}
22,382✔
512
    if isfinite(x) && !iszero(x) && isfinite(y) && !iszero(y)
599,497✔
513
        return copysign(rem_internal(abs(x), abs(y)), x)
590,624✔
514
    elseif isinf(x) || isnan(y) || iszero(y)  # y can still be Inf
17,715✔
515
        return T(NaN)
73✔
516
    else
517
        return x
8,800✔
518
    end
519
end
520

521
function mod(x::T, y::T) where {T<:AbstractFloat}
54,650✔
522
    r = rem(x,y)
97,699✔
523
    if r == 0
93,247✔
524
        copysign(r,y)
16,378✔
525
    elseif (r > 0) ⊻ (y > 0)
76,869✔
526
        r+y
28,263✔
527
    else
528
        r
48,606✔
529
    end
530
end
531

532
## floating point comparisons ##
533
==(x::T, y::T) where {T<:IEEEFloat} = eq_float(x, y)
437,092,261✔
534
!=(x::T, y::T) where {T<:IEEEFloat} = ne_float(x, y)
2,306,148,180✔
535
<( x::T, y::T) where {T<:IEEEFloat} = lt_float(x, y)
167,822,043✔
536
<=(x::T, y::T) where {T<:IEEEFloat} = le_float(x, y)
141,552,337✔
537

538
isequal(x::T, y::T) where {T<:IEEEFloat} = fpiseq(x, y)
4,751,191✔
539

540
# interpret as sign-magnitude integer
541
@inline function _fpint(x)
9,504✔
542
    IntT = inttype(typeof(x))
9,482✔
543
    ix = reinterpret(IntT, x)
5,243,799✔
544
    return ifelse(ix < zero(IntT), ix ⊻ typemax(IntT), ix)
5,243,799✔
545
end
546

547
@inline function isless(a::T, b::T) where T<:IEEEFloat
92,225✔
548
    (isnan(a) || isnan(b)) && return !isnan(a)
5,473,244✔
549

550
    return _fpint(a) < _fpint(b)
2,736,414✔
551
end
552

553
# Exact Float (Tf) vs Integer (Ti) comparisons
554
# Assumes:
555
# - typemax(Ti) == 2^n-1
556
# - typemax(Ti) can't be exactly represented by Tf:
557
#   => Tf(typemax(Ti)) == 2^n or Inf
558
# - typemin(Ti) can be exactly represented by Tf
559
#
560
# 1. convert y::Ti to float fy::Tf
561
# 2. perform Tf comparison x vs fy
562
# 3. if x == fy, check if (1) resulted in rounding:
563
#  a. convert fy back to Ti and compare with original y
564
#  b. unsafe_convert undefined behaviour if fy == Tf(typemax(Ti))
565
#     (but consequently x == fy > y)
566
for Ti in (Int64,UInt64,Int128,UInt128)
567
    for Tf in (Float32,Float64)
568
        @eval begin
569
            function ==(x::$Tf, y::$Ti)
5,216,483✔
570
                fy = ($Tf)(y)
5,714,853✔
571
                (x == fy) & (fy != $(Tf(typemax(Ti)))) & (y == unsafe_trunc($Ti,fy))
6,282,403✔
572
            end
573
            ==(y::$Ti, x::$Tf) = x==y
258,682✔
574

575
            function <(x::$Ti, y::$Tf)
40,634,501✔
576
                fx = ($Tf)(x)
40,636,569✔
577
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
40,789,333✔
578
            end
579
            function <=(x::$Ti, y::$Tf)
20,440✔
580
                fx = ($Tf)(x)
211,753✔
581
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
749,822✔
582
            end
583

584
            function <(x::$Tf, y::$Ti)
632,108✔
585
                fy = ($Tf)(y)
632,142✔
586
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
830,761✔
587
            end
588
            function <=(x::$Tf, y::$Ti)
22,394✔
589
                fy = ($Tf)(y)
22,394✔
590
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
23,031✔
591
            end
592
        end
593
    end
594
end
595
for op in (:(==), :<, :<=)
596
    @eval begin
597
        ($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
2,162,846✔
598
        ($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
252✔
599

600
        ($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
246,181✔
601
        ($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))
79✔
602

603
        ($op)(x::Float16, y::Union{Int16,UInt16}) = ($op)(Float32(x), Float32(y))
10✔
604
        ($op)(x::Union{Int16,UInt16}, y::Float16) = ($op)(Float32(x), Float32(y))
12✔
605
    end
606
end
607

608

609
abs(x::IEEEFloat) = abs_float(x)
184,584,868✔
610

611
"""
612
    isnan(f) -> Bool
613

614
Test whether a number value is a NaN, an indeterminate value which is neither an infinity
615
nor a finite number ("not a number").
616

617
See also: [`iszero`](@ref), [`isone`](@ref), [`isinf`](@ref), [`ismissing`](@ref).
618
"""
619
isnan(x::AbstractFloat) = (x != x)::Bool
2,303,312,757✔
620
isnan(x::Number) = false
88,347✔
621

622
isfinite(x::AbstractFloat) = !isnan(x - x)
1,018,140,676✔
623
isfinite(x::Real) = decompose(x)[3] != 0
71,416✔
624
isfinite(x::Integer) = true
11,584✔
625

626
"""
627
    isinf(f) -> Bool
628

629
Test whether a number is infinite.
630

631
See also: [`Inf`](@ref), [`iszero`](@ref), [`isfinite`](@ref), [`isnan`](@ref).
632
"""
633
isinf(x::Real) = !isnan(x) & !isfinite(x)
67,958✔
634
isinf(x::IEEEFloat) = abs(x) === oftype(x, Inf)
36,709,901✔
635

636
const hx_NaN = hash_uint64(reinterpret(UInt64, NaN))
637
let Tf = Float64, Tu = UInt64, Ti = Int64
638
    @eval function hash(x::$Tf, h::UInt)
81,198✔
639
        # see comments on trunc and hash(Real, UInt)
640
        if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
81,198✔
641
            xi = fptosi($Ti, x)
81,048✔
642
            if isequal(xi, x)
81,048✔
643
                return hash(xi, h)
27,296✔
644
            end
645
        elseif $(Tf(typemin(Tu))) <= x < $(Tf(typemax(Tu)))
150✔
646
            xu = fptoui($Tu, x)
82✔
647
            if isequal(xu, x)
82✔
648
                return hash(xu, h)
82✔
649
            end
650
        elseif isnan(x)
68✔
651
            return hx_NaN ⊻ h # NaN does not have a stable bit pattern
61✔
652
        end
653
        return hash_uint64(bitcast(UInt64, x)) - 3h
53,759✔
654
    end
655
end
656

657
hash(x::Float32, h::UInt) = hash(Float64(x), h)
3,955✔
658
hash(x::Float16, h::UInt) = hash(Float64(x), h)
6✔
659

660
## generic hashing for rational values ##
661

662
function hash(x::Real, h::UInt)
5,203✔
663
    # decompose x as num*2^pow/den
664
    num, pow, den = decompose(x)
1,324✔
665

666
    # handle special values
667
    num == 0 && den == 0 && return hash(NaN, h)
5,203✔
668
    num == 0 && return hash(ifelse(den > 0, 0.0, -0.0), h)
5,203✔
669
    den == 0 && return hash(ifelse(num > 0, Inf, -Inf), h)
1,036✔
670

671
    # normalize decomposition
672
    if den < 0
1,036✔
673
        num = -num
64✔
674
        den = -den
64✔
675
    end
676
    z = trailing_zeros(num)
1,039✔
677
    if z != 0
1,039✔
678
        num >>= z
510✔
679
        pow += z
508✔
680
    end
681
    z = trailing_zeros(den)
1,039✔
682
    if z != 0
1,036✔
683
        den >>= z
3✔
684
        pow -= z
2✔
685
    end
686

687
    # handle values representable as Int64, UInt64, Float64
688
    if den == 1
1,039✔
689
        left = ndigits0z(num,2) + pow
1,037✔
690
        right = trailing_zeros(num) + pow
1,037✔
691
        if -1074 <= right
1,037✔
692
            if 0 <= right && left <= 64
1,037✔
693
                left <= 63                     && return hash(Int64(num) << Int(pow), h)
1,036✔
694
                signbit(num) == signbit(den)   && return hash(UInt64(num) << Int(pow), h)
77✔
695
            end # typemin(Int64) handled by Float64 case
696
            left <= 1024 && left - right <= 53 && return hash(ldexp(Float64(num),pow), h)
10✔
697
        end
698
    end
699

700
    # handle generic rational values
701
    h = hash_integer(den, h)
2✔
702
    h = hash_integer(pow, h)
2✔
703
    h = hash_integer(num, h)
2✔
704
    return h
2✔
705
end
706

707
#=
708
`decompose(x)`: non-canonical decomposition of rational values as `num*2^pow/den`.
709

710
The decompose function is the point where rational-valued numeric types that support
711
hashing hook into the hashing protocol. `decompose(x)` should return three integer
712
values `num, pow, den`, such that the value of `x` is mathematically equal to
713

714
    num*2^pow/den
715

716
The decomposition need not be canonical in the sense that it just needs to be *some*
717
way to express `x` in this form, not any particular way – with the restriction that
718
`num` and `den` may not share any odd common factors. They may, however, have powers
719
of two in common – the generic hashing code will normalize those as necessary.
720

721
Special values:
722

723
 - `x` is zero: `num` should be zero and `den` should have the same sign as `x`
724
 - `x` is infinite: `den` should be zero and `num` should have the same sign as `x`
725
 - `x` is not a number: `num` and `den` should both be zero
726
=#
727

728
decompose(x::Integer) = x, 0, 1
528✔
729

730
function decompose(x::Float16)::NTuple{3,Int}
×
731
    isnan(x) && return 0, 0, 0
×
732
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
×
733
    n = reinterpret(UInt16, x)
×
734
    s = (n & 0x03ff) % Int16
×
735
    e = ((n & 0x7c00) >> 10) % Int
×
736
    s |= Int16(e != 0) << 10
×
737
    d = ifelse(signbit(x), -1, 1)
×
738
    s, e - 25 + (e == 0), d
×
739
end
740

741
function decompose(x::Float32)::NTuple{3,Int}
74✔
742
    isnan(x) && return 0, 0, 0
74✔
743
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
74✔
744
    n = reinterpret(UInt32, x)
66✔
745
    s = (n & 0x007fffff) % Int32
66✔
746
    e = ((n & 0x7f800000) >> 23) % Int
66✔
747
    s |= Int32(e != 0) << 23
66✔
748
    d = ifelse(signbit(x), -1, 1)
66✔
749
    s, e - 150 + (e == 0), d
66✔
750
end
751

752
function decompose(x::Float64)::Tuple{Int64, Int, Int}
18,730✔
753
    isnan(x) && return 0, 0, 0
18,730✔
754
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
18,730✔
755
    n = reinterpret(UInt64, x)
18,723✔
756
    s = (n & 0x000fffffffffffff) % Int64
18,723✔
757
    e = ((n & 0x7ff0000000000000) >> 52) % Int
18,723✔
758
    s |= Int64(e != 0) << 52
18,723✔
759
    d = ifelse(signbit(x), -1, 1)
18,723✔
760
    s, e - 1075 + (e == 0), d
18,723✔
761
end
762

763

764
"""
765
    precision(num::AbstractFloat; base::Integer=2)
766
    precision(T::Type; base::Integer=2)
767

768
Get the precision of a floating point number, as defined by the effective number of bits in
769
the significand, or the precision of a floating-point type `T` (its current default, if
770
`T` is a variable-precision type like [`BigFloat`](@ref)).
771

772
If `base` is specified, then it returns the maximum corresponding
773
number of significand digits in that base.
774

775
!!! compat "Julia 1.8"
776
    The `base` keyword requires at least Julia 1.8.
777
"""
778
function precision end
779

780
_precision(::Type{Float16}) = 11
×
781
_precision(::Type{Float32}) = 24
×
782
_precision(::Type{Float64}) = 53
×
783
function _precision(x, base::Integer=2)
83,718✔
784
    base > 1 || throw(DomainError(base, "`base` cannot be less than 2."))
83,722✔
785
    p = _precision(x)
134,174✔
786
    return base == 2 ? Int(p) : floor(Int, p / log2(base))
83,723✔
787
end
788
precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, base)
100,998✔
789
precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base)
134✔
790

791

792
"""
793
    nextfloat(x::AbstractFloat, n::Integer)
794

795
The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n`
796
applications of [`prevfloat`](@ref) if `n < 0`.
797
"""
798
function nextfloat(f::IEEEFloat, d::Integer)
601,509,656✔
799
    F = typeof(f)
601,378,369✔
800
    fumax = reinterpret(Unsigned, F(Inf))
601,378,369✔
801
    U = typeof(fumax)
601,378,369✔
802

803
    isnan(f) && return f
1,201,637,802✔
804
    fi = reinterpret(Signed, f)
1,201,637,800✔
805
    fneg = fi < 0
1,201,637,800✔
806
    fu = unsigned(fi & typemax(fi))
1,201,637,800✔
807

808
    dneg = d < 0
601,378,424✔
809
    da = uabs(d)
601,378,425✔
810
    if da > typemax(U)
1,201,637,800✔
811
        fneg = dneg
4✔
812
        fu = fumax
4✔
813
    else
814
        du = da % U
601,378,364✔
815
        if fneg ⊻ dneg
1,201,637,796✔
816
            if du > fu
891,442✔
817
                fu = min(fumax, du - fu)
56✔
818
                fneg = !fneg
56✔
819
            else
820
                fu = fu - du
1,525,122✔
821
            end
822
        else
823
            if fumax - fu < du
1,200,746,354✔
824
                fu = fumax
9✔
825
            else
826
                fu = fu + du
1,200,746,345✔
827
            end
828
        end
829
    end
830
    if fneg
1,201,637,800✔
831
        fu |= sign_mask(F)
663,717✔
832
    end
833
    reinterpret(F, fu)
1,201,637,800✔
834
end
835

836
"""
837
    nextfloat(x::AbstractFloat)
838

839
Return the smallest floating point number `y` of the same type as `x` such `x < y`. If no
840
such `y` exists (e.g. if `x` is `Inf` or `NaN`), then return `x`.
841

842
See also: [`prevfloat`](@ref), [`eps`](@ref), [`issubnormal`](@ref).
843
"""
844
nextfloat(x::AbstractFloat) = nextfloat(x,1)
1,200,995,776✔
845

846
"""
847
    prevfloat(x::AbstractFloat, n::Integer)
848

849
The result of `n` iterative applications of `prevfloat` to `x` if `n >= 0`, or `-n`
850
applications of [`nextfloat`](@ref) if `n < 0`.
851
"""
852
prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d)
9✔
853

854
"""
855
    prevfloat(x::AbstractFloat)
856

857
Return the largest floating point number `y` of the same type as `x` such `y < x`. If no
858
such `y` exists (e.g. if `x` is `-Inf` or `NaN`), then return `x`.
859
"""
860
prevfloat(x::AbstractFloat) = nextfloat(x,-1)
1,087,782✔
861

862
for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
863
    for Tf in (Float16, Float32, Float64)
864
        if Ti <: Unsigned || sizeof(Ti) < sizeof(Tf)
865
            # Here `Tf(typemin(Ti))-1` is exact, so we can compare the lower-bound
866
            # directly. `Tf(typemax(Ti))+1` is either always exactly representable, or
867
            # rounded to `Inf` (e.g. when `Ti==UInt128 && Tf==Float32`).
868
            @eval begin
869
                function trunc(::Type{$Ti},x::$Tf)
9,626✔
870
                    if $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf))
466,971✔
871
                        return unsafe_trunc($Ti,x)
466,955✔
872
                    else
873
                        throw(InexactError(:trunc, $Ti, x))
16✔
874
                    end
875
                end
876
                function (::Type{$Ti})(x::$Tf)
2,538✔
877
                    if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && (round(x, RoundToZero) == x)
2,552✔
878
                        return unsafe_trunc($Ti,x)
3,529✔
879
                    else
880
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
24✔
881
                    end
882
                end
883
            end
884
        else
885
            # Here `eps(Tf(typemin(Ti))) > 1`, so the only value which can be truncated to
886
            # `Tf(typemin(Ti)` is itself. Similarly, `Tf(typemax(Ti))` is inexact and will
887
            # be rounded up. This assumes that `Tf(typemin(Ti)) > -Inf`, which is true for
888
            # these types, but not for `Float16` or larger integer types.
889
            @eval begin
890
                function trunc(::Type{$Ti},x::$Tf)
24,229,273✔
891
                    if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
32,711,067✔
892
                        return unsafe_trunc($Ti,x)
32,711,059✔
893
                    else
894
                        throw(InexactError(:trunc, $Ti, x))
8✔
895
                    end
896
                end
897
                function (::Type{$Ti})(x::$Tf)
23,188,275✔
898
                    if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) && (round(x, RoundToZero) == x)
23,916,704✔
899
                        return unsafe_trunc($Ti,x)
23,916,617✔
900
                    else
901
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
76✔
902
                    end
903
                end
904
            end
905
        end
906
    end
907
end
908

909
"""
910
    issubnormal(f) -> Bool
911

912
Test whether a floating point number is subnormal.
913

914
An IEEE floating point number is [subnormal](https://en.wikipedia.org/wiki/Subnormal_number)
915
when its exponent bits are zero and its significand is not zero.
916

917
# Examples
918
```jldoctest
919
julia> floatmin(Float32)
920
1.1754944f-38
921

922
julia> issubnormal(1.0f-37)
923
false
924

925
julia> issubnormal(1.0f-38)
926
true
927
```
928
"""
929
function issubnormal(x::T) where {T<:IEEEFloat}
3,331,725✔
930
    y = reinterpret(Unsigned, x)
4,885,710✔
931
    (y & exponent_mask(T) == 0) & (y & significand_mask(T) != 0)
4,885,710✔
932
end
933

934
ispow2(x::AbstractFloat) = !iszero(x) && frexp(x)[1] == 0.5
42✔
935
iseven(x::AbstractFloat) = isinteger(x) && (abs(x) > maxintfloat(x) || iseven(Integer(x)))
52✔
936
isodd(x::AbstractFloat) = isinteger(x) && abs(x) ≤ maxintfloat(x) && isodd(Integer(x))
28✔
937

938
@eval begin
939
    typemin(::Type{Float16}) = $(bitcast(Float16, 0xfc00))
5✔
940
    typemax(::Type{Float16}) = $(Inf16)
6✔
941
    typemin(::Type{Float32}) = $(-Inf32)
5✔
942
    typemax(::Type{Float32}) = $(Inf32)
229✔
943
    typemin(::Type{Float64}) = $(-Inf64)
42✔
944
    typemax(::Type{Float64}) = $(Inf64)
1,276✔
945
    typemin(x::T) where {T<:Real} = typemin(T)
×
946
    typemax(x::T) where {T<:Real} = typemax(T)
601,418,101✔
947

948
    floatmin(::Type{Float16}) = $(bitcast(Float16, 0x0400))
493,386✔
949
    floatmin(::Type{Float32}) = $(bitcast(Float32, 0x00800000))
160,934✔
950
    floatmin(::Type{Float64}) = $(bitcast(Float64, 0x0010000000000000))
×
951
    floatmax(::Type{Float16}) = $(bitcast(Float16, 0x7bff))
30,925✔
952
    floatmax(::Type{Float32}) = $(bitcast(Float32, 0x7f7fffff))
130,506✔
953
    floatmax(::Type{Float64}) = $(bitcast(Float64, 0x7fefffffffffffff))
702,806✔
954

955
    eps(x::AbstractFloat) = isfinite(x) ? abs(x) >= floatmin(x) ? ldexp(eps(typeof(x)), exponent(x)) : nextfloat(zero(x)) : oftype(x, NaN)
1,379,665✔
956
    eps(::Type{Float16}) = $(bitcast(Float16, 0x1400))
399,583✔
957
    eps(::Type{Float32}) = $(bitcast(Float32, 0x34000000))
620,527✔
958
    eps(::Type{Float64}) = $(bitcast(Float64, 0x3cb0000000000000))
×
959
    eps() = eps(Float64)
548✔
960
end
961

962
"""
963
    floatmin(T = Float64)
964

965
Return the smallest positive normal number representable by the floating-point
966
type `T`.
967

968
# Examples
969
```jldoctest
970
julia> floatmin(Float16)
971
Float16(6.104e-5)
972

973
julia> floatmin(Float32)
974
1.1754944f-38
975

976
julia> floatmin()
977
2.2250738585072014e-308
978
```
979
"""
980
floatmin(x::T) where {T<:AbstractFloat} = floatmin(T)
619,206✔
981

982
"""
983
    floatmax(T = Float64)
984

985
Return the largest finite number representable by the floating-point type `T`.
986

987
See also: [`typemax`](@ref), [`floatmin`](@ref), [`eps`](@ref).
988

989
# Examples
990
```jldoctest
991
julia> floatmax(Float16)
992
Float16(6.55e4)
993

994
julia> floatmax(Float32)
995
3.4028235f38
996

997
julia> floatmax()
998
1.7976931348623157e308
999

1000
julia> typemax(Float64)
1001
Inf
1002
```
1003
"""
1004
floatmax(x::T) where {T<:AbstractFloat} = floatmax(T)
276,760✔
1005

1006
floatmin() = floatmin(Float64)
16✔
1007
floatmax() = floatmax(Float64)
19✔
1008

1009
"""
1010
    eps(::Type{T}) where T<:AbstractFloat
1011
    eps()
1012

1013
Return the *machine epsilon* of the floating point type `T` (`T = Float64` by
1014
default). This is defined as the gap between 1 and the next largest value representable by
1015
`typeof(one(T))`, and is equivalent to `eps(one(T))`.  (Since `eps(T)` is a
1016
bound on the *relative error* of `T`, it is a "dimensionless" quantity like [`one`](@ref).)
1017

1018
# Examples
1019
```jldoctest
1020
julia> eps()
1021
2.220446049250313e-16
1022

1023
julia> eps(Float32)
1024
1.1920929f-7
1025

1026
julia> 1.0 + eps()
1027
1.0000000000000002
1028

1029
julia> 1.0 + eps()/2
1030
1.0
1031
```
1032
"""
1033
eps(::Type{<:AbstractFloat})
1034

1035
"""
1036
    eps(x::AbstractFloat)
1037

1038
Return the *unit in last place* (ulp) of `x`. This is the distance between consecutive
1039
representable floating point values at `x`. In most cases, if the distance on either side
1040
of `x` is different, then the larger of the two is taken, that is
1041

1042
    eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
1043

1044
The exceptions to this rule are the smallest and largest finite values
1045
(e.g. `nextfloat(-Inf)` and `prevfloat(Inf)` for [`Float64`](@ref)), which round to the
1046
smaller of the values.
1047

1048
The rationale for this behavior is that `eps` bounds the floating point rounding
1049
error. Under the default `RoundNearest` rounding mode, if ``y`` is a real number and ``x``
1050
is the nearest floating point number to ``y``, then
1051

1052
```math
1053
|y-x| \\leq \\operatorname{eps}(x)/2.
1054
```
1055

1056
See also: [`nextfloat`](@ref), [`issubnormal`](@ref), [`floatmax`](@ref).
1057

1058
# Examples
1059
```jldoctest
1060
julia> eps(1.0)
1061
2.220446049250313e-16
1062

1063
julia> eps(prevfloat(2.0))
1064
2.220446049250313e-16
1065

1066
julia> eps(2.0)
1067
4.440892098500626e-16
1068

1069
julia> x = prevfloat(Inf)      # largest finite Float64
1070
1.7976931348623157e308
1071

1072
julia> x + eps(x)/2            # rounds up
1073
Inf
1074

1075
julia> x + prevfloat(eps(x)/2) # rounds down
1076
1.7976931348623157e308
1077
```
1078
"""
1079
eps(::AbstractFloat)
1080

1081

1082
## byte order swaps for arbitrary-endianness serialization/deserialization ##
1083
bswap(x::IEEEFloat) = bswap_int(x)
7✔
1084

1085
# integer size of float
1086
uinttype(::Type{Float64}) = UInt64
×
1087
uinttype(::Type{Float32}) = UInt32
×
1088
uinttype(::Type{Float16}) = UInt16
×
1089
inttype(::Type{Float64}) = Int64
×
1090
inttype(::Type{Float32}) = Int32
9,390✔
1091
inttype(::Type{Float16}) = Int16
92✔
1092
# float size of integer
1093
floattype(::Type{UInt64}) = Float64
×
1094
floattype(::Type{UInt32}) = Float32
361✔
1095
floattype(::Type{UInt16}) = Float16
7✔
1096
floattype(::Type{Int64}) = Float64
×
1097
floattype(::Type{Int32}) = Float32
×
1098
floattype(::Type{Int16}) = Float16
×
1099

1100

1101
## Array operations on floating point numbers ##
1102

1103
float(A::AbstractArray{<:AbstractFloat}) = A
×
1104

1105
function float(A::AbstractArray{T}) where T
47✔
1106
    if !isconcretetype(T)
47✔
1107
        error("`float` not defined on abstractly-typed arrays; please convert to a more specific type")
×
1108
    end
1109
    convert(AbstractArray{typeof(float(zero(T)))}, A)
47✔
1110
end
1111

1112
float(r::StepRange) = float(r.start):float(r.step):float(last(r))
1✔
1113
float(r::UnitRange) = float(r.start):float(last(r))
1✔
1114
float(r::StepRangeLen{T}) where {T} =
4✔
1115
    StepRangeLen{typeof(float(T(r.ref)))}(float(r.ref), float(r.step), length(r), r.offset)
1116
function float(r::LinRange)
×
1117
    LinRange(float(r.start), float(r.stop), length(r))
×
1118
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc