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

JuliaLang / julia / #37594

pending completion
#37594

push

local

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

73676 of 84540 relevant lines covered (87.15%)

32579691.71 hits per line

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

94.74
/base/floatfuncs.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
## floating-point functions ##
4

5
copysign(x::Float64, y::Float64) = copysign_float(x, y)
3,184,897✔
6
copysign(x::Float32, y::Float32) = copysign_float(x, y)
1,549,382✔
7
copysign(x::Float32, y::Real) = copysign(x, Float32(y))
51✔
8
copysign(x::Float64, y::Real) = copysign(x, Float64(y))
104✔
9

10
flipsign(x::Float64, y::Float64) = bitcast(Float64, xor_int(bitcast(UInt64, x), and_int(bitcast(UInt64, y), 0x8000000000000000)))
116,413✔
11
flipsign(x::Float32, y::Float32) = bitcast(Float32, xor_int(bitcast(UInt32, x), and_int(bitcast(UInt32, y), 0x80000000)))
123✔
12
flipsign(x::Float32, y::Real) = flipsign(x, Float32(y))
63✔
13
flipsign(x::Float64, y::Real) = flipsign(x, Float64(y))
251✔
14

15
signbit(x::Float64) = signbit(bitcast(Int64, x))
23,808,369✔
16
signbit(x::Float32) = signbit(bitcast(Int32, x))
17,245,800✔
17
signbit(x::Float16) = signbit(bitcast(Int16, x))
10,304✔
18

19
"""
20
    maxintfloat(T=Float64)
21

22
The largest consecutive integer-valued floating-point number that is exactly represented in
23
the given floating-point type `T` (which defaults to `Float64`).
24

25
That is, `maxintfloat` returns the smallest positive integer-valued floating-point number
26
`n` such that `n+1` is *not* exactly representable in the type `T`.
27

28
When an `Integer`-type value is needed, use `Integer(maxintfloat(T))`.
29
"""
30
maxintfloat(::Type{Float64}) = 9007199254740992.
855,395✔
31
maxintfloat(::Type{Float32}) = Float32(16777216.)
3,125,397✔
32
maxintfloat(::Type{Float16}) = Float16(2048f0)
2,284,972✔
33
maxintfloat(x::T) where {T<:AbstractFloat} = maxintfloat(T)
63✔
34

35
"""
36
    maxintfloat(T, S)
37

38
The largest consecutive integer representable in the given floating-point type `T` that
39
also does not exceed the maximum integer representable by the integer type `S`.  Equivalently,
40
it is the minimum of `maxintfloat(T)` and [`typemax(S)`](@ref).
41
"""
42
maxintfloat(::Type{S}, ::Type{T}) where {S<:AbstractFloat, T<:Integer} = min(maxintfloat(S), S(typemax(T)))
6,262,364✔
43
maxintfloat() = maxintfloat(Float64)
1✔
44

45
isinteger(x::AbstractFloat) = (x - trunc(x) == 0)
24,004,703✔
46

47
# See rounding.jl for docstring.
48

49
function round(::Type{T}, x::AbstractFloat, r::RoundingMode) where {T<:Integer}
2,180,262✔
50
    r != RoundToZero && (x = round(x,r))
11,197,877✔
51
    trunc(T, x)
11,197,890✔
52
end
53

54
# NOTE: this relies on the current keyword dispatch behaviour (#9498).
55
function round(x::Real, r::RoundingMode=RoundNearest;
716,783✔
56
               digits::Union{Nothing,Integer}=nothing, sigdigits::Union{Nothing,Integer}=nothing, base::Union{Nothing,Integer}=nothing)
57
    if digits === nothing
280✔
58
        if sigdigits === nothing
47✔
59
            if base === nothing
1✔
60
                # avoid recursive calls
61
                throw(MethodError(round, (x,r)))
×
62
            else
63
                round(x,r)
1✔
64
                # or throw(ArgumentError("`round` cannot use `base` argument without `digits` or `sigdigits` arguments."))
65
            end
66
        else
67
            isfinite(x) || return float(x)
