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

JuliaLang / julia / #37487

pending completion
#37487

push

local

web-flow
Subtype: Add a fastpath for nested constructor with identity name. (#49159)

* Subtype: Add a fastpath for nested constructor with identity name.

* Update src/subtype.c

---------

Co-authored-by: Jameson Nash <vtjnash@gmail.com>

71468 of 82918 relevant lines covered (86.19%)

30575088.21 hits per line

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

88.96
/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)
8,717,521✔
80
reinterpret(::Type{Unsigned}, x::Float32) = reinterpret(UInt32, x)
605,835,261✔
81
reinterpret(::Type{Unsigned}, x::Float16) = reinterpret(UInt16, x)
3,796,508✔
82
reinterpret(::Type{Signed}, x::Float64) = reinterpret(Int64, x)
600,260,377✔
83
reinterpret(::Type{Signed}, x::Float32) = reinterpret(Int32, x)
600,703,742✔
84
reinterpret(::Type{Signed}, x::Float16) = reinterpret(Int16, x)
676,072✔
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,008✔
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,597✔
99
exponent_mask(::Type{Float16}) =    0x7c00
×
100
exponent_one(::Type{Float16}) =     0x3c00
×
101
exponent_half(::Type{Float16}) =    0x3800
999,222✔
102
significand_mask(::Type{Float16}) = 0x03ff
×
103

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

106
for T in (Float16, Float32, Float64)
107
    @eval significand_bits(::Type{$T}) = $(trailing_ones(significand_mask(T)))
×
108
    @eval exponent_bits(::Type{$T}) = $(sizeof(T)*8 - significand_bits(T) - 1)
1,273✔
109
    @eval exponent_bias(::Type{$T}) = $(Int(exponent_one(T) >> significand_bits(T)))
×
110
    # maximum float exponent
111
    @eval exponent_max(::Type{$T}) = $(Int(exponent_mask(T) >> significand_bits(T)) - exponent_bias(T) - 1)
×
112
    # maximum float exponent without bias
113
    @eval exponent_raw_max(::Type{$T}) = $(Int(exponent_mask(T) >> significand_bits(T)))
2,488,273✔
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,211,117✔
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)
263,405,326✔
160
            promote_rule(::Type{$t1}, ::Type{$st}) = $t1
39,518,269✔
161
        end
162
    end
163
    for ut in (Bool, UInt8, UInt16, UInt32, UInt64)
164
        @eval begin
165
            (::Type{$t1})(x::($ut)) = uitofp($t1, x)
85,100,943✔
166
            promote_rule(::Type{$t1}, ::Type{$ut}) = $t1
1,653,514✔
167
        end
168
    end
169
end
170

171
Bool(x::Real) = x==0 ? false : x==1 ? true : throw(InexactError(:Bool, Bool, x))
7,190✔
172

173
promote_rule(::Type{Float64}, ::Type{UInt128}) = Float64
60✔
174
promote_rule(::Type{Float64}, ::Type{Int128}) = Float64
693,619✔
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)
3,956,894✔
201
    sign_bit = ((x >> 127) % UInt64) << 63
3,305,650✔
202
    ux = uabs(x)
3,956,932✔
203
    if ux < UInt128(1) << 104 # Can fit it in two 52 bits mantissas
3,956,932✔
204
        low_exp = 0x1p52
×
205
        high_exp = 0x1p104
×
206
        low_bits = (ux % UInt64) & Base.significand_mask(Float64)
3,285,621✔
207
        low_value = reinterpret(Float64, reinterpret(UInt64, low_exp) | low_bits) - low_exp
3,285,621✔
208
        high_bits = ((ux >> 52) % UInt64)
3,285,621✔
209
        high_value = reinterpret(Float64, reinterpret(UInt64, high_exp) | high_bits) - high_exp
3,285,621✔
210
        reinterpret(Float64, sign_bit | reinterpret(UInt64, low_value + high_value))
3,936,903✔
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,226,072✔
257
Float16(x::Float64) = fptrunc(Float16, x)
282,420✔
258
Float32(x::Float64) = fptrunc(Float32, x)
457,517,377✔
259

260
Float32(x::Float16) = fpext(Float32, x)
27,503,769✔
261
Float64(x::Float32) = fpext(Float64, x)
472,682,324✔
262
Float64(x::Float16) = fpext(Float64, x)
3,078,934✔
263

