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

JuliaLang / julia / #37525

pending completion
#37525

push

local

web-flow
NFC: some cleanup in gc.c (#49577)

71766 of 83435 relevant lines covered (86.01%)

34298284.82 hits per line

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

83.8
/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,158,143✔
80
reinterpret(::Type{Unsigned}, x::Float32) = reinterpret(UInt32, x)
605,835,028✔
81
reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16, x)
3,796,703✔
82
reinterpret(::Type{Signed}, x::Float64) = reinterpret(Int64, x)
600,259,607✔
83
reinterpret(::Type{Signed}, x::Float32) = reinterpret(Int32, x)
600,703,441✔
84
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16, x)
675,844✔
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,254✔
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,877✔
99
exponent_mask(::Type{Float16}) =    0x7c00
×
100
exponent_one(::Type{Float16}) =     0x3c00
×
101
exponent_half(::Type{Float16}) =    0x3800
999,256✔
102
significand_mask(::Type{Float16}) = 0x03ff
×
103

104
mantissa(x::T) where {T} = reinterpret(Unsigned, x) & significand_mask(T)
1,033,126✔
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,560✔
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,186✔
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,462,183✔
160
            promote_rule(::Type{$t1}, ::Type{$st}) = $t1
39,622,148✔
161
        end
162
    end
163
    for ut in (Bool, UInt8, UInt16, UInt32, UInt64)
164
        @eval begin
165
            (::Type{$t1})(x::($ut)) = uitofp($t1, x)
98,867,568✔
166
            promote_rule(::Type{$t1}, ::Type{$ut}) = $t1
1,653,023✔
167
        end
168
    end
169
end
170

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

173
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
46✔
174
promote_rule(::Type{Float64}, ::Type{Int128}) = Float64
693,593✔
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,955,983✔
201
    sign_bit = ((x >> 127) % UInt64) << 63
3,304,737✔
202
    ux = uabs(x)
3,956,007✔
203
    if ux < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
3,956,007✔
204
        low_exp = 0x1p52
×
205
        high_exp = 0x1p104
×
206
        low_bits = (ux % UInt64) & Base.significand_mask(Float64)
3,284,708✔
207
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
3,284,708✔
208
        high_bits = ((ux >> 52) % UInt64)
3,284,708✔
209
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
3,284,708✔
210
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
3,935,978✔
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,224,242✔
257
Float16(x::Float64) = fptrunc(Float16, x)
280,402✔
258
Float32(x::Float64) = fptrunc(Float32, x)
457,589,523✔
259

260
Float32(x::Float16) = fpext(Float32, x)
27,495,551✔
261
Float64(x::Float32) = fpext(Float64, x)
473,334,117✔
262
Float64(x::Float16) = fpext(Float64, x)
3,072,291✔
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,386,396✔
269
AbstractFloat(x::Int128)  = Float64(x) # LOSSY
1,308,508✔
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,577✔
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,140,897✔
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,922✔
312
float(::Type{T}) where {T<:AbstractFloat} = T
23,505✔
313
float(::Type{Union{}}, slurp...) = Union{}(0.0)
×
314

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

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

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

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

334
for Ti in (Int8, Int16, Int32, Int64)
335
    @eval begin
336
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptosi($Ti, x)
45,735,447✔
337
    end
338
end
339
for Ti in (UInt8, UInt16, UInt32, UInt64)
340
    @eval begin
341
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptoui($Ti, x)
41,124,184✔
342
    end
343
end
344

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

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

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

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

382
# fallbacks
383
floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))
80,585✔
384
ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
706,338✔
385
round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))
10,325,335✔
386

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

393
round(x::IEEEFloat, r::RoundingMode{:ToZero})  = trunc_llvm(x)
24,076,216✔
394
round(x::IEEEFloat, r::RoundingMode{:Down})    = floor_llvm(x)
167,005✔
395
round(x::IEEEFloat, r::RoundingMode{:Up})      = ceil_llvm(x)
746,929✔
396
round(x::IEEEFloat, r::RoundingMode{:Nearest}) = rint_llvm(x)
11,039,528✔
397

398
## floating point promotions ##
399
promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
11,256,992✔
400
promote_rule(::Type{Float64}, ::Type{Float16}) = Float64
×
401
promote_rule(::Type{Float64}, ::Type{Float32}) = Float64
×
402

403
widen(::Type{Float16}) = Float32
11,246,157✔
404
widen(::Type{Float32}) = Float64
9,336,284✔
405

406
## floating point arithmetic ##
407
-(x::IEEEFloat) = neg_float(x)
412,990,413✔
408

409
+(x::T, y::T) where {T<:IEEEFloat} = add_float(x, y)
657,379,486✔
410
-(x::T, y::T) where {T<:IEEEFloat} = sub_float(x, y)
1,241,979,806✔
411
*(x::T, y::T) where {T<:IEEEFloat} = mul_float(x, y)
4,173,432,061✔
412
/(x::T, y::T) where {T<:IEEEFloat} = div_float(x, y)
890,597,629✔
413

