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

JuliaLang / julia / #38162

06 Aug 2025 08:25PM UTC coverage: 25.688% (-43.6%) from 69.336%
#38162

push

local

web-flow
fix runtime cglobal builtin function implementation (#59210)

This had failed to be updated for the LazyLibrary changes to codegen.

12976 of 50513 relevant lines covered (25.69%)

676965.51 hits per line

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

13.07
/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
import Core: Float16, Float32, Float64, AbstractFloat
6
import Core: Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128
7

8
## floating point traits ##
9

10
"""
11
    Inf16
12

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

19
A not-a-number value of type [`Float16`](@ref).
20

21
See also: [`NaN`](@ref).
22
"""
23
const NaN16 = bitcast(Float16, 0x7e00)
24
"""
25
    Inf32
26

27
Positive infinity of type [`Float32`](@ref).
28
"""
29
const Inf32 = bitcast(Float32, 0x7f800000)
30
"""
31
    NaN32
32

33
A not-a-number value of type [`Float32`](@ref).
34

35
See also: [`NaN`](@ref).
36
"""
37
const NaN32 = bitcast(Float32, 0x7fc00000)
38
const Inf64 = bitcast(Float64, 0x7ff0000000000000)
39
const NaN64 = bitcast(Float64, 0x7ff8000000000000)
40

41
const Inf = Inf64
42
"""
43
    Inf, Inf64
44

45
Positive infinity of type [`Float64`](@ref).
46

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

49
# Examples
50
```jldoctest
51
julia> π/0
52
Inf
53

54
julia> +1.0 / -0.0
55
-Inf
56

57
julia> ℯ^-Inf
58
0.0
59
```
60
"""
61
Inf, Inf64
62

63
const NaN = NaN64
64
"""
65
    NaN, NaN64
66

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

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

71
# Examples
72
```jldoctest
73
julia> 0/0
74
NaN
75

76
julia> Inf - Inf
77
NaN
78

79
julia> NaN == NaN, isequal(NaN, NaN), isnan(NaN)
80
(false, true, true)
81
```
82

83
!!! note
84
    Always use [`isnan`](@ref) or [`isequal`](@ref) for checking for `NaN`.
85
    Using `x === NaN` may give unexpected results:
86
    ```jldoctest
87
    julia> reinterpret(UInt32, NaN32)
88
    0x7fc00000
89

90
    julia> NaN32p1 = reinterpret(Float32, 0x7fc00001)
91
    NaN32
92

93
    julia> NaN32p1 === NaN32, isequal(NaN32p1, NaN32), isnan(NaN32p1)
94
    (false, true, true)
95
    ```
96
"""
97
NaN, NaN64
98

99
# bit patterns
100
reinterpret(::Type{Unsigned}, x::Float64) = reinterpret(UInt64, x)
×
101
reinterpret(::Type{Unsigned}, x::Float32) = reinterpret(UInt32, x)
×
102
reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16, x)
×
103
reinterpret(::Type{Signed}, x::Float64) = reinterpret(Int64, x)
2✔
104
reinterpret(::Type{Signed}, x::Float32) = reinterpret(Int32, x)
×
105
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16, x)
×
106

107
sign_mask(::Type{Float64}) =        0x8000_0000_0000_0000
×
108
exponent_mask(::Type{Float64}) =    0x7ff0_0000_0000_0000
×
109
exponent_one(::Type{Float64}) =     0x3ff0_0000_0000_0000
×
110
exponent_half(::Type{Float64}) =    0x3fe0_0000_0000_0000
×
111
significand_mask(::Type{Float64}) = 0x000f_ffff_ffff_ffff
×
112

113
sign_mask(::Type{Float32}) =        0x8000_0000
×
114
exponent_mask(::Type{Float32}) =    0x7f80_0000
×
115
exponent_one(::Type{Float32}) =     0x3f80_0000
×
116
exponent_half(::Type{Float32}) =    0x3f00_0000
×
117
significand_mask(::Type{Float32}) = 0x007f_ffff
×
118

119
sign_mask(::Type{Float16}) =        0x8000
×
120
exponent_mask(::Type{Float16}) =    0x7c00
×
121
exponent_one(::Type{Float16}) =     0x3c00
×
122
exponent_half(::Type{Float16}) =    0x3800
×
123
significand_mask(::Type{Float16}) = 0x03ff
×
124

125
mantissa(x::T) where {T} = reinterpret(Unsigned, x) & significand_mask(T)
×
126

127
for T in (Float16, Float32, Float64)
128
    sb = trailing_ones(significand_mask(T))
129
    em = exponent_mask(T)
130
    eb = Int(exponent_one(T) >> sb)
131
    @eval significand_bits(::Type{$T}) = $(sb)
×
132
    @eval exponent_bits(::Type{$T}) = $(sizeof(T)*8 - sb - 1)
×
133
    @eval exponent_bias(::Type{$T}) = $(eb)
×
134
    # maximum float exponent
135
    @eval exponent_max(::Type{$T}) = $(Int(em >> sb) - eb - 1)
×
136
    # maximum float exponent without bias
137
    @eval exponent_raw_max(::Type{$T}) = $(Int(em >> sb))
×
138
end
139

140
"""
141
    exponent_max(T)
142

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

145
# Examples
146
```jldoctest
147
julia> Base.exponent_max(Float64)
148
1023
149
```
150

151
Note, `exponent_max(T) + 1` is a possible value of the exponent field
152
with bias, which might be used as sentinel value for `Inf` or `NaN`.
153
"""
154
function exponent_max end
155

156
"""
157
    exponent_raw_max(T)
158

159
Maximum value of the [`exponent`](@ref) field for a floating point number of type `T` without bias,
160
i.e. the maximum integer value representable by [`exponent_bits(T)`](@ref) bits.
161
"""
162
function exponent_raw_max end
163

164
"""
165
IEEE 754 definition of the minimum exponent.
166
"""
167
ieee754_exponent_min(::Type{T}) where {T<:IEEEFloat} = Int(1 - exponent_max(T))::Int
×
168

