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

JuliaLang / julia / #37650

12 Oct 2023 03:02PM UTC coverage: 85.263% (-2.3%) from 87.56%
#37650

push

local

web-flow
Revert "Reinstate load-time Pkg.precompile" (#51675)

1 of 1 new or added line in 1 file covered. (100.0%)

70225 of 82363 relevant lines covered (85.26%)

12218375.87 hits per line

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

87.34
/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,901,085✔
80
reinterpret(::Type{Unsigned}, x::Float32) = reinterpret(UInt32, x)
605,581,005✔
81
reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16, x)
3,796,602✔
82
reinterpret(::Type{Signed}, x::Float64) = reinterpret(Int64, x)
600,259,753✔
83
reinterpret(::Type{Signed}, x::Float32) = reinterpret(Int32, x)
600,449,726✔
84
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16, x)
675,994✔
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,707,971✔
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,721✔
99
exponent_mask(::Type{Float16}) =    0x7c00
×
100
exponent_one(::Type{Float16}) =     0x3c00
×
101
exponent_half(::Type{Float16}) =    0x3800
999,280✔
102
significand_mask(::Type{Float16}) = 0x03ff
×
103

104
mantissa(x::T) where {T} = reinterpret(Unsigned, x) & significand_mask(T)
1,077,124✔
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)
19,002,203✔
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)))
9,080,965✔
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
IEEE 754 definition of the minimum exponent.
142
"""
143
ieee754_exponent_min(::Type{T}) where {T<:IEEEFloat} = Int(1 - exponent_max(T))::Int
19,002,112✔
144

145
exponent_min(::Type{Float16}) = ieee754_exponent_min(Float16)
19,002,112✔
146
exponent_min(::Type{Float32}) = ieee754_exponent_min(Float32)
×
147
exponent_min(::Type{Float64}) = ieee754_exponent_min(Float64)
×
148

149
function ieee754_representation(
19,002,112✔
150
    ::Type{F}, sign_bit::Bool, exponent_field::Integer, significand_field::Integer
151
) where {F<:IEEEFloat}
152
    T = uinttype(F)
19,002,112✔
153
    ret::T = sign_bit
77,493,747✔
154
    ret <<= exponent_bits(F)
77,493,747✔
155
    ret |= exponent_field
77,493,747✔
156
    ret <<= significand_bits(F)
77,493,747✔
157
    ret |= significand_field
77,493,747✔
158
end
159

160
# ±floatmax(T)
161
function ieee754_representation(
7,524,076✔
162
    ::Type{F}, sign_bit::Bool, ::Val{:omega}
163
) where {F<:IEEEFloat}
164
    ieee754_representation(F, sign_bit, exponent_raw_max(F) - 1, significand_mask(F))
8,761,748✔
165
end
166

167
# NaN or an infinity
168
function ieee754_representation(
5,122,614✔
169
    ::Type{F}, sign_bit::Bool, significand_field::Integer, ::Val{:nan}
170
) where {F<:IEEEFloat}
171
    ieee754_representation(F, sign_bit, exponent_raw_max(F), significand_field)
11,702,137✔
172
end
173

174
# NaN with default payload
175
function ieee754_representation(
26✔
176
    ::Type{F}, sign_bit::Bool, ::Val{:nan}
177
) where {F<:IEEEFloat}
178
    ieee754_representation(F, sign_bit, one(uinttype(F)) << (significand_bits(F) - 1), Val(:nan))
91✔
179
end
180

181
# Infinity
182
function ieee754_representation(
9,665,311✔
183
    ::Type{F}, sign_bit::Bool, ::Val{:inf}
184
) where {F<:IEEEFloat}
185
    ieee754_representation(F, sign_bit, false, Val(:nan))
11,702,046✔
186
end
187

188
# Subnormal or zero
189
function ieee754_representation(
4,787,247✔
190
    ::Type{F}, sign_bit::Bool, significand_field::Integer, ::Val{:subnormal}
191
) where {F<:IEEEFloat}
192
    ieee754_representation(F, sign_bit, false, significand_field)
10,871,434✔
193
end
194

195
# Zero
196
function ieee754_representation(
9,082,050✔
197
    ::Type{F}, sign_bit::Bool, ::Val{:zero}
198
) where {F<:IEEEFloat}
199
    ieee754_representation(F, sign_bit, false, Val(:subnormal))
10,871,434✔
200
end
201

202
"""
203
    uabs(x::Integer)
204