414
muladd(x::T, y::T, z::T) where {T<:IEEEFloat} = muladd_float(x, y, z)
819,282,602✔
415

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

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

424
function explicit_mantissa_noinfnan(x::T) where {T<:IEEEFloat}
1,498✔
425
    m = mantissa(x)
1,033,102✔
426
    issubnormal(x) || (m |= significand_mask(T) + uinttype(T)(1))
2,066,180✔
427
    return m
1,033,102✔
428
end
429

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

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

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

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

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

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

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

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

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

522
function mod(x::T, y::T) where {T<:AbstractFloat}
54,670✔
523
    r = rem(x,y)
97,688✔
524
    if r == 0
93,280✔
525
        copysign(r,y)
16,326✔
526
    elseif (r > 0) ⊻ (y > 0)
76,954✔
527
        r+y
28,275✔
528
    else
529
        r
48,679✔
530
    end
531
end
532

533
## floating point comparisons ##
534
==(x::T, y::T) where {T<:IEEEFloat} = eq_float(x, y)
437,874,821✔
535
!=(x::T, y::T) where {T<:IEEEFloat} = ne_float(x, y)
2,305,898,162✔
536
<( x::T, y::T) where {T<:IEEEFloat} = lt_float(x, y)
180,708,704✔
537
<=(x::T, y::T) where {T<:IEEEFloat} = le_float(x, y)
141,451,551✔
538

539
isequal(x::T, y::T) where {T<:IEEEFloat} = fpiseq(x, y)
4,808,859✔
540

541
# interpret as sign-magnitude integer
542
@inline function _fpint(x)
9,458✔
543
    IntT = inttype(typeof(x))
9,436✔
544
    ix = reinterpret(IntT, x)
5,388,886✔
545
    return ifelse(ix < zero(IntT), ix ⊻ typemax(IntT), ix)
5,388,886✔
546
end
547

548
@inline function isless(a::T, b::T) where T<:IEEEFloat
92,630✔
549
    (isnan(a) || isnan(b)) && return !isnan(a)
5,618,240✔
550

551
    return _fpint(a) < _fpint(b)
2,808,912✔
552
end
553

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

576
            function <(x::$Ti, y::$Tf)
41,005,038✔
577
                fx = ($Tf)(x)
41,007,074✔
578
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
41,159,837✔
579
            end
580
            function <=(x::$Ti, y::$Tf)
20,122✔
581
                fx = ($Tf)(x)
211,559✔
582
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
757,723✔
583
            end
584

585
            function <(x::$Tf, y::$Ti)
631,861✔
586
                fy = ($Tf)(y)
631,880✔
587
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
832,370✔
588
            end
589
            function <=(x::$Tf, y::$Ti)
21,046✔
590
                fy = ($Tf)(y)
21,046✔
591
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
21,684✔
592
            end
593
        end
594
    end
595
end
596
for op in (:(==), :<, :<=)
597
    @eval begin
598
        ($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
2,162,571✔
599
        ($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
253✔
600

601
        ($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
245,983✔
602
        ($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))
13✔
603

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

609

610
abs(x::IEEEFloat) = abs_float(x)
184,140,150✔
611

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

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

618
See also: [`iszero`](@ref), [`isone`](@ref), [`isinf`](@ref), [`ismissing`](@ref).
619
"""
620
isnan(x::AbstractFloat) = (x != x)::Bool
2,303,061,733✔
621
isnan(x::Number) = false
38,599✔
622

623
isfinite(x::AbstractFloat) = !isnan(x - x)
1,018,142,872✔
624
isfinite(x::Real) = decompose(x)[3] != 0
23,694✔
625
isfinite(x::Integer) = true
11,574✔
626

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

630
Test whether a number is infinite.
631

632
See also: [`Inf`](@ref), [`iszero`](@ref), [`isfinite`](@ref), [`isnan`](@ref).
633
"""
634
isinf(x::Real) = !isnan(x) & !isfinite(x)
20,236✔
635
isinf(x::IEEEFloat) = abs(x) === oftype(x, Inf)
36,546,358✔
636

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

658
hash(x::Float32, h::UInt) = hash(Float64(x), h)
2,635✔
659
hash(x::Float16, h::UInt) = hash(Float64(x), h)
7✔
660

661
## generic hashing for rational values ##
662

663
function hash(x::Real, h::UInt)
4,012✔
664
    # decompose x as num*2^pow/den
665
    num, pow, den = decompose(x)
1✔
666

667
    # handle special values
668
    num == 0 && den == 0 && return hash(NaN, h)
4,012✔
669
    num == 0 && return hash(ifelse(den > 0, 0.0, -0.0), h)
4,012✔
670
    den == 0 && return hash(ifelse(num > 0, Inf, -Inf), h)
1✔
671

672
    # normalize decomposition
673
    if den < 0
1✔
674
        num = -num
×
675
        den = -den
×
676
    end
677
    z = trailing_zeros(num)
4✔
678
    if z != 0
4✔
679
        num >>= z
4✔
680
        pow += z
3✔
681
    end
682
    z = trailing_zeros(den)
4✔
683
    if z != 0
1✔
684
        den >>= z
×
685
        pow -= z
×
686
    end
687

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

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

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

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

715
    num*2^pow/den
716

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

722
Special values:
723

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

729
decompose(x::Integer) = x, 0, 1
×
730

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

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

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

764

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

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

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

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

781
_precision(::Type{Float16}) = 11
×
782
_precision(::Type{Float32}) = 24
×
783
_precision(::Type{Float64}) = 53
×
784
function _precision(x, base::Integer=2)
6,083,404✔
785
    base > 1 || throw(DomainError(base, "`base` cannot be less than 2."))
6,083,408✔
786
    p = _precision(x)
12,133,703✔
787
    return base == 2 ? Int(p) : floor(Int, p / log2(base))
6,083,409✔
788
end
789
precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, base)
12,100,684✔
790
precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base)
134✔
791