169
exponent_min(::Type{Float16}) = ieee754_exponent_min(Float16)
×
170
exponent_min(::Type{Float32}) = ieee754_exponent_min(Float32)
×
171
exponent_min(::Type{Float64}) = ieee754_exponent_min(Float64)
×
172

173
function ieee754_representation(
×
174
    ::Type{F}, sign_bit::Bool, exponent_field::Integer, significand_field::Integer
175
) where {F<:IEEEFloat}
176
    T = uinttype(F)
×
177
    ret::T = sign_bit
×
178
    ret <<= exponent_bits(F)
×
179
    ret |= exponent_field
×
180
    ret <<= significand_bits(F)
×
181
    ret |= significand_field
×
182
end
183

184
# ±floatmax(T)
185
function ieee754_representation(
×
186
    ::Type{F}, sign_bit::Bool, ::Val{:omega}
187
) where {F<:IEEEFloat}
188
    ieee754_representation(F, sign_bit, exponent_raw_max(F) - 1, significand_mask(F))
×
189
end
190

191
# NaN or an infinity
192
function ieee754_representation(
×
193
    ::Type{F}, sign_bit::Bool, significand_field::Integer, ::Val{:nan}
194
) where {F<:IEEEFloat}
195
    ieee754_representation(F, sign_bit, exponent_raw_max(F), significand_field)
×
196
end
197

198
# NaN with default payload
199
function ieee754_representation(
×
200
    ::Type{F}, sign_bit::Bool, ::Val{:nan}
201
) where {F<:IEEEFloat}
202
    ieee754_representation(F, sign_bit, one(uinttype(F)) << (significand_bits(F) - 1), Val(:nan))
×
203
end
204

205
# Infinity
206
function ieee754_representation(
×
207
    ::Type{F}, sign_bit::Bool, ::Val{:inf}
208
) where {F<:IEEEFloat}
209
    ieee754_representation(F, sign_bit, false, Val(:nan))
×
210
end
211

212
# Subnormal or zero
213
function ieee754_representation(
×
214
    ::Type{F}, sign_bit::Bool, significand_field::Integer, ::Val{:subnormal}
215
) where {F<:IEEEFloat}
216
    ieee754_representation(F, sign_bit, false, significand_field)
×
217
end
218

219
# Zero
220
function ieee754_representation(
×
221
    ::Type{F}, sign_bit::Bool, ::Val{:zero}
222
) where {F<:IEEEFloat}
223
    ieee754_representation(F, sign_bit, false, Val(:subnormal))
×
224
end
225

226
"""
227
    uabs(x::Integer)
228

229
Return the absolute value of `x`, possibly returning a different type should the
230
operation be susceptible to overflow. This typically arises when `x` is a two's complement
231
signed integer, so that `abs(typemin(x)) == typemin(x) < 0`, in which case the result of
232
`uabs(x)` will be an unsigned integer of the same size.
233
"""
234
uabs(x::Integer) = abs(x)
×
235
uabs(x::BitSigned) = unsigned(abs(x))
×
236

237
## conversions to floating-point ##
238

239
# TODO: deprecate in 2.0
240
Float16(x::Integer) = convert(Float16, convert(Float32, x)::Float32)
×
241

242
for t1 in (Float16, Float32, Float64)
243
    for st in (Int8, Int16, Int32, Int64)
244
        @eval begin
245
            (::Type{$t1})(x::($st)) = sitofp($t1, x)
5,266✔
246
            promote_rule(::Type{$t1}, ::Type{$st}) = $t1
×
247
        end
248
    end
249
    for ut in (Bool, UInt8, UInt16, UInt32, UInt64)
250
        @eval begin
251
            (::Type{$t1})(x::($ut)) = uitofp($t1, x)
397,084✔
252
            promote_rule(::Type{$t1}, ::Type{$ut}) = $t1
×
253
        end
254
    end
255
end
256

257
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
×
258
promote_rule(::Type{Float64}, ::Type{Int128}) = Float64
×
259
promote_rule(::Type{Float32}, ::Type{UInt128}) = Float32
×
260
promote_rule(::Type{Float32}, ::Type{Int128}) = Float32
×
261
promote_rule(::Type{Float16}, ::Type{UInt128}) = Float16
×
262
promote_rule(::Type{Float16}, ::Type{Int128}) = Float16
×
263

264
function Float64(x::UInt128)
×
265
    if x < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
×
266
        low_exp = 0x1p52
×
267
        high_exp = 0x1p104
×
268
        low_bits = (x % UInt64) & Base.significand_mask(Float64)
×
269
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
×
270
        high_bits = ((x >> 52) % UInt64)
×
271
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
×
272
        low_value + high_value
×
273
    else # Large enough that low bits only affect rounding, pack low bits
274
        low_exp = 0x1p76
×
275
        high_exp = 0x1p128
×
276
        low_bits = ((x >> 12) % UInt64) >> 12 | (x % UInt64) & 0xFFFFFF
×
277
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
×
278
        high_bits = ((x >> 76) % UInt64)
×
279
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
×
280
        low_value + high_value
×
281
    end
282
end
283

284
function Float64(x::Int128)
×
285
    sign_bit = ((x >> 127) % UInt64) << 63
×
286
    ux = uabs(x)
×
287
    if ux < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
×
288
        low_exp = 0x1p52
×
289
        high_exp = 0x1p104
×
290
        low_bits = (ux % UInt64) & Base.significand_mask(Float64)
×
291
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
×
292
        high_bits = ((ux >> 52) % UInt64)
×
293
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
×
294
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
×
295
    else # Large enough that low bits only affect rounding, pack low bits
296
        low_exp = 0x1p76
×
297
        high_exp = 0x1p128
×
298
        low_bits = ((ux >> 12) % UInt64) >> 12 | (ux % UInt64) & 0xFFFFFF
×
299
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
×
300
        high_bits = ((ux >> 76) % UInt64)
×
301
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
×
302
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
×
303
    end
304
end
305

306
function Float32(x::UInt128)
×
307
    x == 0 && return 0f0
