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

JuliaLang / julia / #37594

pending completion
#37594

push

local

web-flow
Move `round(T::Type, x)` docstring above `round(z::Complex, ...)` docstring (#50775)

73676 of 84540 relevant lines covered (87.15%)

32579691.71 hits per line

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

89.63
/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)
10,793,350✔
80
reinterpret(::Type{Unsigned}, x::Float32) = reinterpret(UInt32, x)
605,835,615✔
81
reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16, x)
3,765,484✔
82
reinterpret(::Type{Signed}, x::Float64) = reinterpret(Int64, x)
600,260,416✔
83
reinterpret(::Type{Signed}, x::Float32) = reinterpret(Int32, x)
600,703,309✔
84
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16, x)
644,577✔
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,906✔
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,680,077✔
99
exponent_mask(::Type{Float16}) =    0x7c00
×
100
exponent_one(::Type{Float16}) =     0x3c00
×
101
exponent_half(::Type{Float16}) =    0x3800
999,326✔
102
significand_mask(::Type{Float16}) = 0x03ff
×
103

104
mantissa(x::T) where {T} = reinterpret(Unsigned, x) & significand_mask(T)
1,094,686✔
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,489,129✔
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,351,366✔
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)
266,870,842✔
160
            promote_rule(::Type{$t1}, ::Type{$st}) = $t1
40,283,570✔
161
        end
162
    end
163
    for ut in (Bool, UInt8, UInt16, UInt32, UInt64)
164
        @eval begin
165
            (::Type{$t1})(x::($ut)) = uitofp($t1, x)
93,496,080✔
166
            promote_rule(::Type{$t1}, ::Type{$ut}) = $t1
1,653,316✔
167
        end
168
    end
169
end
170

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

173
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
66✔
174
promote_rule(::Type{Float64}, ::Type{Int128}) = Float64
724,731✔
175
promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32
37✔
176
promote_rule(::Type{Float32}, ::Type{Int128}) = Float32
37✔
177
promote_rule(::Type{Float16}, ::Type{UInt128}) = Float16
37✔
178
promote_rule(::Type{Float16}, ::Type{Int128}) = Float16
37✔
179

180
function Float64(x::UInt128)
21,882✔
181
    if x < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
21,918✔
182
        low_exp = 0x1p52
×
183
        high_exp = 0x1p104
×
184
        low_bits = (x % UInt64) & Base.significand_mask(Float64)
891✔
185
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
891✔
186
        high_bits = ((x >> 52) % UInt64)
891✔
187
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
891✔
188
        low_value + high_value
907✔
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)
4,097,139✔
201
    sign_bit = ((x >> 127) % UInt64) << 63
3,445,905✔
202
    ux = uabs(x)
4,097,177✔
203
    if ux < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
4,097,177✔
204
        low_exp = 0x1p52
×
205
        high_exp = 0x1p104
×
206
        low_bits = (ux % UInt64) & Base.significand_mask(Float64)
3,425,876✔
207
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
3,425,876✔
208
        high_bits = ((ux >> 52) % UInt64)
3,425,876✔
209
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
3,425,876✔
210
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
4,077,148✔
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)
326✔
223
    x == 0 && return 0f0
326✔
224
    n = top_set_bit(x) # ndigits0z(x,2)
311✔
225
    if n <= 24
311✔
226
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
309✔
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
311✔
233
    reinterpret(Float32, d + y)
311✔
234
end
235

236
function Float32(x::Int128)
328✔
237
    x == 0 && return 0f0
328✔
238
    s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit
313✔
239
    x = abs(x) % UInt128
313✔
240
    n = top_set_bit(x) # ndigits0z(x,2)
313✔
241
    if n <= 24
313✔
242
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
310✔
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
313✔
249
    reinterpret(Float32, s | d + y)
313✔
250
end
251

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

256
Float16(x::Float32) = fptrunc(Float16, x)
6,695,716✔
257
Float16(x::Float64) = fptrunc(Float16, x)
282,389✔
258
Float32(x::Float64) = fptrunc(Float32, x)
457,973,073✔
259