792

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

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

804
    isnan(f) && return f
1,201,638,673✔
805
    fi = reinterpret(Signed, f)
1,201,638,671✔
806
    fneg = fi < 0
1,201,638,671✔
807
    fu = unsigned(fi & typemax(fi))
1,201,638,671✔
808

809
    dneg = d < 0
601,379,294✔
810
    da = uabs(d)
601,379,295✔
811
    if da > typemax(U)
1,201,638,671✔
812
        fneg = dneg
4✔
813
        fu = fumax
4✔
814
    else
815
        du = da % U
601,379,232✔
816
        if fneg ⊻ dneg
1,201,638,667✔
817
            if du > fu
891,459✔
818
                fu = min(fumax, du - fu)
56✔
819
                fneg = !fneg
56✔
820
            else
821
                fu = fu - du
1,525,137✔
822
            end
823
        else
824
            if fumax - fu < du
1,200,747,208✔
825
                fu = fumax
9✔
826
            else
827
                fu = fu + du
1,200,747,199✔
828
            end
829
        end
830
    end
831
    if fneg
1,201,638,671✔
832
        fu |= sign_mask(F)
663,716✔
833
    end
834
    reinterpret(F, fu)
1,201,638,671✔
835
end
836

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

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

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

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

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

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

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

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

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

913
Test whether a floating point number is subnormal.
914

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

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

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

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

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

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

949
    floatmin(::Type{Float16}) = $(bitcast(Float16, 0x0400))
493,804✔
950
    floatmin(::Type{Float32}) = $(bitcast(Float32, 0x00800000))
161,230✔
951
    floatmin(::Type{Float64}) = $(bitcast(Float64, 0x0010000000000000))
×
952
    floatmax(::Type{Float16}) = $(bitcast(Float16, 0x7bff))
30,895✔
953
    floatmax(::Type{Float32}) = $(bitcast(Float32, 0x7f7fffff))
130,522✔
954
    floatmax(::Type{Float64}) = $(bitcast(Float64, 0x7fefffffffffffff))
717,548✔
955

956
    eps(x::AbstractFloat) = isfinite(x) ? abs(x) >= floatmin(x) ? ldexp(eps(typeof(x)), exponent(x)) : nextfloat(zero(x)) : oftype(x, NaN)
1,380,015✔
957
    eps(::Type{Float16}) = $(bitcast(Float16, 0x1400))
399,807✔
958
    eps(::Type{Float32}) = $(bitcast(Float32, 0x34000000))
619,766✔
959
    eps(::Type{Float64}) = $(bitcast(Float64, 0x3cb0000000000000))
×
960
    eps() = eps(Float64)
548✔
961
end
962

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

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

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

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

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

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

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

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

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

995
julia> floatmax(Float32)
996
3.4028235f38
997

998
julia> floatmax()
999
1.7976931348623157e308
1000

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

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

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

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

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

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

1027
julia> 1.0 + eps()
1028
1.0000000000000002
1029

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1082

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

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

1101

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

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

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

1113
float(r::StepRange) = float(r.start):float(r.step):float(last(r))
1✔
1114
float(r::UnitRange) = float(r.start):float(last(r))
1✔
1115
float(r::StepRangeLen{T}) where {T} =
4✔
1116
    StepRangeLen{typeof(float(T(r.ref)))}(float(r.ref), float(r.step), length(r), r.offset)
1117
function float(r::LinRange)
×
1118
    LinRange(float(r.start), float(r.stop), length(r))
×
1119
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