205
Return the absolute value of `x`, possibly returning a different type should the
206
operation be susceptible to overflow. This typically arises when `x` is a two's complement
207
signed integer, so that `abs(typemin(x)) == typemin(x) < 0`, in which case the result of
208
`uabs(x)` will be an unsigned integer of the same size.
209
"""
210
uabs(x::Integer) = abs(x)
5✔
211
uabs(x::BitSigned) = unsigned(abs(x))
4,096,941✔
212

213
## conversions to floating-point ##
214

215
# TODO: deprecate in 2.0
216
Float16(x::Integer) = convert(Float16, convert(Float32, x)::Float32)
×
217

218
for t1 in (Float16, Float32, Float64)
219
    for st in (Int8, Int16, Int32, Int64)
220
        @eval begin
221
            (::Type{$t1})(x::($st)) = sitofp($t1, x)
263,856,360✔
222
            promote_rule(::Type{$t1}, ::Type{$st}) = $t1
40,374,799✔
223
        end
224
    end
225
    for ut in (Bool, UInt8, UInt16, UInt32, UInt64)
226
        @eval begin
227
            (::Type{$t1})(x::($ut)) = uitofp($t1, x)
112,250,404✔
228
            promote_rule(::Type{$t1}, ::Type{$ut}) = $t1
1,650,804✔
229
        end
230
    end
231
end
232

233
Bool(x::Real) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x))
16,407,348✔
234

235
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
53✔
236
promote_rule(::Type{Float64}, ::Type{Int128}) = Float64
724,735✔
237
promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32
23✔
238
promote_rule(::Type{Float32}, ::Type{Int128}) = Float32
23✔
239
promote_rule(::Type{Float16}, ::Type{UInt128}) = Float16
23✔
240
promote_rule(::Type{Float16}, ::Type{Int128}) = Float16
23✔
241

242
function Float64(x::UInt128)
21,182✔
243
    if x < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
21,206✔
244
        low_exp = 0x1p52
×
245
        high_exp = 0x1p104
×
246
        low_bits = (x % UInt64) & Base.significand_mask(Float64)
177✔
247
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
177✔
248
        high_bits = ((x >> 52) % UInt64)
177✔
249
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
177✔
250
        low_value + high_value
193✔
251
    else # Large enough that low bits only affect rounding, pack low bits
252
        low_exp = 0x1p76
×
253
        high_exp = 0x1p128
×
254
        low_bits = ((x >> 12) % UInt64) >> 12 | (x % UInt64) & 0xFFFFFF
21,013✔
255
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
21,013✔
256
        high_bits = ((x >> 76) % UInt64)
21,013✔
257
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
21,013✔
258
        low_value + high_value
21,013✔
259
    end
260
end
261

262
function Float64(x::Int128)
4,096,263✔
263
    sign_bit = ((x >> 127) % UInt64) << 63
3,445,001✔
264
    ux = uabs(x)
4,096,289✔
265
    if ux < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
4,096,289✔
266
        low_exp = 0x1p52
×
267
        high_exp = 0x1p104
×
268
        low_bits = (ux % UInt64) & Base.significand_mask(Float64)
3,424,965✔
269
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
3,424,965✔
270
        high_bits = ((ux >> 52) % UInt64)
3,424,965✔
271
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
3,424,965✔
272
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
4,076,253✔
273
    else # Large enough that low bits only affect rounding, pack low bits
274
        low_exp = 0x1p76
×
275
        high_exp = 0x1p128
×
276
        low_bits = ((ux >> 12) % UInt64) >> 12 | (ux % UInt64) & 0xFFFFFF
20,036✔
277
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
20,036✔
278
        high_bits = ((ux >> 76) % UInt64)
20,036✔
279
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
20,036✔
280
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
20,036✔
281
    end
282
end
283

284
function Float32(x::UInt128)
30✔
285
    x == 0 && return 0f0
30✔
286
    n = top_set_bit(x) # ndigits0z(x,2)
28✔
287
    if n <= 24
28✔
288
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
25✔
289
    else
290
        y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit
3✔
291
        y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent)
3✔
292
        y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even
3✔
293
    end
294
    d = ((n+126) % UInt32) << 23
28✔
295
    reinterpret(Float32, d + y)
28✔
296
end
297

298
function Float32(x::Int128)
32✔
299
    x == 0 && return 0f0
32✔
300
    s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit
31✔
301
    x = abs(x) % UInt128
31✔
302
    n = top_set_bit(x) # ndigits0z(x,2)
31✔
303
    if n <= 24
31✔
304
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
26✔
305
    else
306
        y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit
5✔
307
        y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent)
5✔
308
        y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even
5✔
309
    end
310
    d = ((n+126) % UInt32) << 23
31✔
311
    reinterpret(Float32, s | d + y)
31✔
312
end
313

314
# TODO: optimize
315
Float16(x::UInt128) = convert(Float16, Float64(x))
24✔
316
Float16(x::Int128)  = convert(Float16, Float64(x))
28✔
317

318
Float16(x::Float32) = fptrunc(Float16, x)
6,478,428✔
319
Float16(x::Float64) = fptrunc(Float16, x)
288,683✔
320
Float32(x::Float64) = fptrunc(Float32, x)
458,005,338✔
321

322
Float32(x::Float16) = fpext(Float32, x)
27,515,907✔
323
Float64(x::Float32) = fpext(Float64, x)
478,669,965✔
324
Float64(x::Float16) = fpext(Float64, x)
3,138,015✔
325

326
AbstractFloat(x::Bool)    = Float64(x)
448,143✔
327
AbstractFloat(x::Int8)    = Float64(x)
86✔
328
AbstractFloat(x::Int16)   = Float64(x)
59✔
329
AbstractFloat(x::Int32)   = Float64(x)
68,375✔
330
AbstractFloat(x::Int64)   = Float64(x) # LOSSY
20,964,410✔
331
AbstractFloat(x::Int128)  = Float64(x) # LOSSY
1,417,595✔
332
AbstractFloat(x::UInt8)   = Float64(x)
12,290✔
333
AbstractFloat(x::UInt16)  = Float64(x)
45✔
334
AbstractFloat(x::UInt32)  = Float64(x)
45✔
335
AbstractFloat(x::UInt64)  = Float64(x) # LOSSY
1,600✔
336
AbstractFloat(x::UInt128) = Float64(x) # LOSSY
2,058✔
337

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

340
"""
341
    float(x)
342

343
Convert a number or array to a floating point data type.
344

345
See also: [`complex`](@ref), [`oftype`](@ref), [`convert`](@ref).
346

347
# Examples
348
```jldoctest
349
julia> float(1:1000)
350
1.0:1.0:1000.0
351

352
julia> float(typemax(Int32))
353
2.147483647e9
354
```
355
"""
356
float(x) = AbstractFloat(x)
40,956,497✔
357

358
"""
359
    float(T::Type)
360

361
Return an appropriate type to represent a value of type `T` as a floating point value.
362
Equivalent to `typeof(float(zero(T)))`.
363

364
# Examples
365
```jldoctest
366
julia> float(Complex{Int})
367
ComplexF64 (alias for Complex{Float64})
368

369
julia> float(Int)
370
Float64
371
```
372
"""
373
float(::Type{T}) where {T<:Number} = typeof(float(zero(T)))
3,763✔
374
float(::Type{T}) where {T<:AbstractFloat} = T
23,703✔
375
float(::Type{Union{}}, slurp...) = Union{}(0.0)
×
376

377
"""
378
    unsafe_trunc(T, x)