48✔
68
            _round_sigdigits(x, r, sigdigits, base === nothing ? 10 : base)
42✔
69
        end
70
    else
71
        if sigdigits === nothing
233✔
72
            isfinite(x) || return float(x)
22,654✔
73
            _round_digits(x, r, digits, base === nothing ? 10 : base)
22,646✔
74
        else
75
            throw(ArgumentError("`round` cannot use both `digits` and `sigdigits` arguments."))
×
76
        end
77
    end
78
end
79

80
trunc(x::Real; kwargs...) = round(x, RoundToZero; kwargs...)
48,222,085✔
81
floor(x::Real; kwargs...) = round(x, RoundDown; kwargs...)
118,469✔
82
ceil(x::Real; kwargs...)  = round(x, RoundUp; kwargs...)
17,444✔
83

84
# fallbacks
85
trunc(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundToZero; kwargs...)
309✔
86
floor(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundDown; kwargs...)
436,279✔
87
ceil(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundUp; kwargs...)
1,274,199✔
88
round(::Type{T}, x::Real; kwargs...) where {T} = round(T, x, RoundNearest; kwargs...)
20,658,901✔
89

90
round(::Type{T}, x::Real, r::RoundingMode) where {T} = convert(T, round(x, r))
3✔
91

92
round(x::Integer, r::RoundingMode) = x
387✔
93

94
# round x to multiples of 1/invstep
95
function _round_invstep(x, invstep, r::RoundingMode)
142✔
96
    y = round(x * invstep, r) / invstep
22,598✔
97
    if !isfinite(y)
22,598✔
98
        return x
×
99
    end
100
    return y
22,598✔
101
end
102

103
# round x to multiples of 1/(invstepsqrt^2)
104
# Using square root of step prevents overflowing
105
function _round_invstepsqrt(x, invstepsqrt, r::RoundingMode)
9✔
106
    y = round((x * invstepsqrt) * invstepsqrt, r) / invstepsqrt / invstepsqrt
26✔
107
    if !isfinite(y)
26✔
108
        return x
16✔
109
    end
110
    return y
10✔
111
end
112

113
# round x to multiples of step
114
function _round_step(x, step, r::RoundingMode)
6✔
115
    # TODO: use div with rounding mode
116
    y = round(x / step, r) * step
15✔
117
    if !isfinite(y)
15✔
118
        if x > 0
12✔
119
            return (r == RoundUp ? oftype(x, Inf) : zero(x))
4✔
120
        elseif x < 0
8✔
121
            return (r == RoundDown ? -oftype(x, Inf) : -zero(x))
4✔
122
        else
123
            return x
4✔
124
        end
125
    end
126
    return y
3✔
127
end
128

129
function _round_digits(x, r::RoundingMode, digits::Integer, base)
22,639✔
130
    fx = float(x)
157✔
131
    if digits >= 0
22,639✔
132
        invstep = oftype(fx, base)^digits
45,227✔
133
        if isfinite(invstep)
22,624✔
134
            return _round_invstep(fx, invstep, r)
22,598✔
135
        else
136
            invstepsqrt = oftype(fx, base)^oftype(fx, digits/2)
26✔
137
            return _round_invstepsqrt(fx, invstepsqrt, r)
26✔
138
        end
139
    else
140
        step = oftype(fx, base)^-digits
24✔
141
        return _round_step(fx, step, r)
15✔
142
    end
143
end
144

145
hidigit(x::Integer, base) = ndigits0z(x, base)
×
146
function hidigit(x::AbstractFloat, base)
42✔
147
    iszero(x) && return 0
42✔
148
    if base == 10
40✔
149
        return 1 + floor(Int, log10(abs(x)))
36✔
150
    elseif base == 2
4✔
151
        return 1 + exponent(x)
3✔
152
    else
153
        return 1 + floor(Int, log(base, abs(x)))
1✔
154
    end
155
end
156
hidigit(x::Real, base) = hidigit(float(x), base)
3✔
157

158
function _round_sigdigits(x, r::RoundingMode, sigdigits::Integer, base)
42✔
159
    h = hidigit(x, base)