×
308
    n = top_set_bit(x) # ndigits0z(x,2)
×
309
    if n <= 24
×
310
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
×
311
    else
312
        y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit
×
313
        y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent)
×
314
        y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even
×
315
    end
316
    d = ((n+126) % UInt32) << 23
×
317
    reinterpret(Float32, d + y)
×
318
end
319

320
function Float32(x::Int128)
×
321
    x == 0 && return 0f0
×
322
    s = ((x >>> 96) % UInt32) & 0x8000_0000 # sign bit
×
323
    x = abs(x) % UInt128
×
324
    n = top_set_bit(x) # ndigits0z(x,2)
×
325
    if n <= 24
×
326
        y = ((x % UInt32) << (24-n)) & 0x007f_ffff
×
327
    else
328
        y = ((x >> (n-25)) % UInt32) & 0x00ff_ffff # keep 1 extra bit
×
329
        y = (y+one(UInt32))>>1 # round, ties up (extra leading bit in case of next exponent)
×
330
        y &= ~UInt32(trailing_zeros(x) == (n-25)) # fix last bit to round to even
×
331
    end
332
    d = ((n+126) % UInt32) << 23
×
333
    reinterpret(Float32, s | d + y)
×
334
end
335

336
# TODO: optimize
337
Float16(x::UInt128) = convert(Float16, Float64(x))
×
338
Float16(x::Int128)  = convert(Float16, Float64(x))
×
339

340
Float16(x::Float32) = fptrunc(Float16, x)
×
341
Float16(x::Float64) = fptrunc(Float16, x)
×
342
Float32(x::Float64) = fptrunc(Float32, x)
×
343

344
Float32(x::Float16) = fpext(Float32, x)
×
345
Float64(x::Float32) = fpext(Float64, x)
×
346
Float64(x::Float16) = fpext(Float64, x)
600,174✔
347

348
AbstractFloat(x::Bool)    = Float64(x)
×
349
AbstractFloat(x::Int8)    = Float64(x)
×
350
AbstractFloat(x::Int16)   = Float64(x)
×
351
AbstractFloat(x::Int32)   = Float64(x)
×
352
AbstractFloat(x::Int64)   = Float64(x) # LOSSY
3,804✔
353
AbstractFloat(x::Int128)  = Float64(x) # LOSSY
×
354
AbstractFloat(x::UInt8)   = Float64(x)
×
355
AbstractFloat(x::UInt16)  = Float64(x)
×
356
AbstractFloat(x::UInt32)  = Float64(x)
×
357
AbstractFloat(x::UInt64)  = Float64(x) # LOSSY
14✔
358
AbstractFloat(x::UInt128) = Float64(x) # LOSSY
×
359

360
Bool(x::Float16) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x))
×
361

362
"""
363
    float(x)
364

365
Convert a number or array to a floating point data type.
366

367
See also: [`complex`](@ref), [`oftype`](@ref), [`convert`](@ref).
368

369
# Examples
370
```jldoctest
371
julia> float(typemax(Int32))
372
2.147483647e9
373
```
374
"""
375
float(x) = AbstractFloat(x)
3,818✔
376

377
"""
378
    float(T::Type)
379

380
Return an appropriate type to represent a value of type `T` as a floating point value.
381
Equivalent to `typeof(float(zero(T)))`.
382

383
# Examples
384
```jldoctest
385
julia> float(Complex{Int})
386
ComplexF64 (alias for Complex{Float64})
387

388
julia> float(Int)
389
Float64
390
```
391
"""
392
float(::Type{T}) where {T<:Number} = typeof(float(zero(T)))
×
393
float(::Type{T}) where {T<:AbstractFloat} = T
×
394
float(::Type{Union{}}, slurp...) = Union{}(0.0)
×
395

396
"""
397
    unsafe_trunc(T, x)
398

399
Return the nearest integral value of type `T` whose absolute value is
400
less than or equal to the absolute value of `x`. If the value is not representable by `T`,
401
an arbitrary value will be returned.
402
See also [`trunc`](@ref).
403

404
# Examples
405
```jldoctest
406
julia> unsafe_trunc(Int, -2.2)
407
-2
408

409
julia> unsafe_trunc(Int, NaN) isa Int
410
true
411
```
412
"""
413
function unsafe_trunc end
414

415
for Ti in (Int8, Int16, Int32, Int64)
416
    @eval begin
417
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptosi($Ti, x)
1,350✔
418
    end
419
end
420
for Ti in (UInt8, UInt16, UInt32, UInt64)
421
    @eval begin
422
        unsafe_trunc(::Type{$Ti}, x::IEEEFloat) = fptoui($Ti, x)
1,125,889✔
423
    end
424
end
425

426
function unsafe_trunc(::Type{UInt128}, x::Float64)
×
427
    xu = reinterpret(UInt64,x)
×
428
    k = Int(xu >> 52) & 0x07ff - 1075
×
429
    xu = (xu & 0x000f_ffff_ffff_ffff) | 0x0010_0000_0000_0000
×
430
    if k <= 0
×
431
        UInt128(xu >> -k)
×
432
    else
433
        UInt128(xu) << k
×
434
    end
435
end
436
function unsafe_trunc(::Type{Int128}, x::Float64)
×
437
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
×
438
end
439

440
function unsafe_trunc(::Type{UInt128}, x::Float32)
×
441
    xu = reinterpret(UInt32,x)
×
442
    k = Int(xu >> 23) & 0x00ff - 150
×
443
    xu = (xu & 0x007f_ffff) | 0x0080_0000
×
444
    if k <= 0
×
445
        UInt128(xu >> -k)
×
446
    else
447
        UInt128(xu) << k
×
448
    end
449
end
450
function unsafe_trunc(::Type{Int128}, x::Float32)
×
451
    copysign(unsafe_trunc(UInt128,x) % Int128, x)
×
452
end
453

454
unsafe_trunc(::Type{UInt128}, x::Float16) = unsafe_trunc(UInt128, Float32(x))
×
455
unsafe_trunc(::Type{Int128}, x::Float16) = unsafe_trunc(Int128, Float32(x))
×
456