260
Float32(x::Float16) = fpext(Float32, x)
27,503,587✔
261
Float64(x::Float32) = fpext(Float64, x)
478,504,437✔
262
Float64(x::Float16) = fpext(Float64, x)
3,047,697✔
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)
62,735✔
268
AbstractFloat(x::Int64)   = Float64(x) # LOSSY
24,454,978✔
269
AbstractFloat(x::Int128)  = Float64(x) # LOSSY
1,417,838✔
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,864✔
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)
44,336,020✔
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,925✔
312
float(::Type{T}) where {T<:AbstractFloat} = T
23,535✔
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)
46,120,778✔
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)
23,131,828✔
342
    end
343
end
344

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

359
function unsafe_trunc(::Type{UInt128}, x::Float32)
577✔
360
    xu = reinterpret(UInt32,x)
577✔
361
    k = Int(xu >> 23) & 0x00ff - 150
577✔
362
    xu = (xu & 0x007f_ffff) | 0x0080_0000
577✔
363
    if k <= 0
577✔
364
        UInt128(xu >> -k)
577✔
365
    else
366
        UInt128(xu) << k
×
367
    end
368
end
369
function unsafe_trunc(::Type{Int128}, x::Float32)
289✔
370
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
289✔
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)
3,124✔
381

382
# Bool
383
trunc(::Type{Bool}, x::AbstractFloat) = (-1 < x < 2) ? 1 <= x : throw(InexactError(:trunc, Bool, x))
8✔
384
floor(::Type{Bool}, x::AbstractFloat) = (0 <= x < 2) ? 1 <= x : throw(InexactError(:floor, Bool, x))
6✔
385
ceil(::Type{Bool}, x::AbstractFloat)  = (-1 < x <= 1) ? 0 < x : throw(InexactError(:ceil, Bool, x))
6✔
386
round(::Type{Bool}, x::AbstractFloat) = (-0.5 <= x < 1.5) ? 0.5 < x : throw(InexactError(:round, Bool, x))
8✔
387

388
round(x::IEEEFloat, r::RoundingMode{:ToZero})  = trunc_llvm(x)
24,113,111✔
389
round(x::IEEEFloat, r::RoundingMode{:Down})    = floor_llvm(x)
304,058✔
390
round(x::IEEEFloat, r::RoundingMode{:Up})      = ceil_llvm(x)
677,493✔
391
round(x::IEEEFloat, r::RoundingMode{:Nearest}) = rint_llvm(x)
11,072,293✔
392

393
## floating point promotions ##
394
promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
12,259,797✔
395
promote_rule(::Type{Float64}, ::Type{Float16}) = Float64
×
396
promote_rule(::Type{Float64}, ::Type{Float32}) = Float64
×
397

398
widen(::Type{Float16}) = Float32
11,750,531✔
399
widen(::Type{Float32}) = Float64
9,544,752✔
400

401
## floating point arithmetic ##
402
-(x::IEEEFloat) = neg_float(x)
414,555,733✔
403

404
+(x::T, y::T) where {T<:IEEEFloat} = add_float(x, y)
715,459,805✔
405
-(x::T, y::T) where {T<:IEEEFloat} = sub_float(x, y)
1,246,586,268✔
406
*(x::T, y::T) where {T<:IEEEFloat} = mul_float(x, y)
4,200,435,554✔
407
/(x::T, y::T) where {T<:IEEEFloat} = div_float(x, y)
896,193,507✔
408

409
muladd(x::T, y::T, z::T) where {T<:IEEEFloat} = muladd_float(x, y, z)
819,009,682✔
410

411
# TODO: faster floating point div?
412
# TODO: faster floating point fld?
413
# TODO: faster floating point mod?
414

415
function unbiased_exponent(x::T) where {T<:IEEEFloat}
1,498✔
416
    return (reinterpret(Unsigned, x) & exponent_mask(T)) >> significand_bits(T)
1,094,662✔
417
end
418

419
function explicit_mantissa_noinfnan(x::T) where {T<:IEEEFloat}
1,498✔
420
    m = mantissa(x)
1,094,662✔
421
    issubnormal(x) || (m |= significand_mask(T) + uinttype(T)(1))
2,189,300✔
422
    return m
1,094,662✔
423
end
424

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

442
@assume_effects :terminates_locally :nothrow function rem_internal(x::T, y::T) where {T<:IEEEFloat}
621,130✔
443
    xuint = reinterpret(Unsigned, x)
621,130✔
444
    yuint = reinterpret(Unsigned, y)
621,130✔
445
    if xuint <= yuint