42✔
160
    _round_digits(x, r, sigdigits-h, base)
42✔
161
end
162

163
# C-style round
164
function round(x::AbstractFloat, ::RoundingMode{:NearestTiesAway})
50,594✔
165
    y = trunc(x)
50,594✔
166
    ifelse(x==y,y,trunc(2*x-y))
50,594✔
167
end
168
# Java-style round
169
function round(x::T, ::RoundingMode{:NearestTiesUp}) where {T <: AbstractFloat}
50,594✔
170
    copysign(floor((x + (T(0.25) - eps(T(0.5)))) + (T(0.25) + eps(T(0.5)))), x)
50,594✔
171
end
172

173
function Base.round(x::AbstractFloat, ::typeof(RoundFromZero))
50,552✔
174
    signbit(x) ? round(x, RoundDown) : round(x, RoundUp)
51,076✔
175
end
176

177
# isapprox: approximate equality of numbers
178
"""
179
    isapprox(x, y; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps, nans::Bool=false[, norm::Function])
180

181
Inexact equality comparison. Two numbers compare equal if their relative distance *or* their
182
absolute distance is within tolerance bounds: `isapprox` returns `true` if
183
`norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`. The default `atol` (absolute tolerance) is zero and the
184
default `rtol` (relative tolerance) depends on the types of `x` and `y`. The keyword argument `nans` determines
185
whether or not NaN values are considered equal (defaults to false).
186

187
For real or complex floating-point values, if an `atol > 0` is not specified, `rtol` defaults to
188
the square root of [`eps`](@ref) of the type of `x` or `y`, whichever is bigger (least precise).
189
This corresponds to requiring equality of about half of the significant digits. Otherwise,
190
e.g. for integer arguments or if an `atol > 0` is supplied, `rtol` defaults to zero.
191

192
The `norm` keyword defaults to `abs` for numeric `(x,y)` and to `LinearAlgebra.norm` for
193
arrays (where an alternative `norm` choice is sometimes useful).
194
When `x` and `y` are arrays, if `norm(x-y)` is not finite (i.e. `±Inf`
195
or `NaN`), the comparison falls back to checking whether all elements of `x` and `y` are
196
approximately equal component-wise.
197

198
The binary operator `≈` is equivalent to `isapprox` with the default arguments, and `x ≉ y`
199
is equivalent to `!isapprox(x,y)`.
200

201
Note that `x ≈ 0` (i.e., comparing to zero with the default tolerances) is
202
equivalent to `x == 0` since the default `atol` is `0`.  In such cases, you should either
203
supply an appropriate `atol` (or use `norm(x) ≤ atol`) or rearrange your code (e.g.
204
use `x ≈ y` rather than `x - y ≈ 0`).   It is not possible to pick a nonzero `atol`
205
automatically because it depends on the overall scaling (the "units") of your problem:
206
for example, in `x - y ≈ 0`, `atol=1e-9` is an absurdly small tolerance if `x` is the
207
[radius of the Earth](https://en.wikipedia.org/wiki/Earth_radius) in meters,
208
but an absurdly large tolerance if `x` is the
209
[radius of a Hydrogen atom](https://en.wikipedia.org/wiki/Bohr_radius) in meters.
210

211
!!! compat "Julia 1.6"
212
    Passing the `norm` keyword argument when comparing numeric (non-array) arguments
213
    requires Julia 1.6 or later.
214

215
# Examples
216
```jldoctest
217
julia> isapprox(0.1, 0.15; atol=0.05)
218
true
219

220
julia> isapprox(0.1, 0.15; rtol=0.34)
221
true
222

223
julia> isapprox(0.1, 0.15; rtol=0.33)
224
false
225

226
julia> 0.1 + 1e-10 ≈ 0.1
227
true
228

229
julia> 1e-10 ≈ 0
230
false
231

232
julia> isapprox(1e-10, 0, atol=1e-8)
233
true
234

235
julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0]) # using `norm`
236
true
237
```
238
"""
239
function isapprox(x::Number, y::Number;
730,945✔
240
                  atol::Real=0, rtol::Real=rtoldefault(x,y,atol),
241
                  nans::Bool=false, norm::Function=abs)