457
# matches convert methods
458
# also determines trunc, floor, ceil
459
round(::Type{Signed},   x::IEEEFloat, r::RoundingMode) = round(Int, x, r)
×
460
round(::Type{Unsigned}, x::IEEEFloat, r::RoundingMode) = round(UInt, x, r)
×
461
round(::Type{Integer},  x::IEEEFloat, r::RoundingMode) = round(Int, x, r)
×
462

463
round(x::IEEEFloat, ::RoundingMode{:ToZero})  = trunc_llvm(x)
751,606✔
464
round(x::IEEEFloat, ::RoundingMode{:Down})    = floor_llvm(x)
1,170✔
465
round(x::IEEEFloat, ::RoundingMode{:Up})      = ceil_llvm(x)
750,428✔
466
round(x::IEEEFloat, ::RoundingMode{:Nearest}) = rint_llvm(x)
15✔
467

468
rounds_up(x, ::RoundingMode{:Down}) = false
×
469
rounds_up(x, ::RoundingMode{:Up}) = true
×
470
rounds_up(x, ::RoundingMode{:ToZero}) = signbit(x)
×
471
rounds_up(x, ::RoundingMode{:FromZero}) = !signbit(x)
×
472
function _round_convert(::Type{T}, x_integer, x, r::Union{RoundingMode{:ToZero}, RoundingMode{:FromZero}, RoundingMode{:Up}, RoundingMode{:Down}}) where {T<:AbstractFloat}
×
473
    x_t = convert(T, x_integer)
×
474
    if rounds_up(x, r)
×
475
        x_t < x ? nextfloat(x_t) : x_t
×
476
    else
477
        x_t > x ? prevfloat(x_t) : x_t
×
478
    end
479
end
480

481
## floating point promotions ##
482
promote_rule(::Type{Float32}, ::Type{Float16}) = Float32
×
483
promote_rule(::Type{Float64}, ::Type{Float16}) = Float64
×
484
promote_rule(::Type{Float64}, ::Type{Float32}) = Float64
×
485

486
widen(::Type{Float16}) = Float32
×
487
widen(::Type{Float32}) = Float64
×
488

489
## floating point arithmetic ##
490
-(x::IEEEFloat) = neg_float(x)
×
491

492
+(x::T, y::T) where {T<:IEEEFloat} = add_float(x, y)
600,252✔
493
-(x::T, y::T) where {T<:IEEEFloat} = sub_float(x, y)
752,024✔
494
*(x::T, y::T) where {T<:IEEEFloat} = mul_float(x, y)
773,313✔
495
/(x::T, y::T) where {T<:IEEEFloat} = div_float(x, y)
3,812✔
496

497
muladd(x::T, y::T, z::T) where {T<:IEEEFloat} = muladd_float(x, y, z)
×
498

499
# TODO: faster floating point div?
500
# TODO: faster floating point fld?
501
# TODO: faster floating point mod?
502

503
function unbiased_exponent(x::T) where {T<:IEEEFloat}
504
    return (reinterpret(Unsigned, x) & exponent_mask(T)) >> significand_bits(T)
×
505
end
506

507
function explicit_mantissa_noinfnan(x::T) where {T<:IEEEFloat}
508
    m = mantissa(x)
×
509
    issubnormal(x) || (m |= significand_mask(T) + uinttype(T)(1))
×
510
    return m
×
511
end
512

513
function _to_float(number::U, ep) where {U<:Unsigned}
514
    F = floattype(U)
×
515
    S = signed(U)
×
516
    epint = unsafe_trunc(S,ep)
×
517
    lz::signed(U) = unsafe_trunc(S, Core.Intrinsics.ctlz_int(number) - U(exponent_bits(F)))
×
518
    number <<= lz
×
519
    epint -= lz
×
520
    bits = U(0)
×
521
    if epint >= 0
×
522
        bits = number & significand_mask(F)
×
523
        bits |= ((epint + S(1)) << significand_bits(F)) & exponent_mask(F)
×
524
    else
525
        bits = (number >> -epint) & significand_mask(F)
×
526
    end
527
    return reinterpret(F, bits)
×
528
end
529

530
function rem_internal(x::T, y::T) where {T<:IEEEFloat}
×
531
    @_terminates_locally_meta
×
532
    xuint = reinterpret(Unsigned, x)
×
533
    yuint = reinterpret(Unsigned, y)
×
534
    if xuint <= yuint
×
535
        if xuint < yuint
×
536
            return x
×
537
        end
538
        return zero(T)
×
539
    end
540

541
    e_x = unbiased_exponent(x)
×
542
    e_y = unbiased_exponent(y)
×
543
    # Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
544
    if e_y > (significand_bits(T)) && (e_x - e_y) <= (exponent_bits(T))
×
545
        m_x = explicit_mantissa_noinfnan(x)
×
546
        m_y = explicit_mantissa_noinfnan(y)
×
547
        d = urem_int((m_x << (e_x - e_y)),  m_y)
×
548
        iszero(d) && return zero(T)
×
549
        return _to_float(d, e_y - uinttype(T)(1))
×
550
    end
551
    # Both are subnormals
552
    if e_x == 0 && e_y == 0
×
553
        return reinterpret(T, urem_int(xuint, yuint) & significand_mask(T))
×
554
    end
555

556
    m_x = explicit_mantissa_noinfnan(x)
×
557
    e_x -= uinttype(T)(1)
×
558
    m_y = explicit_mantissa_noinfnan(y)
×
559
    lz_m_y = uinttype(T)(exponent_bits(T))
×
560
    if e_y > 0
×
561
        e_y -= uinttype(T)(1)
×
562
    else
563
        m_y = mantissa(y)
×
564
        lz_m_y = Core.Intrinsics.ctlz_int(m_y)
×
565
    end
566

567
    tz_m_y = Core.Intrinsics.cttz_int(m_y)
×
568
    sides_zeroes_cnt = lz_m_y + tz_m_y