379

380
Return the nearest integral value of type `T` whose absolute value is
381
less than or equal to the absolute value of `x`. If the value is not representable by `T`,
382
an arbitrary value will be returned.
383
See also [`trunc`](@ref).
384

385
# Examples
386
```jldoctest
387
julia> unsafe_trunc(Int, -2.2)
388
-2
389

390
julia> unsafe_trunc(Int, NaN)
391
-9223372036854775808
392
```
393
"""
394
function unsafe_trunc end
395

396
for Ti in (Int8, Int16, Int32, Int64)
397
    @eval begin
398
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptosi($Ti, x)
46,607,867✔
399
    end
400
end
401
for Ti in (UInt8, UInt16, UInt32, UInt64)
402
    @eval begin
403
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptoui($Ti, x)
41,915,258✔
404
    end
405
end
406

407
function unsafe_trunc(::Type{UInt128}, x::Float64)
652,444✔
408
    xu = reinterpret(UInt64,x)
652,444✔
409
    k = Int(xu >> 52) & 0x07ff - 1075
652,444✔
410
    xu = (xu & 0x000f_ffff_ffff_ffff) | 0x0010_0000_0000_0000
652,444✔
411
    if k <= 0
652,444✔
412
        UInt128(xu >> -k)
651,407✔
413
    else
414
        UInt128(xu) << k
1,037✔
415
    end
416
end
417
function unsafe_trunc(::Type{Int128}, x::Float64)
651,398✔
418
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
651,422✔
419
end
420

421
function unsafe_trunc(::Type{UInt128}, x::Float32)
63✔
422
    xu = reinterpret(UInt32,x)
63✔
423
    k = Int(xu >> 23) & 0x00ff - 150
63✔
424
    xu = (xu & 0x007f_ffff) | 0x0080_0000
63✔
425
    if k <= 0
63✔
426
        UInt128(xu >> -k)
43✔
427
    else
428
        UInt128(xu) << k
20✔
429
    end
430
end
431
function unsafe_trunc(::Type{Int128}, x::Float32)
31✔
432
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
45✔
433
end
434

435
unsafe_trunc(::Type{UInt128}, x::Float16) = unsafe_trunc(UInt128, Float32(x))
18✔
436
unsafe_trunc(::Type{Int128}, x::Float16) = unsafe_trunc(Int128, Float32(x))
17✔
437

438
# matches convert methods
439
# also determines trunc, floor, ceil
440
round(::Type{Signed},   x::IEEEFloat, r::RoundingMode) = round(Int, x, r)
×
441
round(::Type{Unsigned}, x::IEEEFloat, r::RoundingMode) = round(UInt, x, r)
×
442
round(::Type{Integer},  x::IEEEFloat, r::RoundingMode) = round(Int, x, r)
559✔
443

444
round(x::IEEEFloat, ::RoundingMode{:ToZero})  = trunc_llvm(x)
35,310,265✔
445
round(x::IEEEFloat, ::RoundingMode{:Down})    = floor_llvm(x)
307,936✔
446
round(x::IEEEFloat, ::RoundingMode{:Up})      = ceil_llvm(x)
685,219✔
447
round(x::IEEEFloat, ::RoundingMode{:Nearest}) = rint_llvm(x)
11,065,192✔
448

449
## floating point promotions ##
450
promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
12,268,464✔
451
promote_rule(::Type{Float64}, ::Type{Float16}) = Float64
×
452
promote_rule(::Type{Float64}, ::Type{Float32}) = Float64
×
453

454
widen(::Type{Float16}) = Float32
11,747,104✔
455
widen(::Type{Float32}) = Float64
9,555,586✔
456

457
## floating point arithmetic ##
458
-(x::IEEEFloat) = neg_float(x)
414,676,235✔
459

460
+(x::T, y::T) where {T<:IEEEFloat} = add_float(x, y)
659,470,503✔
461
-(x::T, y::T) where {T<:IEEEFloat} = sub_float(x, y)
1,258,416,706✔
462
*(x::T, y::T) where {T<:IEEEFloat} = mul_float(x, y)
2,147,483,647✔
463
/(x::T, y::T) where {T<:IEEEFloat} = div_float(x, y)
892,799,292✔
464

465
muladd(x::T, y::T, z::T) where {T<:IEEEFloat} = muladd_float(x, y, z)
819,086,004✔
466

467
# TODO: faster floating point div?
468
# TODO: faster floating point fld?
469
# TODO: faster floating point mod?
470

471
function unbiased_exponent(x::T) where {T<:IEEEFloat}
1,506✔
472
    return (reinterpret(Unsigned, x) & exponent_mask(T)) >> significand_bits(T)
1,077,100✔
473
end
474

475
function explicit_mantissa_noinfnan(x::T) where {T<:IEEEFloat}
1,506✔
476
    m = mantissa(x)
1,077,100✔
477
    issubnormal(x) || (m |= significand_mask(T) + uinttype(T)(1))
2,154,160✔
478
    return m
1,077,100✔
479
end
480

481
function _to_float(number::U, ep) where {U<:Unsigned}
372✔
482
    F = floattype(U)
372✔
483
    S = signed(U)
372✔
484
    epint = unsafe_trunc(S,ep)
523,880✔
485
    lz::signed(U) = unsafe_trunc(S, Core.Intrinsics.ctlz_int(number) - U(exponent_bits(F)))
523,880✔
486
    number <<= lz
523,880✔
487
    epint -= lz
523,880✔
488
    bits = U(0)
372✔
489
    if epint >= 0
523,880✔
490
        bits = number & significand_mask(F)
523,864✔
491
        bits |= ((epint + S(1)) << significand_bits(F)) & exponent_mask(F)
523,864✔
492
    else
493
        bits = (number >> -epint) & significand_mask(F)
16✔
494
    end
495
    return reinterpret(F, bits)
523,880✔
496
end
497

498
@assume_effects :terminates_locally :nothrow function rem_internal(x::T, y::T) where {T<:IEEEFloat}
612,419✔
499
    xuint = reinterpret(Unsigned, x)
612,419✔
500
    yuint = reinterpret(Unsigned, y)
612,419✔
501
    if xuint <= yuint
612,419✔
502
        if xuint < yuint
73,869✔
503
            return x
66,139✔
504
        end
505
        return zero(T)
7,730✔
506
    end
507

508
    e_x = unbiased_exponent(x)
538,550✔
509
    e_y = unbiased_exponent(y)
538,550✔
510
    # Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
511
    if e_y > (significand_bits(T)) && (e_x - e_y) <= (exponent_bits(T))
538,550✔
512
        m_x = explicit_mantissa_noinfnan(x)
246,670✔
513
        m_y = explicit_mantissa_noinfnan(y)
246,670✔
514
        d = urem_int((m_x << (e_x - e_y)),  m_y)
123,335✔
515
        iszero(d) && return zero(T)
123,335✔
516
        return _to_float(d, e_y - uinttype(T)(1))
108,950✔
517
    end
518
    # Both are subnormals
519
    if e_x == 0 && e_y == 0
415,215✔
520
        return reinterpret(T, urem_int(xuint, yuint) & significand_mask(T))
×
521
    end
522

523
    m_x = explicit_mantissa_noinfnan(x)
830,418✔
524
    e_x -= uinttype(T)(1)
415,215✔
525
    m_y = explicit_mantissa_noinfnan(y)
830,402✔
526
    lz_m_y = uinttype(T)(exponent_bits(T))
44✔
527
    if e_y > 0
415,215✔
528
        e_y -= uinttype(T)(1)
415,191✔
529
    else
530
        m_y = mantissa(y)
24✔
531
        lz_m_y = Core.Intrinsics.ctlz_int(m_y)
24✔
532
    end
533

534
    tz_m_y = Core.Intrinsics.cttz_int(m_y)
415,215✔
535
    sides_zeroes_cnt = lz_m_y + tz_m_y
415,215✔
536

537
    # n>0
538
    exp_diff = e_x - e_y
415,215✔
539
    # Shift hy right until the end or n = 0
540
    right_shift = min(exp_diff, tz_m_y)
415,215✔
541
    m_y >>= right_shift
415,215✔
542
    exp_diff -= right_shift
415,215✔
543
    e_y += right_shift
415,215✔
544
    # Shift hx left until the end or n = 0
545
    left_shift = min(exp_diff, uinttype(T)(exponent_bits(T)))
415,215✔
546
    m_x <<= left_shift
415,215✔
547
    exp_diff -= left_shift
415,215✔
548

549
    m_x = urem_int(m_x, m_y)
415,215✔
550
    iszero(m_x) && return zero(T)
415,215✔
551
    iszero(exp_diff) && return _to_float(m_x, e_y)
414,930✔
552

553
    while exp_diff > sides_zeroes_cnt
402,804✔
554
        exp_diff -= sides_zeroes_cnt
1,215✔
555
        m_x <<= sides_zeroes_cnt
1,215✔
556
        m_x = urem_int(m_x, m_y)
1,215✔
557
    end
1,215✔
558
    m_x <<= exp_diff
401,589✔
559
    m_x = urem_int(m_x, m_y)
401,589✔
560
    return _to_float(m_x, e_y)
401,597✔
561
end
562

563
function rem(x::T, y::T) where {T<:IEEEFloat}
21,415✔
564
    if isfinite(x) && !iszero(x) && isfinite(y) && !iszero(y)
621,329✔
565
        return copysign(rem_internal(abs(x), abs(y)), x)
612,437✔
566
    elseif isinf(x) || isnan(y) || iszero(y)  # y can still be Inf
17,787✔
567
        return T(NaN)
41✔
568
    else
569
        return x
8,859✔
570
    end
571
end
572

573
function mod(x::T, y::T) where {T<:AbstractFloat}
54,674✔
574
    r = rem(x,y)
94,939✔
575
    if r == 0
90,510✔
576
        copysign(r,y)
15,760✔
577
    elseif (r > 0) ⊻ (y > 0)
74,750✔
578
        r+y
26,540✔
579
    else
580
        r
48,210✔
581
    end
582
end
583

584
## floating point comparisons ##
585
==(x::T, y::T) where {T<:IEEEFloat} = eq_float(x, y)
438,429,142✔
586
!=(x::T, y::T) where {T<:IEEEFloat} = ne_float(x, y)
2,147,483,647✔
587
<( x::T, y::T) where {T<:IEEEFloat} = lt_float(x, y)
181,994,032✔
588
<=(x::T, y::T) where {T<:IEEEFloat} = le_float(x, y)
143,623,668✔
589

590
isequal(x::T, y::T) where {T<:IEEEFloat} = fpiseq(x, y)
3,510,348✔
591

592
# interpret as sign-magnitude integer
593
@inline function _fpint(x)
9,398✔
594
    IntT = inttype(typeof(x))
9,352✔
595
    ix = reinterpret(IntT, x)
1,132,606✔
596
    return ifelse(ix < zero(IntT), ix ⊻ typemax(IntT), ix)
1,132,606✔
597
end
598

599
@inline function isless(a::T, b::T) where T<:IEEEFloat
435,925✔
600
    (isnan(a) || isnan(b)) && return !isnan(a)
1,362,104✔
601

602
    return _fpint(a) < _fpint(b)
680,848✔
603
end
604

605
# Exact Float (Tf) vs Integer (Ti) comparisons
606
# Assumes:
607
# - typemax(Ti) == 2^n-1
608
# - typemax(Ti) can't be exactly represented by Tf:
609
#   => Tf(typemax(Ti)) == 2^n or Inf
610
# - typemin(Ti) can be exactly represented by Tf
611
#
612
# 1. convert y::Ti to float fy::Tf
613
# 2. perform Tf comparison x vs fy
614
# 3. if x == fy, check if (1) resulted in rounding:
615
#  a. convert fy back to Ti and compare with original y
616
#  b. unsafe_convert undefined behaviour if fy == Tf(typemax(Ti))
617
#     (but consequently x == fy > y)
618
for Ti in (Int64,UInt64,Int128,UInt128)
619
    for Tf in (Float32,Float64)
620
        @eval begin
621
            function ==(x::$Tf, y::$Ti)
7,809,188✔
622
                fy = ($Tf)(y)
8,785,390✔
623
                (x == fy) & (fy != $(Tf(typemax(Ti)))) & (y == unsafe_trunc($Ti,fy))
20,299,590✔
624
            end
625
            ==(y::$Ti, x::$Tf) = x==y
753,841✔
626

627
            function <(x::$Ti, y::$Tf)
41,854,782✔
628
                fx = ($Tf)(x)
41,856,831✔
629
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
42,005,243✔
630
            end
631
            function <=(x::$Ti, y::$Tf)
101,848✔
632
                fx = ($Tf)(x)
289,437✔
633
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
1,242,042✔
634
            end
635

636
            function <(x::$Tf, y::$Ti)
631,181✔
637
                fy = ($Tf)(y)
824,286✔
638
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
1,565,919✔
639
            end
640
            function <=(x::$Tf, y::$Ti)
23,154✔
641
                fy = ($Tf)(y)
23,154✔
642
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
23,782✔
643
            end
644
        end
645
    end
646
end
647
for op in (:(==), :<, :<=)
648
    @eval begin
649
        ($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
2,179,618✔
650
        ($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
16,650✔
651

652
        ($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
246,181✔
653
        ($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))
79✔
654

655
        ($op)(x::Float16, y::Union{Int16,UInt16}) = ($op)(Float32(x), Float32(y))
10✔
656
        ($op)(x::Union{Int16,UInt16}, y::Float16) = ($op)(Float32(x), Float32(y))
12✔
657
    end
658
end
659

660

661
abs(x::IEEEFloat) = abs_float(x)
185,932,848✔
662

663
"""
664
    isnan(f) -> Bool