242
    x == y || (isfinite(x) && isfinite(y) && norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))) || (nans && isnan(x) && isnan(y))
379,420✔
243
end
244

245
"""
246
    isapprox(x; kwargs...) / ≈(x; kwargs...)
247

248
Create a function that compares its argument to `x` using `≈`, i.e. a function equivalent to `y -> y ≈ x`.
249

250
The keyword arguments supported here are the same as those in the 2-argument `isapprox`.
251

252
!!! compat "Julia 1.5"
253
    This method requires Julia 1.5 or later.
254
"""
255
isapprox(y; kwargs...) = x -> isapprox(x, y; kwargs...)
199✔
256

257
const ≈ = isapprox
258
"""
259
    x ≉ y
260

261
This is equivalent to `!isapprox(x,y)` (see [`isapprox`](@ref)).
262
"""
263
≉(args...; kws...) = !≈(args...; kws...)
58✔
264

265
# default tolerance arguments
266
rtoldefault(::Type{T}) where {T<:AbstractFloat} = sqrt(eps(T))
776,962✔
267
rtoldefault(::Type{<:Real}) = 0
11,042✔
268
function rtoldefault(x::Union{T,Type{T}}, y::Union{S,Type{S}}, atol::Real) where {T<:Number,S<:Number}
392,178✔
269
    rtol = max(rtoldefault(real(T)),rtoldefault(real(S)))
392,178✔
270
    return atol > 0 ? zero(rtol) : rtol
394,886✔
271
end
272

273
# fused multiply-add
274

275
"""
276
    fma(x, y, z)
277

278
Computes `x*y+z` without rounding the intermediate result `x*y`. On some systems this is
279
significantly more expensive than `x*y+z`. `fma` is used to improve accuracy in certain
280
algorithms. See [`muladd`](@ref).
281
"""
282
function fma end
283
function fma_emulated(a::Float32, b::Float32, c::Float32)::Float32
262,155✔
284
    ab = Float64(a) * b
262,155✔
285
    res = ab+c
262,155✔
286
    reinterpret(UInt64, res)&0x1fff_ffff!=0x1000_0000 && return res
262,155✔
287
    # yes error compensation is necessary. It sucks
288
    reslo = abs(c)>abs(ab) ? ab-(res - c) : c-(res - ab)
4✔
289
    res = iszero(reslo) ? res : (signbit(reslo) ? prevfloat(res) : nextfloat(res))
3✔
290
    return res
2✔
291
end
292

293
""" Splits a Float64 into a hi bit and a low bit where the high bit has 27 trailing 0s and the low bit has 26 trailing 0s"""
294
@inline function splitbits(x::Float64)
×
295
    hi = reinterpret(Float64, reinterpret(UInt64, x) & 0xffff_ffff_f800_0000)
923,397✔
296
    return hi, x-hi
923,397✔
297
end
298

299
function twomul(a::Float64, b::Float64)
×
300
    ahi, alo = splitbits(a)
307,799✔
301
    bhi, blo = splitbits(b)
307,799✔
302
    abhi = a*b
307,799✔
303
    blohi, blolo = splitbits(blo)
307,799✔
304
    ablo = alo*blohi - (((abhi - ahi*bhi) - alo*bhi) - ahi*blo) + blolo*alo
307,799✔
305
    return abhi, ablo
×
306
end
307

308
function fma_emulated(a::Float64, b::Float64,c::Float64)
262,160✔
309
    abhi, ablo = @inline twomul(a,b)
262,160✔
310
    if !isfinite(abhi+c) || isless(abs(abhi), nextfloat(0x1p-969)) || issubnormal(a) || issubnormal(b)
491,060✔
311
        aandbfinite = isfinite(a) && isfinite(b)
69,652✔
312
        if !(isfinite(c) && aandbfinite)
69,652✔
313
            return aandbfinite ? c : abhi+c
376✔
314
        end
315
        (iszero(a) || iszero(b)) && return abhi+c
69,276✔
316
        # The checks above satisfy exponent's nothrow precondition