×
569

570
    # n>0
571
    exp_diff = e_x - e_y
×
572
    # Shift hy right until the end or n = 0
573
    right_shift = min(exp_diff, tz_m_y)
×
574
    m_y >>= right_shift
×
575
    exp_diff -= right_shift
×
576
    e_y += right_shift
×
577
    # Shift hx left until the end or n = 0
578
    left_shift = min(exp_diff, uinttype(T)(exponent_bits(T)))
×
579
    m_x <<= left_shift
×
580
    exp_diff -= left_shift
×
581

582
    m_x = urem_int(m_x, m_y)
×
583
    iszero(m_x) && return zero(T)
×
584
    iszero(exp_diff) && return _to_float(m_x, e_y)
×
585

586
    while exp_diff > sides_zeroes_cnt
×
587
        exp_diff -= sides_zeroes_cnt
×
588
        m_x <<= sides_zeroes_cnt
×
589
        m_x = urem_int(m_x, m_y)
×
590
    end
×
591
    m_x <<= exp_diff
×
592
    m_x = urem_int(m_x, m_y)
×
593
    return _to_float(m_x, e_y)
×
594
end
595

596
function rem(x::T, y::T) where {T<:IEEEFloat}
597
    if isfinite(x) && !iszero(x) && isfinite(y) && !iszero(y)
14✔
598
        return copysign(rem_internal(abs(x), abs(y)), x)
14✔
599
    elseif isinf(x) || isnan(y) || iszero(y)  # y can still be Inf
×
600
        return T(NaN)
×
601
    else
602
        return x
×
603
    end
604
end
605

606
function mod(x::T, y::T) where T<:AbstractFloat
607
    if isinf(y) && isfinite(x)
×
608
        return x
×
609
    end
610
    r = rem(x,y)
×
611
    if r == 0
×
612
        copysign(r,y)
×
613
    elseif (r > 0) ⊻ (y > 0)
×
614
        r+y
×
615
    else
616
        r
×
617
    end
618
end
619

620
## floating point comparisons ##
621
==(x::T, y::T) where {T<:IEEEFloat} = eq_float(x, y)
2,252,874✔
622
!=(x::T, y::T) where {T<:IEEEFloat} = ne_float(x, y)
603✔
623
<( x::T, y::T) where {T<:IEEEFloat} = lt_float(x, y)
1,878,291✔
624
<=(x::T, y::T) where {T<:IEEEFloat} = le_float(x, y)
751,700✔
625

626
isequal(x::T, y::T) where {T<:IEEEFloat} = fpiseq(x, y)
×
627

628
# interpret as sign-magnitude integer
629
function _fpint(x)
2✔
630
    @inline
×
631
    IntT = inttype(typeof(x))
×
632
    ix = reinterpret(IntT, x)
2✔
633
    return ifelse(ix < zero(IntT), ix ⊻ typemax(IntT), ix)
2✔
634
end
635

636
function isless(a::T, b::T) where T<:IEEEFloat
637
    @inline
×
638
    (isnan(a) || isnan(b)) && return !isnan(a)
×
639

640
    return _fpint(a) < _fpint(b)
×
641
end
642

643
# Exact Float (Tf) vs Integer (Ti) comparisons
644
# Assumes:
645
# - typemax(Ti) == 2^n-1
646
# - typemax(Ti) can't be exactly represented by Tf:
647
#   => Tf(typemax(Ti)) == 2^n or Inf
648
# - typemin(Ti) can be exactly represented by Tf
649
#
650
# 1. convert y::Ti to float fy::Tf
651
# 2. perform Tf comparison x vs fy
652
# 3. if x == fy, check if (1) resulted in rounding:
653
#  a. convert fy back to Ti and compare with original y
654
#  b. unsafe_convert undefined behaviour if fy == Tf(typemax(Ti))
655
#     (but consequently x == fy > y)
656
for Ti in (Int64,UInt64,Int128,UInt128)
657
    for Tf in (Float32,Float64)
658
        @eval begin
659
            function ==(x::$Tf, y::$Ti)
660
                fy = ($Tf)(y)
2✔
661
                (x == fy) & (fy != $(Tf(typemax(Ti)))) & (y == unsafe_trunc($Ti,fy))
2✔
662
            end
663
            ==(y::$Ti, x::$Tf) = x==y
×
664

665
            function <(x::$Ti, y::$Tf)
666
                fx = ($Tf)(x)
375,355✔
667
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
375,355✔
668
            end
669
            function <=(x::$Ti, y::$Tf)
670
                fx = ($Tf)(x)
750,415✔
671
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
750,415✔
672
            end
673

674
            function <(x::$Tf, y::$Ti)
675
                fy = ($Tf)(y)
436✔
676
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
436✔
677
            end
678
            function <=(x::$Tf, y::$Ti)
679
                fy = ($Tf)(y)
124✔
680
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
124✔
681
            end
682
        end
683
    end
684
end
685
for op in (:(==), :<, :<=)
686
    @eval begin