621,130✔
446
        if xuint < yuint
73,799✔
447
            return x
68,109✔
448
        end
449
        return zero(T)
5,690✔
450
    end
451

452
    e_x = unbiased_exponent(x)
547,331✔
453
    e_y = unbiased_exponent(y)
547,331✔
454
    # Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
455
    if e_y > (significand_bits(T)) && (e_x - e_y) <= (exponent_bits(T))
547,331✔
456
        m_x = explicit_mantissa_noinfnan(x)
264,234✔
457
        m_y = explicit_mantissa_noinfnan(y)
264,234✔
458
        d = urem_int((m_x << (e_x - e_y)),  m_y)
132,117✔
459
        iszero(d) && return zero(T)
132,117✔
460
        return _to_float(d, e_y - uinttype(T)(1))
113,367✔
461
    end
462
    # Both are subnormals
463
    if e_x == 0 && e_y == 0
415,214✔
464
        return reinterpret(T, urem_int(xuint, yuint) & significand_mask(T))
×
465
    end
466

467
    m_x = explicit_mantissa_noinfnan(x)
830,428✔
468
    e_x -= uinttype(T)(1)
415,214✔
469
    m_y = explicit_mantissa_noinfnan(y)
830,404✔
470
    lz_m_y = uinttype(T)(exponent_bits(T))
44✔
471
    if e_y > 0
415,214✔
472
        e_y -= uinttype(T)(1)
415,190✔
473
    else
474
        m_y = mantissa(y)
24✔
475
        lz_m_y = Core.Intrinsics.ctlz_int(m_y)
24✔
476
    end
477

478
    tz_m_y = Core.Intrinsics.cttz_int(m_y)
415,214✔
479
    sides_zeroes_cnt = lz_m_y + tz_m_y
415,214✔
480

481
    # n>0
482
    exp_diff = e_x - e_y
415,214✔
483
    # Shift hy right until the end or n = 0
484
    right_shift = min(exp_diff, tz_m_y)
415,214✔
485
    m_y >>= right_shift
415,214✔
486
    exp_diff -= right_shift
415,214✔
487
    e_y += right_shift
415,214✔
488
    # Shift hx left until the end or n = 0
489
    left_shift = min(exp_diff, uinttype(T)(exponent_bits(T)))
415,214✔
490
    m_x <<= left_shift
415,214✔
491
    exp_diff -= left_shift
415,214✔
492

493
    m_x = urem_int(m_x, m_y)
415,214✔
494
    iszero(m_x) && return zero(T)
415,214✔
495
    iszero(exp_diff) && return _to_float(m_x, e_y)
414,929✔
496

497
    while exp_diff > sides_zeroes_cnt
402,808✔
498
        exp_diff -= sides_zeroes_cnt
1,215✔
499
        m_x <<= sides_zeroes_cnt
1,215✔
500
        m_x = urem_int(m_x, m_y)
1,215✔
501
    end
1,215✔
502
    m_x <<= exp_diff
401,593✔
503
    m_x = urem_int(m_x, m_y)
401,593✔
504
    return _to_float(m_x, e_y)
401,593✔
505
end
506

507
function rem(x::T, y::T) where {T<:IEEEFloat}
22,350✔
508
    if isfinite(x) && !iszero(x) && isfinite(y) && !iszero(y)
629,800✔
509
        return copysign(rem_internal(abs(x), abs(y)), x)
621,140✔
510
    elseif isinf(x) || isnan(y) || iszero(y)  # y can still be Inf
17,307✔
511
        return T(NaN)
41✔
512
    else
513
        return x
8,619✔
514
    end
515
end
516

517
function mod(x::T, y::T) where {T<:AbstractFloat}
54,925✔
518
    r = rem(x,y)
97,373✔
519
    if r == 0
92,992✔
520
        copysign(r,y)
16,754✔
521
    elseif (r > 0) ⊻ (y > 0)
76,238✔
522
        r+y
28,595✔
523
    else
524
        r
47,643✔
525
    end
526
end
527

528
## floating point comparisons ##
529
==(x::T, y::T) where {T<:IEEEFloat} = eq_float(x, y)
199,048,654✔
530
!=(x::T, y::T) where {T<:IEEEFloat} = ne_float(x, y)
2,331,707,275✔
531
<( x::T, y::T) where {T<:IEEEFloat} = lt_float(x, y)
162,550,530✔
532
<=(x::T, y::T) where {T<:IEEEFloat} = le_float(x, y)
143,061,770✔
533