317
        bias = Math._exponent_finite_nonzero(a) + Math._exponent_finite_nonzero(b)
69,275✔
318
        c_denorm = ldexp(c, -bias)
138,550✔
319
        if isfinite(c_denorm)
69,275✔
320
            # rescale a and b to [1,2), equivalent to ldexp(a, -exponent(a))
321
            issubnormal(a) && (a *= 0x1p52)
45,639✔
322
            issubnormal(b) && (b *= 0x1p52)
45,639✔
323
            a = reinterpret(Float64, (reinterpret(UInt64, a) & ~Base.exponent_mask(Float64)) | Base.exponent_one(Float64))
45,639✔
324
            b = reinterpret(Float64, (reinterpret(UInt64, b) & ~Base.exponent_mask(Float64)) | Base.exponent_one(Float64))
45,639✔
325
            c = c_denorm
×
326
            abhi, ablo = twomul(a,b)
45,639✔
327
            # abhi <= 4 -> isfinite(r)      (α)
328
            r = abhi+c
45,639✔
329
            # s ≈ 0                         (β)
330
            s = (abs(abhi) > abs(c)) ? (abhi-r+c+ablo) : (c-r+abhi+ablo)
58,321✔
331
            # α ⩓ β -> isfinite(sumhi)      (γ)
332
            sumhi = r+s
45,639✔
333
            # If result is subnormal, ldexp will cause double rounding because subnormals have fewer mantisa bits.
334
            # As such, we need to check whether round to even would lead to double rounding and manually round sumhi to avoid it.
335
            if issubnormal(ldexp(sumhi, bias))
91,278✔
336
                sumlo = r-sumhi+s
15✔
337
                # finite: See γ
338
                # non-zero: If sumhi == ±0., then ldexp(sumhi, bias) == ±0,
339
                # so we don't take this branch.
340
                bits_lost = -bias-Math._exponent_finite_nonzero(sumhi)-1022
15✔
341
                sumhiInt = reinterpret(UInt64, sumhi)
15✔
342
                if (bits_lost != 1) ⊻ (sumhiInt&1 == 1)
15✔
343
                    sumhi = nextfloat(sumhi, cmp(sumlo,0))
20✔
344
                end
345
            end
346
            return ldexp(sumhi, bias)
45,639✔
347
        end
348
        isinf(abhi) && signbit(c) == signbit(a*b) && return abhi
23,636✔
349
        # fall through
350
    end
351
    r = abhi+c
216,144✔
352
    s = (abs(abhi) > abs(c)) ? (abhi-r+c+ablo) : (c-r+abhi+ablo)
334,019✔
353
    return r+s
216,144✔
354
end
355
fma_llvm(x::Float32, y::Float32, z::Float32) = fma_float(x, y, z)
2,448,598✔
356
fma_llvm(x::Float64, y::Float64, z::Float64) = fma_float(x, y, z)
404,384,773✔
357

358
# Disable LLVM's fma if it is incorrect, e.g. because LLVM falls back
359
# onto a broken system libm; if so, use a software emulated fma
360
@assume_effects :consistent fma(x::Float32, y::Float32, z::Float32) = Core.Intrinsics.have_fma(Float32) ? fma_llvm(x,y,z) : fma_emulated(x,y,z)
2,448,598✔
361
@assume_effects :consistent fma(x::Float64, y::Float64, z::Float64) = Core.Intrinsics.have_fma(Float64) ? fma_llvm(x,y,z) : fma_emulated(x,y,z)
404,384,773✔
362

363
function fma(a::Float16, b::Float16, c::Float16)
499,778✔
364
    Float16(muladd(Float32(a), Float32(b), Float32(c))) #don't use fma if the hardware doesn't have it.
499,778✔
365
end
366

367
# This is necessary at least on 32-bit Intel Linux, since fma_llvm may
368
# have called glibc, and some broken glibc fma implementations don't
369
# properly restore the rounding mode
370
Rounding.setrounding_raw(Float32, Rounding.JL_FE_TONEAREST)
371
Rounding.setrounding_raw(Float64, Rounding.JL_FE_TONEAREST)
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