687
        ($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
×
688
        ($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
×
689

690
        ($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
×
691
        ($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))
×
692

693
        ($op)(x::Float16, y::Union{Int16,UInt16}) = ($op)(Float32(x), Float32(y))
×
694
        ($op)(x::Union{Int16,UInt16}, y::Float16) = ($op)(Float32(x), Float32(y))
×
695
    end
696
end
697

698

699
abs(x::IEEEFloat) = abs_float(x)
14✔
700

701
"""
702
    isnan(f)::Bool
703

704
Test whether a number value is a NaN, an indeterminate value which is neither an infinity
705
nor a finite number ("not a number").
706

707
See also: [`iszero`](@ref), [`isone`](@ref), [`isinf`](@ref), [`ismissing`](@ref).
708
"""
709
isnan(x::AbstractFloat) = (x != x)::Bool
160✔
710
isnan(x::Number) = false
×
711

712
isfinite(x::AbstractFloat) = !isnan(x - x)
156✔
713
isfinite(x::Real) = decompose(x)[3] != 0
×
714
isfinite(x::Integer) = true
×
715

716
"""
717
    isinf(f)::Bool
718

719
Test whether a number is infinite.
720

721
See also: [`Inf`](@ref), [`iszero`](@ref), [`isfinite`](@ref), [`isnan`](@ref).
722
"""
723
isinf(x::Real) = !isnan(x) & !isfinite(x)
×
724
isinf(x::IEEEFloat) = abs(x) === oftype(x, Inf)
×
725

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

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

733
    num*2^pow/den
734

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

740
Special values:
741

742
 - `x` is zero: `num` should be zero and `den` should have the same sign as `x`
743
 - `x` is infinite: `den` should be zero and `num` should have the same sign as `x`
744
 - `x` is not a number: `num` and `den` should both be zero
745
=#
746

747
decompose(x::Integer) = x, 0, 1
×
748

749
function decompose(x::Float16)::NTuple{3,Int}
×
750
    isnan(x) && return 0, 0, 0
×
751
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
×
752
    n = reinterpret(UInt16, x)
×
753
    s = (n & 0x03ff) % Int16
×
754
    e = ((n & 0x7c00) >> 10) % Int
×
755
    s |= Int16(e != 0) << 10
×
756
    d = ifelse(signbit(x), -1, 1)
×
757
    s, e - 25 + (e == 0), d
×
758
end
759

760
function decompose(x::Float32)::NTuple{3,Int}
×
761
    isnan(x) && return 0, 0, 0
×
762
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
×
763
    n = reinterpret(UInt32, x)
×
764
    s = (n & 0x007fffff) % Int32
×
765
    e = ((n & 0x7f800000) >> 23) % Int
×
766
    s |= Int32(e != 0) << 23
×
767
    d = ifelse(signbit(x), -1, 1)
×
768
    s, e - 150 + (e == 0), d
×
769
end
770

771
function decompose(x::Float64)::Tuple{Int64, Int, Int}
×
772
    isnan(x) && return 0, 0, 0
×
773
    isinf(x) && return ifelse(x < 0, -1, 1), 0, 0
×
774
    n = reinterpret(UInt64, x)
×
775
    s = (n & 0x000fffffffffffff) % Int64
×
776
    e = ((n & 0x7ff0000000000000) >> 52) % Int
×
777
    s |= Int64(e != 0) << 52
×
778
    d = ifelse(signbit(x), -1, 1)
×
779
    s, e - 1075 + (e == 0), d
×
780
end
781

782

783
"""
784
    precision(num::AbstractFloat; base::Integer=2)
785
    precision(T::Type; base::Integer=2)
786

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

791
If `base` is specified, then it returns the maximum corresponding
792
number of significand digits in that base.
793

794
!!! compat "Julia 1.8"
795
    The `base` keyword requires at least Julia 1.8.
796
"""
797
function precision end
798

799
_precision_with_base_2(::Type{Float16}) = 11
×
800
_precision_with_base_2(::Type{Float32}) = 24
×
801
_precision_with_base_2(::Type{Float64}) = 53
×
802
function _precision(x, base::Integer)
×
803
    base > 1 || throw(DomainError(base, "`base` cannot be less than 2."))
×
804
    p = _precision_with_base_2(x)
×
805
    return base == 2 ? Int(p) : floor(Int, p / log2(base))
×
806
end
807
precision(::Type{T}; base::Integer=2) where {T<:AbstractFloat} = _precision(T, base)
×
808
precision(::T; base::Integer=2) where {T<:AbstractFloat} = precision(T; base)
×
809

810

811
"""
812
    nextfloat(x::AbstractFloat, n::Integer)
813

814
The result of `n` iterative applications of `nextfloat` to `x` if `n >= 0`, or `-n`
815
applications of [`prevfloat`](@ref) if `n < 0`.
816
"""
817
function nextfloat(f::IEEEFloat, d::Integer)
818
    F = typeof(f)
×
819
    fumax = reinterpret(Unsigned, F(Inf))
×
820
    U = typeof(fumax)
×
821

822
    isnan(f) && return f
2✔
823
    fi = reinterpret(Signed, f)
2✔
824
    fneg = fi < 0
2✔
825
    fu = unsigned(fi & typemax(fi))
2✔
826

827
    dneg = d < 0
×
828
    da = uabs(d)
×
829
    if da > typemax(U)
2✔
830
        fneg = dneg
×
831
        fu = fumax
×
832
    else
833
        du = da % U
×
834
        if fneg ⊻ dneg
2✔
835
            if du > fu
×
836
                fu = min(fumax, du - fu)
×
837
                fneg = !fneg
×
838
            else
839
                fu = fu - du
×
840
            end
841
        else
842
            if fumax - fu < du
2✔
843
                fu = fumax
×
844
            else
845
                fu = fu + du
2✔
846
            end
847
        end
848
    end
849
    if fneg
2✔
850
        fu |= sign_mask(F)
×
851
    end
852
    reinterpret(F, fu)
2✔
853
end
854

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

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

861
See also: [`prevfloat`](@ref), [`eps`](@ref), [`issubnormal`](@ref).
862
"""
863
nextfloat(x::AbstractFloat) = nextfloat(x,1)
2✔
864

865
"""
866
    prevfloat(x::AbstractFloat, n::Integer)
867

868
The result of `n` iterative applications of `prevfloat` to `x` if `n >= 0`, or `-n`
869
applications of [`nextfloat`](@ref) if `n < 0`.
870
"""
871
prevfloat(x::AbstractFloat, d::Integer) = nextfloat(x, -d)
×
872

873
"""
874
    prevfloat(x::AbstractFloat)
875

876
Return the largest floating point number `y` of the same type as `x` such that `y < x`.
877
If no such `y` exists (e.g. if `x` is `-Inf` or `NaN`), then return `x`.
878
"""
879
prevfloat(x::AbstractFloat) = nextfloat(x,-1)
×
880

881
for Ti in (Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128)
882
    for Tf in (Float16, Float32, Float64)
883
        if Ti <: Unsigned || sizeof(Ti) < sizeof(Tf)
884
            # Here `Tf(typemin(Ti))-1` is exact, so we can compare the lower-bound
885
            # directly. `Tf(typemax(Ti))+1` is either always exactly representable, or
886
            # rounded to `Inf` (e.g. when `Ti==UInt128 && Tf==Float32`).
887
            @eval begin
888
                function round(::Type{$Ti},x::$Tf,::RoundingMode{:ToZero})
×
889
                    if $(Tf(typemin(Ti))-one(Tf)) < x < $(Tf(typemax(Ti))+one(Tf))
×
890
                        return unsafe_trunc($Ti,x)
×
891
                    else
892
                        throw(InexactError(:round, $Ti, x, RoundToZero))
×
893
                    end
894
                end
895
                function (::Type{$Ti})(x::$Tf)
896
                    # When typemax(Ti) is not representable by Tf but typemax(Ti) + 1 is,
897
                    # then < Tf(typemax(Ti) + 1) is stricter than <= Tf(typemax(Ti)). Using
898
                    # the former causes us to throw on UInt64(Float64(typemax(UInt64))+1)
899
                    if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti))+one(Tf))) && isinteger(x)
750,408✔
900
                        return unsafe_trunc($Ti,x)
750,408✔
901
                    else
902
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
×
903
                    end
904
                end
905
            end
906
        else
907
            # Here `eps(Tf(typemin(Ti))) > 1`, so the only value which can be truncated to
908
            # `Tf(typemin(Ti)` is itself. Similarly, `Tf(typemax(Ti))` is inexact and will
909
            # be rounded up. This assumes that `Tf(typemin(Ti)) > -Inf`, which is true for
910
            # these types, but not for `Float16` or larger integer types.
911
            @eval begin
912
                function round(::Type{$Ti},x::$Tf,::RoundingMode{:ToZero})
913
                    if $(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))
1✔
914
                        return unsafe_trunc($Ti,x)
1✔
915
                    else
916
                        throw(InexactError(:round, $Ti, x, RoundToZero))
×
917
                    end
918
                end
919
                function (::Type{$Ti})(x::$Tf)
920
                    if ($(Tf(typemin(Ti))) <= x < $(Tf(typemax(Ti)))) && isinteger(x)
1,198✔
921
                        return unsafe_trunc($Ti,x)
1,198✔
922
                    else
923
                        throw(InexactError($(Expr(:quote,Ti.name.name)), $Ti, x))
×
924
                    end
925
                end
926
            end
927
        end
928
    end
929
end
930

931
"""
932
    issubnormal(f)::Bool
933

934
Test whether a floating point number is subnormal.
935

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

939
# Examples
940
```jldoctest
941
julia> floatmin(Float32)
942
1.1754944f-38
943

944
julia> issubnormal(1.0f-37)
945
false
946

947
julia> issubnormal(1.0f-38)
948
true
949
```
950
"""
951
function issubnormal(x::T) where {T<:IEEEFloat}
952
    y = reinterpret(Unsigned, x)
×
953
    (y & exponent_mask(T) == 0) & (y & significand_mask(T) != 0)
×
954
end
955

956
ispow2(x::AbstractFloat) = !iszero(x) && frexp(x)[1] == 0.5
×
957
iseven(x::AbstractFloat) = isinteger(x) && (abs(x) > maxintfloat(x) || iseven(Integer(x)))
×
958
isodd(x::AbstractFloat) = isinteger(x) && abs(x) ≤ maxintfloat(x) && isodd(Integer(x))
×
959

960
@eval begin
961
    typemin(::Type{Float16}) = $(bitcast(Float16, 0xfc00))
×
962
    typemax(::Type{Float16}) = $(Inf16)
×
963
    typemin(::Type{Float32}) = $(-Inf32)
×
964
    typemax(::Type{Float32}) = $(Inf32)
×
965
    typemin(::Type{Float64}) = $(-Inf64)
×
966
    typemax(::Type{Float64}) = $(Inf64)
×
967
    typemin(x::T) where {T<:Real} = typemin(T)
×
968
    typemax(x::T) where {T<:Real} = typemax(T)
136,264✔
969

970
    floatmin(::Type{Float16}) = $(bitcast(Float16, 0x0400))
×
971
    floatmin(::Type{Float32}) = $(bitcast(Float32, 0x00800000))
×
972
    floatmin(::Type{Float64}) = $(bitcast(Float64, 0x0010000000000000))
×
973
    floatmax(::Type{Float16}) = $(bitcast(Float16, 0x7bff))
×
974
    floatmax(::Type{Float32}) = $(bitcast(Float32, 0x7f7fffff))
×
975
    floatmax(::Type{Float64}) = $(bitcast(Float64, 0x7fefffffffffffff))
×
976

977
    eps(::Type{Float16}) = $(bitcast(Float16, 0x1400))
×
978
    eps(::Type{Float32}) = $(bitcast(Float32, 0x34000000))
×
979
    eps(::Type{Float64}) = $(bitcast(Float64, 0x3cb0000000000000))
×
980
    eps() = eps(Float64)
×
981
end
982

983
eps(x::AbstractFloat) = isfinite(x) ? abs(x) >= floatmin(x) ? ldexp(eps(typeof(x)), exponent(x)) : nextfloat(zero(x)) : oftype(x, NaN)
×
984

985
function eps(x::T) where T<:IEEEFloat
986
    # For isfinite(x), toggling the LSB will produce either prevfloat(x) or
987
    # nextfloat(x) but will never change the sign or exponent.
988
    # For !isfinite(x), this will map Inf to NaN and NaN to NaN or Inf.
989
    y = reinterpret(T, reinterpret(Unsigned, x) ⊻ true)
×
990
    # The absolute difference between these values is eps(x). This is true even
991
    # for Inf/NaN values.
992
    return abs(x - y)
×
993
end
994

995
"""
996
    floatmin(T = Float64)
997

998
Return the smallest positive normal number representable by the floating-point
999
type `T`.
1000

1001
See also: [`typemin`](@ref), [`maxintfloat`](@ref), [`floatmax`](@ref), [`eps`](@ref).
1002

1003
# Examples
1004
```jldoctest
1005
julia> floatmin(Float16)
1006
Float16(6.104e-5)
1007

1008
julia> floatmin(Float32)
1009
1.1754944f-38
1010

1011
julia> floatmin()
1012
2.2250738585072014e-308
1013
```
1014
"""
1015
floatmin(x::T) where {T<:AbstractFloat} = floatmin(T)
×
1016

1017
"""
1018
    floatmax(T = Float64)
1019

1020
Return the largest finite number representable by the floating-point type `T`.
1021

1022
See also: [`typemax`](@ref), [`maxintfloat`](@ref), [`floatmin`](@ref), [`eps`](@ref).
1023

1024
# Examples
1025
```jldoctest
1026
julia> floatmax(Float16)
1027
Float16(6.55e4)
1028

1029
julia> floatmax(Float32)
1030
3.4028235f38
1031

1032
julia> floatmax()
1033
1.7976931348623157e308
1034

1035
julia> typemax(Float64)
1036
Inf
1037
```
1038
"""
1039
floatmax(x::T) where {T<:AbstractFloat} = floatmax(T)
×
1040

1041
floatmin() = floatmin(Float64)
×
1042
floatmax() = floatmax(Float64)
×
1043

1044
"""
1045
    eps(::Type{T}) where T<:AbstractFloat
1046
    eps()
1047

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

1053
# Examples
1054
```jldoctest
1055
julia> eps()
1056
2.220446049250313e-16
1057

1058
julia> eps(Float32)
1059
1.1920929f-7
1060

1061
julia> 1.0 + eps()
1062
1.0000000000000002
1063

1064
julia> 1.0 + eps()/2
1065
1.0
1066
```
1067

1068
More generally, for any floating-point numeric type, `eps` corresponds to an
1069
upper bound on the distance to the nearest floating-point complex value: if ``\text{fl}(x)`` is the closest
1070
floating-point value to a number ``x`` (e.g. an arbitrary real number), then ``\text{fl}(x)``
1071
satisfies ``|x - \text{fl}(x)| ≤ \text{eps}(x)/2``, not including overflow cases.
1072
This allows the definition of `eps` to be extended to complex numbers,
1073
for which ``\text{fl}(a + ib) = \text{fl}(a) + i \text{fl}(b)``.
1074
"""
1075
eps(::Type{<:AbstractFloat})
1076

1077
"""
1078
    eps(x::AbstractFloat)
1079

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

1084
    eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
1085

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

1090
The rationale for this behavior is that `eps` bounds the floating point rounding
1091
error. Under the default `RoundNearest` rounding mode, if ``y`` is a real number and ``x``
1092
is the nearest floating point number to ``y``, then
1093

1094
```math
1095
|y-x| \\leq \\operatorname{eps}(x)/2.
1096
```
1097

1098
See also: [`nextfloat`](@ref), [`issubnormal`](@ref), [`floatmax`](@ref).
1099

1100
# Examples
1101
```jldoctest
1102
julia> eps(1.0)
1103
2.220446049250313e-16
1104

1105
julia> eps(prevfloat(2.0))
1106
2.220446049250313e-16
1107

1108
julia> eps(2.0)
1109
4.440892098500626e-16
1110

1111
julia> x = prevfloat(Inf)      # largest finite Float64
1112
1.7976931348623157e308
1113

1114
julia> x + eps(x)/2            # rounds up
1115
Inf
1116

1117
julia> x + prevfloat(eps(x)/2) # rounds down
1118
1.7976931348623157e308
1119
```
1120
"""
1121
eps(::AbstractFloat)
1122

1123

1124
## byte order swaps for arbitrary-endianness serialization/deserialization ##
1125
bswap(x::IEEEFloat) = bswap_int(x)
×
1126

1127
# integer size of float
1128
uinttype(::Type{Float64}) = UInt64
×
1129
uinttype(::Type{Float32}) = UInt32
×
1130
uinttype(::Type{Float16}) = UInt16
×
1131
inttype(::Type{Float64}) = Int64
×
1132
inttype(::Type{Float32}) = Int32
×
1133
inttype(::Type{Float16}) = Int16
×
1134
# float size of integer
1135
floattype(::Type{UInt64}) = Float64
×
1136
floattype(::Type{UInt32}) = Float32
×
1137
floattype(::Type{UInt16}) = Float16
×
1138
floattype(::Type{Int64}) = Float64
×
1139
floattype(::Type{Int32}) = Float32
×
1140
floattype(::Type{Int16}) = Float16
×
1141

1142

1143
## Array operations on floating point numbers ##
1144
"""
1145
    float(A::AbstractArray)
1146

1147
Return an array containing the floating-point analog of each entry in array `A`.
1148

1149
Equivalent to `float.(A)`, except that the return value may share memory with all or
1150
part of `A` in accordance with the behavior of `convert(T, A)` given output type `T`.
1151

1152
# Examples
1153
```jldoctest
1154
julia> float(1:1000)
1155
1.0:1.0:1000.0
1156
```
1157
"""
1158
float(A::AbstractArray{<:AbstractFloat}) = A
×
1159

1160
function float(A::AbstractArray{T}) where T
×
1161
    if !isconcretetype(T)
×
1162
        error("`float` not defined on abstractly-typed arrays; please convert to a more specific type")
×
1163
    end
1164
    convert(AbstractArray{typeof(float(zero(T)))}, A)
×
1165
end
1166

1167
float(r::StepRange) = float(r.start):float(r.step):float(last(r))
×
1168
float(r::UnitRange) = float(r.start):float(last(r))
×
1169
float(r::StepRangeLen{T}) where {T} =
×
1170
    StepRangeLen{typeof(float(T(r.ref)))}(float(r.ref), float(r.step), length(r), r.offset)
1171
function float(r::LinRange)
×
1172
    LinRange(float(r.start), float(r.stop), length(r))
×
1173
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