534
isequal(x::T, y::T) where {T<:IEEEFloat} = fpiseq(x, y)
4,983,431✔
535

536
# interpret as sign-magnitude integer
537
@inline function _fpint(x)
10,394✔
538
    IntT = inttype(typeof(x))
10,368✔
539
    ix = reinterpret(IntT, x)
31,703,536✔
540
    return ifelse(ix < zero(IntT), ix ⊻ typemax(IntT), ix)
31,703,536✔
541
end
542

543
@inline function isless(a::T, b::T) where T<:IEEEFloat
106,001✔
544
    (isnan(a) || isnan(b)) && return !isnan(a)
31,983,346✔
545

546
    return _fpint(a) < _fpint(b)
15,966,229✔
547
end
548

549
# Exact Float (Tf) vs Integer (Ti) comparisons
550
# Assumes:
551
# - typemax(Ti) == 2^n-1
552
# - typemax(Ti) can't be exactly represented by Tf:
553
#   => Tf(typemax(Ti)) == 2^n or Inf
554
# - typemin(Ti) can be exactly represented by Tf
555
#
556
# 1. convert y::Ti to float fy::Tf
557
# 2. perform Tf comparison x vs fy
558
# 3. if x == fy, check if (1) resulted in rounding:
559
#  a. convert fy back to Ti and compare with original y
560
#  b. unsafe_convert undefined behaviour if fy == Tf(typemax(Ti))
561
#     (but consequently x == fy > y)
562
for Ti in (Int64,UInt64,Int128,UInt128)
563
    for Tf in (Float32,Float64)
564
        @eval begin
565
            function ==(x::$Tf, y::$Ti)
5,617,329✔
566
                fy = ($Tf)(y)
6,126,393✔
567
                (x == fy) & (fy != $(Tf(typemax(Ti)))) & (y == unsafe_trunc($Ti,fy))
8,612,460✔
568
            end
569
            ==(y::$Ti, x::$Tf) = x==y
269,535✔
570

571
            function <(x::$Ti, y::$Tf)
23,071,610✔
572
                fx = ($Tf)(x)
23,085,264✔
573
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
23,236,425✔
574
            end
575
            function <=(x::$Ti, y::$Tf)
21,679✔
576
                fx = ($Tf)(x)
203,073✔
577
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
782,255✔
578
            end
579

580
            function <(x::$Tf, y::$Ti)
632,966✔
581
                fy = ($Tf)(y)
821,128✔
582
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
1,108,784✔
583
            end
584
            function <=(x::$Tf, y::$Ti)
21,911✔
585
                fy = ($Tf)(y)
21,911✔
586
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
22,538✔
587
            end
588
        end
589
    end
590
end
591
for op in (:(==), :<, :<=)
592
    @eval begin