264
AbstractFloat(x::Bool)    = Float64(x)
432,699✔
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
23,919,653✔
269
AbstractFloat(x::Int128)  = Float64(x) # LOSSY
1,308,669✔
270
AbstractFloat(x::UInt8)   = Float64(x)
50✔
271
AbstractFloat(x::UInt16)  = Float64(x)
45✔
272
AbstractFloat(x::UInt32)  = Float64(x)
45✔
273
AbstractFloat(x::UInt64)  = Float64(x) # LOSSY
1,830✔
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)
43,445,513✔
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)))
511✔
312
float(::Type{T}) where {T<:AbstractFloat} = T
21,465✔
313

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

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

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

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

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

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

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

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

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

381
# fallbacks
382
floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))
76,808✔
383
ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
686,608✔
384
round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))
10,328,927✔
385

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

392
round(x::IEEEFloat, r::RoundingMode{:ToZero})  = trunc_llvm(x)
24,075,955✔
393
round(x::IEEEFloat, r::RoundingMode{:Down})    = floor_llvm(x)
162,712✔
394
round(x::IEEEFloat, r::RoundingMode{:Up})      = ceil_llvm(x)
726,956✔
395
round(x::IEEEFloat, r::RoundingMode{:Nearest}) = rint_llvm(x)
11,036,999✔
396

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

402
widen(::Type{Float16}) = Float32
11,250,075✔
403
widen(::Type{Float32}) = Float64
9,324,969✔
404

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

408
+(x::T, y::T) where {T<:IEEEFloat} = add_float(x, y)
710,067,360✔
409
-(x::T, y::T) where {T<:IEEEFloat} = sub_float(x, y)
1,239,493,154✔
410
*(x::T, y::T) where {T<:IEEEFloat} = mul_float(x, y)
4,160,381,101✔
411
/(x::T, y::T) where {T<:IEEEFloat} = div_float(x, y)
893,901,783✔
412

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

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

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

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

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

446
@assume_effects :terminates_locally :nothrow function rem_internal(x::T, y::T) where {T<:IEEEFloat}
587,680✔
447
    xuint = reinterpret(Unsigned, x)
587,680✔
448
    yuint = reinterpret(Unsigned, y)
587,680✔
449
    if xuint <= yuint
587,680✔
450
        if xuint < yuint
72,339✔
451
            return x
66,649✔
452
        end
453
        return zero(T)
5,690✔
454
    end
455

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

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

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

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

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

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

511
function rem(x::T, y::T) where {T<:IEEEFloat}
22,382✔
512
    if isfinite(x) && !iszero(x) && isfinite(y) && !iszero(y)
594,975✔
513
        return copysign(rem_internal(abs(x), abs(y)), x)
587,678✔
514
    elseif isinf(x) || isnan(y) || iszero(y)  # y can still be Inf
14,563✔
515
        return T(NaN)
73✔
516
    else
517
        return x
7,224✔
518
    end
519
end
520

521
function mod(x::T, y::T) where {T<:AbstractFloat}
52,865✔
522
    r = rem(x,y)
95,550✔
523
    if r == 0
91,802✔
524
        copysign(r,y)
15,433✔
525
    elseif (r > 0) ⊻ (y > 0)
76,369✔
526
        r+y
28,590✔
527
    else
528
        r
47,779✔
529
    end
530
end
531

532
## floating point comparisons ##
533
==(x::T, y::T) where {T<:IEEEFloat} = eq_float(x, y)
243,053,196✔
534
!=(x::T, y::T) where {T<:IEEEFloat} = ne_float(x, y)
2,327,457,372✔
535
<( x::T, y::T) where {T<:IEEEFloat} = lt_float(x, y)
164,785,868✔
536
<=(x::T, y::T) where {T<:IEEEFloat} = le_float(x, y)
141,257,095✔
537

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

540
# interpret as sign-magnitude integer
541
@inline function _fpint(x)
10,391✔
542
    IntT = inttype(typeof(x))
10,384✔
543
    ix = reinterpret(IntT, x)
31,158,441✔
544
    return ifelse(ix < zero(IntT), ix ⊻ typemax(IntT), ix)
31,158,441✔
545
end
546

547
@inline function isless(a::T, b::T) where T<:IEEEFloat
98,775✔
548
    (isnan(a) || isnan(b)) && return !isnan(a)
31,438,254✔
549

550
    return _fpint(a) < _fpint(b)
15,693,694✔
551
end
552

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

575
            function <(x::$Ti, y::$Tf)
39,893,436✔
576
                fx = ($Tf)(x)
39,907,021✔
577
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < unsafe_trunc($Ti,fx)) ))
40,058,173✔
578
            end
579
            function <=(x::$Ti, y::$Tf)
21,742✔
580
                fx = ($Tf)(x)
204,591✔
581
                (fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= unsafe_trunc($Ti,fx)) ))
740,199✔
582
            end
583

584
            function <(x::$Tf, y::$Ti)