665

666
Test whether a number value is a NaN, an indeterminate value which is neither an infinity
667
nor a finite number ("not a number").
668

669
See also: [`iszero`](@ref), [`isone`](@ref), [`isinf`](@ref), [`ismissing`](@ref).
670
"""
671
isnan(x::AbstractFloat) = (x != x)::Bool
2,147,483,647✔
672
isnan(x::Number) = false
88,826✔
673

674
isfinite(x::AbstractFloat) = !isnan(x - x)
1,017,772,960✔
675
isfinite(x::Real) = decompose(x)[3] != 0
69,244✔
676
isfinite(x::Integer) = true
122,394✔
677

678
"""
679
    isinf(f) -> Bool
680

681
Test whether a number is infinite.
682

683
See also: [`Inf`](@ref), [`iszero`](@ref), [`isfinite`](@ref), [`isnan`](@ref).
684
"""
685
isinf(x::Real) = !isnan(x) & !isfinite(x)
177,874✔
686
isinf(x::IEEEFloat) = abs(x) === oftype(x, Inf)
36,706,994✔
687

688
const hx_NaN = hash_uint64(reinterpret(UInt64, NaN))
689
function hash(x::Float64, h::UInt)
781,519✔
690
    # see comments on trunc and hash(Real, UInt)
691
    if typemin(Int64) <= x < typemax(Int64)
781,921✔
692
        xi = fptosi(Int64, x)
545,224✔
693
        if isequal(xi, x)
545,224✔
694
            return hash(xi, h)
781,771✔
695
        end
696
    elseif typemin(UInt64) <= x < typemax(UInt64)
147✔
697
        xu = fptoui(UInt64, x)
94✔
698
        if isequal(xu, x)
94✔
699
            return hash(xu, h)
94✔
700
        end
701
    elseif isnan(x)
53✔
702
        return hx_NaN ⊻ h # NaN does not have a stable bit pattern
46✔
703
    end
704
    return hash_uint64(bitcast(UInt64, x)) - 3h
523,372✔
705
end
706

707
hash(x::Float32, h::UInt) = hash(Float64(x), h)
6,375✔
708

709
function hash(x::Float16, h::UInt)
6✔
710
    # see comments on trunc and hash(Real, UInt)
711
    if isfinite(x) # all finite Float16 fit in Int64
6✔
712
        xi = fptosi(Int64, x)
6✔
713
        if isequal(xi, x)
6✔
714
            return hash(xi, h)
6✔
715
        end
716
    elseif isnan(x)
×
717
        return hx_NaN ⊻ h # NaN does not have a stable bit pattern
×
718
    end
719
    return hash_uint64(bitcast(UInt64, Float64(x))) - 3h
×
720
end
721

722
## generic hashing for rational values ##
723
function hash(x::Real, h::UInt)
240,905✔
724
    # decompose x as num*2^pow/den
725
    num, pow, den = decompose(x)
241,037✔
726

727
    # handle special values
728
    num == 0 && den == 0 && return hash(NaN, h)
240,905✔
729
    num == 0 && return hash(ifelse(den > 0, 0.0, -0.0), h)
240,905✔
730
    den == 0 && return hash(ifelse(num > 0, Inf, -Inf), h)
4,179✔
731

732
    # normalize decomposition
733
    if den < 0
4,179✔
734
        num = -num
832✔
735
        den = -den
832✔
736
    end
737
    num_z = trailing_zeros(num)
4,179✔
738
    num >>= num_z
6,517✔
739
    den_z = trailing_zeros(den)
4,179✔
740
    den >>= den_z
4,182✔
741
    pow += num_z - den_z
4,179✔
742
    # If the real can be represented as an Int64, UInt64, or Float64, hash as those types.
743
    # To be an Integer the denominator must be 1 and the power must be non-negative.
744
    if den == 1
4,179✔
745
        # left = ceil(log2(num*2^pow))
746
        left = top_set_bit(abs(num)) + pow
7,317✔
747
        # 2^-1074 is the minimum Float64 so if the power is smaller, not a Float64
748
        if -1074 <= pow
4,176✔
749
            if 0 <= pow # if pow is non-negative, it is an integer
4,176✔
750
                left <= 63 && return hash(Int64(num) << Int(pow), h)
4,173✔
751
                left <= 64 && !signbit(num) && return hash(UInt64(num) << Int(pow), h)
175✔
752
            end # typemin(Int64) handled by Float64 case
753
            # 2^1024 is the maximum Float64 so if the power is greater, not a Float64
754
            # Float64s only have 53 mantisa bits (including implicit bit)
755
            left <= 1024 && left - pow <= 53 && return hash(ldexp(Float64(num), pow), h)
110✔
756
        end
757
    else
758
        h = hash_integer(den, h)
3✔
759
    end
760
    # handle generic rational values
761
    h = hash_integer(pow, h)
4✔
762
    h = hash_integer(num, h)
7✔
763
    return h
4✔
764
end
765

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

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

773
    num*2^pow/den
774

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

780
Special values:
781

782
 - `x` is zero: `num` should be zero and `den` should have the same sign as `x`
783
 - `x` is infinite: `den` should be zero and `num` should have the same sign as `x`
784
 - `x` is not a number: `num` and `den` should both be zero
785
=#
786

787
decompose(x::Integer) = x, 0, 1
237,069✔
788

789
function decompose(x::Float16)::NTuple{3,Int}
×
790
    isnan(x) && return 0, 0, 0
×
791
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
×
792
    n = reinterpret(UInt16, x)
×
793
    s = (n & 0x03ff) % Int16
×
794
    e = ((n & 0x7c00) >> 10) % Int
×
795
    s |= Int16(e != 0) << 10
×
796
    d = ifelse(signbit(x), -1, 1)
×
797
    s, e - 25 + (e == 0), d
×
798
end
799

800
function decompose(x::Float32)::NTuple{3,Int}
74✔
801
    isnan(x) && return 0, 0, 0
74✔
802
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
74✔
803
    n = reinterpret(UInt32, x)
66✔
804
    s = (n & 0x007fffff) % Int32
66✔
805
    e = ((n & 0x7f800000) >> 23) % Int
66✔
806
    s |= Int32(e != 0) << 23
66✔
807
    d = ifelse(signbit(x), -1, 1)
66✔
808
    s, e - 150 + (e == 0), d
66✔
809
end
810

811
function decompose(x::Float64)::Tuple{Int64, Int, Int}
18,730✔
812
    isnan(x) && return 0, 0, 0
18,730✔
813
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
18,730✔
814
    n = reinterpret(UInt64, x)
18,723✔
815
    s = (n & 0x000fffffffffffff) % Int64
18,723✔
816
    e = ((n & 0x7ff0000000000000) >> 52) % Int
18,723✔
817
    s |= Int64(e != 0) << 52
18,723✔
818
    d = ifelse(signbit(x), -1, 1)
18,723✔
819
    s, e - 1075 + (e == 0), d
18,723✔
820
end
821

822

823
"""
824
    precision(num::AbstractFloat; base::Integer=2)