593
        ($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
2,136,873✔
594
        ($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
2,051✔
595

596
        ($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
246,721✔
597
        ($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))
611✔
598

599
        ($op)(x::Float16, y::Union{Int16,UInt16}) = ($op)(Float32(x), Float32(y))
280✔
600
        ($op)(x::Union{Int16,UInt16}, y::Float16) = ($op)(Float32(x), Float32(y))
278✔
601
    end
602
end
603

604

605
abs(x::IEEEFloat) = abs_float(x)
185,533,213✔
606

607
"""
608
    isnan(f) -> Bool
609

610
Test whether a number value is a NaN, an indeterminate value which is neither an infinity
611
nor a finite number ("not a number").
612

613
See also: [`iszero`](@ref), [`isone`](@ref), [`isinf`](@ref), [`ismissing`](@ref).
614
"""
615
isnan(x::AbstractFloat) = (x != x)::Bool
2,328,813,899✔
616
isnan(x::Number) = false
104,466✔
617

618
isfinite(x::AbstractFloat) = !isnan(x - x)
1,017,653,326✔
619
isfinite(x::Real) = decompose(x)[3] != 0
73,819✔
620
isfinite(x::Integer) = true
122,408✔
621

622
"""
623
    isinf(f) -> Bool
624

625
Test whether a number is infinite.
626

627
See also: [`Inf`](@ref), [`iszero`](@ref), [`isfinite`](@ref), [`isnan`](@ref).
628
"""
629
isinf(x::Real) = !isnan(x) & !isfinite(x)
181,512✔
630
isinf(x::IEEEFloat) = abs(x) === oftype(x, Inf)
36,492,094✔
631

632
const hx_NaN = hash_uint64(reinterpret(UInt64, NaN))
633
function hash(x::Float64, h::UInt)
92,029✔
634
    # see comments on trunc and hash(Real, UInt)
635
    if typemin(Int64) <= x < typemax(Int64)
92,029✔
636
        xi = fptosi(Int64, x)
91,862✔
637
        if isequal(xi, x)
91,862✔
638
            return hash(xi, h)
35,035✔
639
        end
640
    elseif typemin(UInt64) <= x < typemax(UInt64)
167✔
641
        xu = fptoui(UInt64, x)
94✔
642
        if isequal(xu, x)
94✔
643
            return hash(xu, h)
94✔
644
        end
645
    elseif isnan(x)
73✔
646
        return hx_NaN ⊻ h # NaN does not have a stable bit pattern
66✔
647
    end
648
    return hash_uint64(bitcast(UInt64, x)) - 3h
56,834✔
649
end
650

651
hash(x::Float32, h::UInt) = hash(Float64(x), h)
4,293✔
652

653
function hash(x::Float16, h::UInt)
6✔
654
    # see comments on trunc and hash(Real, UInt)
655
    if isfinite(x) # all finite Float16 fit in Int64
6✔
656
        xi = fptosi(Int64, x)
6✔
657
        if isequal(xi, x)
6✔
658
            return hash(xi, h)
6✔
659
        end
660
    elseif isnan(x)
×
661
        return hx_NaN ⊻ h # NaN does not have a stable bit pattern
×
662
    end
663
    return hash_uint64(bitcast(UInt64, Float64(x))) - 3h
×
664
end
665

666
## generic hashing for rational values ##
667
function hash(x::Real, h::UInt)
9,606✔
668
    # decompose x as num*2^pow/den
669
    num, pow, den = decompose(x)
9,738✔
670

671
    # handle special values
672
    num == 0 && den == 0 && return hash(NaN, h)
9,606✔
673
    num == 0 && return hash(ifelse(den > 0, 0.0, -0.0), h)
9,606✔
674
    den == 0 && return hash(ifelse(num > 0, Inf, -Inf), h)
5,344✔
675

676
    # normalize decomposition
677
    if den < 0
5,344✔
678
        num = -num
902✔
679
        den = -den
902✔
680
    end
681
    num_z = trailing_zeros(num)
5,344✔
682
    num >>= num_z
8,006✔
683
    den_z = trailing_zeros(den)
5,344✔
684
    den >>= den_z
5,347✔
685
    pow += num_z - den_z
5,344✔
686
    # If the real can be represented as an Int64, UInt64, or Float64, hash as those types.
687
    # To be an Integer the denominator must be 1 and the power must be non-negative.
688
    if den == 1
5,344✔
689
        # left = ceil(log2(num*2^pow))
690
        left = top_set_bit(abs(num)) + pow
8,806✔
691
        # 2^-1074 is the minimum Float64 so if the power is smaller, not a Float64
692
        if -1074 <= pow
5,341✔
693
            if 0 <= pow # if pow is non-negative, it is an integer
5,341✔
694
                left <= 63 && return hash(Int64(num) << Int(pow), h)
5,234✔
695
                left <= 64 && !signbit(num) && return hash(UInt64(num) << Int(pow), h)
736✔
696
            end # typemin(Int64) handled by Float64 case
697
            # 2^1024 is the maximum Float64 so if the power is greater, not a Float64
698
            # Float64s only have 53 mantisa bits (including implicit bit)
699
            left <= 1024 && left - pow <= 53 && return hash(ldexp(Float64(num), pow), h)
775✔
700
        end
701
    else
702
        h = hash_integer(den, h)
3✔
703
    end
704
    # handle generic rational values
705
    h = hash_integer(pow, h)
565✔
706
    h = hash_integer(num, h)
565✔
707
    return h
565✔
708
end
709

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

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

717
    num*2^pow/den
718

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

724
Special values:
725

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

731
decompose(x::Integer) = x, 0, 1
5,446✔
732

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

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

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

766

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

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

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

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

783
_precision(::Type{Float16}) = 11
×
784
_precision(::Type{Float32}) = 24
×
785
_precision(::Type{Float64}) = 53
×
786
function _precision(x, base::Integer=2)
12,090,046✔
787
    base > 1 || throw(DomainError(base, "`base` cannot be less than 2."))
12,090,050✔
788
    p = _precision(x)
24,145,216✔
789
    return base == 2 ? Int(p) : floor(Int, p / log2(base))
12,090,051✔
790
end
791
precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, base)
24,110,426✔
792
precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base)
134✔
793