633,344✔
585
                fy = ($Tf)(y)
634,257✔
586
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) < y))
828,363✔
587
            end
588
            function <=(x::$Tf, y::$Ti)
21,435✔
589
                fy = ($Tf)(y)
21,435✔
590
                (x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (unsafe_trunc($Ti,fy) <= y))
22,072✔
591
            end
592
        end
593
    end
594
end
595
for op in (:(==), :<, :<=)
596
    @eval begin
597
        ($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
2,165,868✔
598
        ($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
2,048✔
599

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

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

608

609
abs(x::IEEEFloat) = abs_float(x)
183,557,683✔
610

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

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

617
See also: [`iszero`](@ref), [`isone`](@ref), [`isinf`](@ref), [`ismissing`](@ref).
618
"""
619
isnan(x::AbstractFloat) = (x != x)::Bool
2,324,763,276✔
620
isnan(x::Number) = false
100,717✔
621

622
isfinite(x::AbstractFloat) = !isnan(x - x)
1,016,865,830✔
623
isfinite(x::Real) = decompose(x)[3] != 0
73,816✔
624
isfinite(x::Integer) = true
11,064✔
625

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

629
Test whether a number is infinite.
630

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

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

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

660
## generic hashing for rational values ##
661

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

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

671
    # normalize decomposition
672
    if den < 0
885✔
673
        num = -num
66✔
674
        den = -den
66✔
675
    end
676
    z = trailing_zeros(num)
1,168✔
677
    if z != 0
1,168✔
678
        num >>= z
928✔
679
        pow += z
603✔
680
    end
681
    z = trailing_zeros(den)
1,168✔
682
    if z != 0
885✔
683
        den >>= z
×
684
        pow -= z
×
685
    end
686

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

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

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

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

714
    num*2^pow/den
715

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

721
Special values:
722

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

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

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

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

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

763

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

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

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

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

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

791

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

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

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

808
    dneg = d < 0
601,379,822✔
809
    da = uabs(d)
601,379,823✔
810
    if da > typemax(U)
1,201,639,970✔
811
        fneg = dneg
4✔
812
        fu = fumax
4✔
813
    else
814
        du = da % U
601,379,761✔
815
        if fneg ⊻ dneg
1,201,639,966✔
816
            if du > fu
891,826✔
817
                fu = min(fumax, du - fu)
59✔
818
                fneg = !fneg
59✔
819
            else
820
                fu = fu - du
1,525,910✔
821
            end
822
        else
823
            if fumax - fu < du
1,200,748,140✔
824
                fu = fumax
9✔
825
            else
826
                fu = fu + du
1,200,748,131✔
827
            end
828
        end
829
    end
830
    if fneg
1,201,639,970✔
831
        fu |= sign_mask(F)
663,724✔
832
    end
833
    reinterpret(F, fu)
1,201,639,970✔
834
end
835

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

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

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

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

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

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

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

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

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

912
Test whether a floating point number is subnormal.
913

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

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

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

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

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

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

948
    floatmin(::Type{Float16}) = $(bitcast(Float16, 0x0400))
493,754✔
949
    floatmin(::Type{Float32}) = $(bitcast(Float32, 0x00800000))
161,595✔
950
    floatmin(::Type{Float64}) = $(bitcast(Float64, 0x0010000000000000))
×
951
    floatmax(::Type{Float16}) = $(bitcast(Float16, 0x7bff))
30,877✔
952
    floatmax(::Type{Float32}) = $(bitcast(Float32, 0x7f7fffff))
130,481✔
953
    floatmax(::Type{Float64}) = $(bitcast(Float64, 0x7fefffffffffffff))
625,680✔
954

955
    eps(x::AbstractFloat) = isfinite(x) ? abs(x) >= floatmin(x) ? ldexp(eps(typeof(x)), exponent(x)) : nextfloat(zero(x)) : oftype(x, NaN)
1,380,169✔
956
    eps(::Type{Float16}) = $(bitcast(Float16, 0x1400))
399,533✔
957
    eps(::Type{Float32}) = $(bitcast(Float32, 0x34000000))
615,587✔
958
    eps(::Type{Float64}) = $(bitcast(Float64, 0x3cb0000000000000))
×
959
    eps() = eps(Float64)
391✔
960
end
961

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

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

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

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

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

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

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

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

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

994
julia> floatmax(Float32)
995
3.4028235f38
996

997
julia> floatmax()
998
1.7976931348623157e308
999

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

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

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

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

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

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

1026
julia> 1.0 + eps()
1027
1.0000000000000002
1028

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1081

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

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

1100

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

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

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

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

© 2026 Coveralls, Inc