825
    precision(T::Type; base::Integer=2)
826

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

831
If `base` is specified, then it returns the maximum corresponding
832
number of significand digits in that base.
833

834
!!! compat "Julia 1.8"
835
    The `base` keyword requires at least Julia 1.8.
836
"""
837
function precision end
838

839
_precision(::Type{Float16}) = 11
×
840
_precision(::Type{Float32}) = 24
×
841
_precision(::Type{Float64}) = 53
×
842
function _precision(x, base::Integer=2)
13,563,106✔
843
    base > 1 || throw(DomainError(base, "`base` cannot be less than 2."))
13,563,110✔
844
    p = _precision(x)
27,090,906✔
845
    return base == 2 ? Int(p) : floor(Int, p / log2(base))
13,563,111✔
846
end
847
precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, base)
27,055,686✔
848
precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base)
134✔
849

850

851
"""
852
    nextfloat(x::AbstractFloat, n::Integer)
853

854
The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n`
855
applications of [`prevfloat`](@ref) if `n < 0`.
856
"""
857
function nextfloat(f::IEEEFloat, d::Integer)
601,257,101✔
858
    F = typeof(f)
601,125,672✔
859
    fumax = reinterpret(Unsigned, F(Inf))
601,125,672✔
860
    U = typeof(fumax)
601,125,672✔
861

862
    isnan(f) && return f
1,201,385,254✔
863
    fi = reinterpret(Signed, f)
1,201,385,252✔
864
    fneg = fi < 0
1,201,385,252✔
865
    fu = unsigned(fi & typemax(fi))
1,201,385,252✔
866

867
    dneg = d < 0
601,125,867✔
868
    da = uabs(d)
601,125,868✔
869
    if da > typemax(U)
1,201,385,252✔
870
        fneg = dneg
4✔
871
        fu = fumax
4✔
872
    else
873
        du = da % U
601,125,667✔
874
        if fneg ⊻ dneg
1,201,385,248✔
875
            if du > fu
764,716✔
876
                fu = min(fumax, du - fu)
101✔
877
                fneg = !fneg
101✔
878
            else
879
                fu = fu - du
1,398,396✔
880
            end
881
        else
882
            if fumax - fu < du
1,200,620,532✔
883
                fu = fumax
42✔
884
            else
885
                fu = fu + du
1,200,620,490✔
886
            end
887
        end
888
    end
889
    if fneg
1,201,385,252✔
890
        fu |= sign_mask(F)
536,887✔
891
    end
892
    reinterpret(F, fu)
1,201,385,252✔
893
end
894

895
"""
896
    nextfloat(x::AbstractFloat)
897

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

901
See also: [`prevfloat`](@ref), [`eps`](@ref), [`issubnormal`](@ref).
902
"""
903
nextfloat(x::AbstractFloat) = nextfloat(x,1)
2,147,483,647✔
904

905
"""
906
    prevfloat(x::AbstractFloat, n::Integer)
907

908
The result of `n` iterative applications of `prevfloat` to `x` if `n >= 0`, or `-n`
909
applications of [`nextfloat`](@ref) if `n < 0`.
910
"""
911
prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d)
9✔
912

913
"""
914
    prevfloat(x::AbstractFloat)
915

916
Return the largest floating point number `y` of the same type as `x` such `y < x`. If no
917
such `y` exists (e.g. if `x` is `-Inf` or `NaN`), then return `x`.
918
"""
919
prevfloat(x::AbstractFloat) = nextfloat(x,-1)
1,087,846✔
920

921
for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
922
    for Tf in (Float16, Float32, Float64)
923
        if Ti <: Unsigned || sizeof(Ti) < sizeof(Tf)
924
            # Here `Tf(typemin(Ti))-1` is exact, so we can compare the lower-bound
925
            # directly. `Tf(typemax(Ti))+1` is either always exactly representable, or
926
            # rounded to `Inf` (e.g. when `Ti==UInt128 && Tf==Float32`).
927
            @eval begin
928
                function round(::Type{$Ti},x::$Tf,::RoundingMode{:ToZero})
1,113✔
929
                    if $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf))
1,113✔
930
                        return unsafe_trunc($Ti,x)
1,113✔
931
                    else
932
                        throw(InexactError(:round, $Ti, x, RoundToZero))
×
933
                    end
934
                end
935
                function (::Type{$Ti})(x::$Tf)
11,861✔
936
                    # When typemax(Ti) is not representable by Tf but typemax(Ti) + 1 is,
937
                    # then < Tf(typemax(Ti) + 1) is stricter than <= Tf(typemax(Ti)). Using
938
                    # the former causes us to throw on UInt64(Float64(typemax(UInt64))+1)
939
                    if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))+one(Tf))) && isinteger(x)
420,005✔
940
                        return unsafe_trunc($Ti,x)
420,656✔
941
                    else
942
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
356✔
943
                    end
944
                end
945
            end
946
        else
947
            # Here `eps(Tf(typemin(Ti))) > 1`, so the only value which can be truncated to
948
            # `Tf(typemin(Ti)` is itself. Similarly, `Tf(typemax(Ti))` is inexact and will
949
            # be rounded up. This assumes that `Tf(typemin(Ti)) > -Inf`, which is true for
950
            # these types, but not for `Float16` or larger integer types.
951
            @eval begin
952
                function round(::Type{$Ti},x::$Tf,::RoundingMode{:ToZero})
22,067,392✔
953
                    if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
22,067,392✔
954
                        return unsafe_trunc($Ti,x)
22,067,392✔
955
                    else
956
                        throw(InexactError(:round, $Ti, x, RoundToZero))
×
957
                    end
958
                end
959
                function (::Type{$Ti})(x::$Tf)
25,380,110✔
960
                    if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) && isinteger(x)
34,736,066✔
961
                        return unsafe_trunc($Ti,x)
34,735,816✔
962
                    else
963
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
238✔
964
                    end
965
                end
966
            end
967
        end
968
    end
969
end
970

971
"""
972
    issubnormal(f) -> Bool
973

974
Test whether a floating point number is subnormal.
975

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

979
# Examples
980
```jldoctest
981
julia> floatmin(Float32)
982
1.1754944f-38
983

984
julia> issubnormal(1.0f-37)
985
false
986

987
julia> issubnormal(1.0f-38)
988
true
989
```
990
"""
991
function issubnormal(x::T) where {T<:IEEEFloat}
3,332,267✔
992
    y = reinterpret(Unsigned, x)
4,930,508✔
993
    (y & exponent_mask(T) == 0) & (y & significand_mask(T) != 0)
4,930,508✔
994
end
995

996
ispow2(x::AbstractFloat) = !iszero(x) && frexp(x)[1] == 0.5
42✔
997
iseven(x::AbstractFloat) = isinteger(x) && (abs(x) > maxintfloat(x) || iseven(Integer(x)))
52✔
998
isodd(x::AbstractFloat) = isinteger(x) && abs(x) ≤ maxintfloat(x) && isodd(Integer(x))
28✔
999

1000
@eval begin
1001
    typemin(::Type{Float16}) = $(bitcast(Float16, 0xfc00))
20✔
1002
    typemax(::Type{Float16}) = $(Inf16)
21✔
1003
    typemin(::Type{Float32}) = $(-Inf32)
20✔
1004
    typemax(::Type{Float32}) = $(Inf32)
247✔
1005
    typemin(::Type{Float64}) = $(-Inf64)
65✔
1006
    typemax(::Type{Float64}) = $(Inf64)
1,309✔
1007
    typemin(x::T) where {T<:Real} = typemin(T)
×
1008
    typemax(x::T) where {T<:Real} = typemax(T)
601,317,635✔
1009

1010
    floatmin(::Type{Float16}) = $(bitcast(Float16, 0x0400))
493,613✔
1011
    floatmin(::Type{Float32}) = $(bitcast(Float32, 0x00800000))
161,226✔
1012
    floatmin(::Type{Float64}) = $(bitcast(Float64, 0x0010000000000000))
×
1013
    floatmax(::Type{Float16}) = $(bitcast(Float16, 0x7bff))
30,844✔
1014
    floatmax(::Type{Float32}) = $(bitcast(Float32, 0x7f7fffff))
130,553✔
1015
    floatmax(::Type{Float64}) = $(bitcast(Float64, 0x7fefffffffffffff))
1,026,826✔
1016

1017
    eps(x::AbstractFloat) = isfinite(x) ? abs(x) >= floatmin(x) ? ldexp(eps(typeof(x)), exponent(x)) : nextfloat(zero(x)) : oftype(x, NaN)
1,374,675✔
1018
    eps(::Type{Float16}) = $(bitcast(Float16, 0x1400))
399,672✔
1019
    eps(::Type{Float32}) = $(bitcast(Float32, 0x34000000))
621,552✔
1020
    eps(::Type{Float64}) = $(bitcast(Float64, 0x3cb0000000000000))
1✔
1021
    eps() = eps(Float64)
555✔
1022
end
1023

1024
"""
1025
    floatmin(T = Float64)
1026

1027
Return the smallest positive normal number representable by the floating-point
1028
type `T`.
1029

1030
# Examples
1031
```jldoctest
1032
julia> floatmin(Float16)
1033
Float16(6.104e-5)
1034

1035
julia> floatmin(Float32)
1036
1.1754944f-38
1037

1038
julia> floatmin()
1039
2.2250738585072014e-308
1040
```
1041
"""
1042
floatmin(x::T) where {T<:AbstractFloat} = floatmin(T)
619,671✔
1043

1044
"""
1045
    floatmax(T = Float64)
1046

1047
Return the largest finite number representable by the floating-point type `T`.
1048

1049
See also: [`typemax`](@ref), [`floatmin`](@ref), [`eps`](@ref).
1050

1051
# Examples
1052
```jldoctest
1053
julia> floatmax(Float16)
1054
Float16(6.55e4)
1055

1056
julia> floatmax(Float32)
1057
3.4028235f38
1058

1059
julia> floatmax()
1060
1.7976931348623157e308
1061

1062
julia> typemax(Float64)
1063
Inf
1064
```
1065
"""
1066
floatmax(x::T) where {T<:AbstractFloat} = floatmax(T)
282,878✔
1067

1068
floatmin() = floatmin(Float64)
16✔
1069
floatmax() = floatmax(Float64)
19✔
1070

1071
"""
1072
    eps(::Type{T}) where T<:AbstractFloat
1073
    eps()
1074

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

1080
# Examples
1081
```jldoctest
1082
julia> eps()
1083
2.220446049250313e-16
1084

1085
julia> eps(Float32)
1086
1.1920929f-7
1087

1088
julia> 1.0 + eps()
1089
1.0000000000000002
1090

1091
julia> 1.0 + eps()/2
1092
1.0
1093
```
1094
"""
1095
eps(::Type{<:AbstractFloat})
1096

1097
"""
1098
    eps(x::AbstractFloat)
1099

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

1104
    eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
1105

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

1110
The rationale for this behavior is that `eps` bounds the floating point rounding
1111
error. Under the default `RoundNearest` rounding mode, if ``y`` is a real number and ``x``
1112
is the nearest floating point number to ``y``, then
1113

1114
```math
1115
|y-x| \\leq \\operatorname{eps}(x)/2.
1116
```
1117

1118
See also: [`nextfloat`](@ref), [`issubnormal`](@ref), [`floatmax`](@ref).
1119

1120
# Examples
1121
```jldoctest
1122
julia> eps(1.0)
1123
2.220446049250313e-16
1124

1125
julia> eps(prevfloat(2.0))
1126
2.220446049250313e-16
1127

1128
julia> eps(2.0)
1129
4.440892098500626e-16
1130

1131
julia> x = prevfloat(Inf)      # largest finite Float64
1132
1.7976931348623157e308
1133

1134
julia> x + eps(x)/2            # rounds up
1135
Inf
1136

1137
julia> x + prevfloat(eps(x)/2) # rounds down
1138
1.7976931348623157e308
1139
```
1140
"""
1141
eps(::AbstractFloat)
1142

1143

1144
## byte order swaps for arbitrary-endianness serialization/deserialization ##
1145
bswap(x::IEEEFloat) = bswap_int(x)
7✔
1146

1147
# integer size of float
1148
uinttype(::Type{Float64}) = UInt64
×
1149
uinttype(::Type{Float32}) = UInt32
×
1150
uinttype(::Type{Float16}) = UInt16
×
1151
inttype(::Type{Float64}) = Int64
×
1152
inttype(::Type{Float32}) = Int32
9,256✔
1153
inttype(::Type{Float16}) = Int16
96✔
1154
# float size of integer
1155
floattype(::Type{UInt64}) = Float64
×
1156
floattype(::Type{UInt32}) = Float32
363✔
1157
floattype(::Type{UInt16}) = Float16
9✔
1158
floattype(::Type{Int64}) = Float64
×
1159
floattype(::Type{Int32}) = Float32
×
1160
floattype(::Type{Int16}) = Float16
×
1161

1162

1163
## Array operations on floating point numbers ##
1164

1165
float(A::AbstractArray{<:AbstractFloat}) = A
×
1166

1167
function float(A::AbstractArray{T}) where T
47✔
1168
    if !isconcretetype(T)
47✔
1169
        error("`float` not defined on abstractly-typed arrays; please convert to a more specific type")
×
1170
    end
1171
    convert(AbstractArray{typeof(float(zero(T)))}, A)
52✔
1172
end
1173

1174
float(r::StepRange) = float(r.start):float(r.step):float(last(r))
49✔
1175
float(r::UnitRange) = float(r.start):float(last(r))
49✔
1176
float(r::StepRangeLen{T}) where {T} =
4✔
1177
    StepRangeLen{typeof(float(T(r.ref)))}(float(r.ref), float(r.step), length(r), r.offset)
1178
function float(r::LinRange)
×
1179
    LinRange(float(r.start), float(r.stop), length(r))
×
1180
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