794

795
"""
796
    nextfloat(x::AbstractFloat, n::Integer)
797

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

806
    isnan(f) && return f
1,201,608,083✔
807
    fi = reinterpret(Signed, f)
1,201,608,081✔
808
    fneg = fi < 0
1,201,608,081✔
809
    fu = unsigned(fi & typemax(fi))
1,201,608,081✔
810

811
    dneg = d < 0
601,347,896✔
812
    da = uabs(d)
601,347,897✔
813
    if da > typemax(U)
1,201,608,081✔
814
        fneg = dneg
4✔
815
        fu = fumax
4✔
816
    else
817
        du = da % U
601,347,833✔
818
        if fneg ⊻ dneg
1,201,608,077✔
819
            if du > fu
891,843✔
820
                fu = min(fumax, du - fu)
59✔
821
                fneg = !fneg
59✔
822
            else
823
                fu = fu - du
1,525,929✔
824
            end
825
        else
826
            if fumax - fu < du
1,200,716,234✔
827
                fu = fumax
9✔
828
            else
829
                fu = fu + du
1,200,716,225✔
830
            end
831
        end
832
    end
833
    if fneg
1,201,608,081✔
834
        fu |= sign_mask(F)
663,723✔
835
    end
836
    reinterpret(F, fu)
1,201,608,081✔
837
end
838

839
"""
840
    nextfloat(x::AbstractFloat)
841

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

845
See also: [`prevfloat`](@ref), [`eps`](@ref), [`issubnormal`](@ref).
846
"""
847
nextfloat(x::AbstractFloat) = nextfloat(x,1)
1,200,965,656✔
848

849
"""
850
    prevfloat(x::AbstractFloat, n::Integer)
851

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

857
"""
858
    prevfloat(x::AbstractFloat)
859

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

865
for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
866
    for Tf in (Float16, Float32, Float64)
867
        if Ti <: Unsigned || sizeof(Ti) < sizeof(Tf)
868
            # Here `Tf(typemin(Ti))-1` is exact, so we can compare the lower-bound
869
            # directly. `Tf(typemax(Ti))+1` is either always exactly representable, or
870
            # rounded to `Inf` (e.g. when `Ti==UInt128 && Tf==Float32`).
871
            @eval begin
872
                function trunc(::Type{$Ti},x::$Tf)
9,627✔
873
                    if $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf))
416,026✔
874
                        return unsafe_trunc($Ti,x)
416,010✔
875
                    else
876
                        throw(InexactError(:trunc, $Ti, x))
16✔
877
                    end
878
                end
879
                function (::Type{$Ti})(x::$Tf)
2,583✔
880
                    if ($(Tf(typemin(Ti))) <= x <= $(Tf(typemax(Ti)))) && isinteger(x)
2,856✔
881
                        return unsafe_trunc($Ti,x)
3,791✔
882
                    else
883
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
66✔
884
                    end
885
                end
886
            end
887
        else
888
            # Here `eps(Tf(typemin(Ti))) > 1`, so the only value which can be truncated to
889
            # `Tf(typemin(Ti)` is itself. Similarly, `Tf(typemax(Ti))` is inexact and will
890
            # be rounded up. This assumes that `Tf(typemin(Ti)) > -Inf`, which is true for
891
            # these types, but not for `Float16` or larger integer types.
892
            @eval begin
893
                function trunc(::Type{$Ti},x::$Tf)
24,227,565✔
894
                    if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
32,843,375✔
895
                        return unsafe_trunc($Ti,x)
32,843,367✔
896
                    else
897
                        throw(InexactError(:trunc, $Ti, x))
8✔
898
                    end
899
                end
900
                function (::Type{$Ti})(x::$Tf)
23,218,795✔
901
                    if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) && isinteger(x)
23,950,288✔
902
                        return unsafe_trunc($Ti,x)
23,950,184✔
903
                    else
904
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
94✔
905
                    end
906
                end
907
            end
908
        end
909
    end
910
end
911

912
"""
913
    issubnormal(f) -> Bool
914

915
Test whether a floating point number is subnormal.
916

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

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

925
julia> issubnormal(1.0f-37)
926
false
927

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

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

941
@eval begin
942
    typemin(::Type{Float16}) = $(bitcast(Float16, 0xfc00))
16✔
943
    typemax(::Type{Float16}) = $(Inf16)
17✔
944
    typemin(::Type{Float32}) = $(-Inf32)
16✔
945
    typemax(::Type{Float32}) = $(Inf32)
243✔
946
    typemin(::Type{Float64}) = $(-Inf64)
53✔
947
    typemax(::Type{Float64}) = $(Inf64)
1,297✔
948
    typemin(x::T) where {T<:Real} = typemin(T)
5,049✔
949
    typemax(x::T) where {T<:Real} = typemax(T)
601,387,589✔
950

951
    floatmin(::Type{Float16}) = $(bitcast(Float16, 0x0400))
493,362✔
952
    floatmin(::Type{Float32}) = $(bitcast(Float32, 0x00800000))
161,350✔
953
    floatmin(::Type{Float64}) = $(bitcast(Float64, 0x0010000000000000))
×
954
    floatmax(::Type{Float16}) = $(bitcast(Float16, 0x7bff))
156✔
955
    floatmax(::Type{Float32}) = $(bitcast(Float32, 0x7f7fffff))
130,495✔
956
    floatmax(::Type{Float64}) = $(bitcast(Float64, 0x7fefffffffffffff))
978,174✔
957

958
    eps(x::AbstractFloat) = isfinite(x) ? abs(x) >= floatmin(x) ? ldexp(eps(typeof(x)), exponent(x)) : nextfloat(zero(x)) : oftype(x, NaN)
1,379,808✔
959
    eps(::Type{Float16}) = $(bitcast(Float16, 0x1400))
399,917✔
960
    eps(::Type{Float32}) = $(bitcast(Float32, 0x34000000))
618,560✔
961
    eps(::Type{Float64}) = $(bitcast(Float64, 0x3cb0000000000000))
1✔
962
    eps() = eps(Float64)
232✔
963
end
964

965
"""
966
    floatmin(T = Float64)
967

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

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

976
julia> floatmin(Float32)
977
1.1754944f-38
978

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

985
"""
986
    floatmax(T = Float64)
987

988
Return the largest finite number representable by the floating-point type `T`.
989

990
See also: [`typemax`](@ref), [`floatmin`](@ref), [`eps`](@ref).
991

992
# Examples
993
```jldoctest
994
julia> floatmax(Float16)
995
Float16(6.55e4)
996

997
julia> floatmax(Float32)
998
3.4028235f38
999

1000
julia> floatmax()
1001
1.7976931348623157e308
1002

1003
julia> typemax(Float64)
1004
Inf
1005
```
1006
"""
1007
floatmax(x::T) where {T<:AbstractFloat} = floatmax(T)
252,879✔
1008

1009
floatmin() = floatmin(Float64)
16✔
1010
floatmax() = floatmax(Float64)
19✔
1011

1012
"""
1013
    eps(::Type{T}) where T<:AbstractFloat
1014
    eps()
1015

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

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

1026
julia> eps(Float32)
1027
1.1920929f-7
1028

1029
julia> 1.0 + eps()
1030
1.0000000000000002
1031

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

1038
"""
1039
    eps(x::AbstractFloat)
1040

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

1045
    eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
1046

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

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

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

1059
See also: [`nextfloat`](@ref), [`issubnormal`](@ref), [`floatmax`](@ref).
1060

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

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

1069
julia> eps(2.0)
1070
4.440892098500626e-16
1071

1072
julia> x = prevfloat(Inf)      # largest finite Float64
1073
1.7976931348623157e308
1074

1075
julia> x + eps(x)/2            # rounds up
1076
Inf
1077

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

1084

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

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

1103

1104
## Array operations on floating point numbers ##
1105

1106
float(A::AbstractArray{<:AbstractFloat}) = A
×
1107

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

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