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

JuliaLang / julia / #38064

08 May 2025 12:35AM UTC coverage: 25.677% (-0.08%) from 25.752%
#38064

push

local

web-flow
Add default for inline command line switch (#58230)

Fixes small discrepancy between man page and documentation.

12790 of 49812 relevant lines covered (25.68%)

708025.35 hits per line

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

3.88
/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)
12✔
6
copysign(x::Float32, y::Float32) = copysign_float(x, y)
×
7
copysign(x::Float32, y::Real) = copysign(x, Float32(y))
×
8
copysign(x::Float64, y::Real) = copysign(x, Float64(y))
×
9

10
flipsign(x::Float64, y::Float64) = bitcast(Float64, xor_int(bitcast(UInt64, x), and_int(bitcast(UInt64, y), 0x8000000000000000)))
×
11
flipsign(x::Float32, y::Float32) = bitcast(Float32, xor_int(bitcast(UInt32, x), and_int(bitcast(UInt32, y), 0x80000000)))
×
12
flipsign(x::Float32, y::Real) = flipsign(x, Float32(y))
×
13
flipsign(x::Float64, y::Real) = flipsign(x, Float64(y))
×
14

15
signbit(x::Float64) = signbit(bitcast(Int64, x))
×
16
signbit(x::Float32) = signbit(bitcast(Int32, x))
×
17
signbit(x::Float16) = signbit(bitcast(Int16, x))
×
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
See also: [`typemax`](@ref), [`floatmax`](@ref).
31
"""
32
maxintfloat(::Type{Float64}) = 9007199254740992.
×
33
maxintfloat(::Type{Float32}) = Float32(16777216.)
×
34
maxintfloat(::Type{Float16}) = Float16(2048f0)
×
35
maxintfloat(x::T) where {T<:AbstractFloat} = maxintfloat(T)
×
36

37
"""
38
    maxintfloat(T, S)
39

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

47
isinteger(x::AbstractFloat) = iszero(x - trunc(x)) # note: x == trunc(x) would be incorrect for x=Inf
5,778,036✔
48

49
# See rounding.jl for docstring.
50

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

77
# round x to multiples of 1/invstep
78
function _round_invstep(x, invstep, r::RoundingMode)
79
    y = round(x * invstep, r) / invstep
×
80
    if !isfinite(y)
×
81
        return x
×
82
    end
83
    return y
×
84
end
85

86
# round x to multiples of 1/(invstepsqrt^2)
87
# Using square root of step prevents overflowing
88
function _round_invstepsqrt(x, invstepsqrt, r::RoundingMode)
89
    y = round((x * invstepsqrt) * invstepsqrt, r) / invstepsqrt / invstepsqrt
×
90
    if !isfinite(y)
×
91
        return x
×
92
    end
93
    return y
×
94
end
95

96
# round x to multiples of step
97
function _round_step(x, step, r::RoundingMode)
98
    # TODO: use div with rounding mode
99
    y = round(x / step, r) * step
×
100
    if !isfinite(y)
×
101
        if x > 0
×
102
            return (r == RoundUp ? oftype(x, Inf) : zero(x))
×
103
        elseif x < 0
×
104
            return (r == RoundDown ? -oftype(x, Inf) : -zero(x))
×
105
        else
106
            return x
×
107
        end
108
    end
109
    return y
×
110
end
111

112
function _round_digits(x, r::RoundingMode, digits::Integer, base)
×
113
    fx = float(x)
×
114
    if digits >= 0
×
115
        invstep = oftype(fx, base)^digits
×
116
        if isfinite(invstep)
×
117
            return _round_invstep(fx, invstep, r)
×
118
        else
119
            invstepsqrt = oftype(fx, base)^oftype(fx, digits/2)
×
120
            return _round_invstepsqrt(fx, invstepsqrt, r)
×
121
        end
122
    else
123
        step = oftype(fx, base)^-digits
×
124
        return _round_step(fx, step, r)
×
125
    end
126
end
127

128
hidigit(x::Integer, base) = ndigits0z(x, base)
×
129
function hidigit(x::AbstractFloat, base)
×
130
    iszero(x) && return 0
×
131
    if base == 10
×
132
        return 1 + floor(Int, log10(abs(x)))
×
133
    elseif base == 2
×
134
        return 1 + exponent(x)
×
135
    else
136
        return 1 + floor(Int, log(base, abs(x)))
×
137
    end
138
end
139
hidigit(x::Real, base) = hidigit(float(x), base)
×
140

141
function _round_sigdigits(x, r::RoundingMode, sigdigits::Integer, base)
×
142
    h = hidigit(x, base)
×
143
    _round_digits(x, r, sigdigits-h, base)
×
144
end
145

146
# C-style round
147
function round(x::AbstractFloat, ::RoundingMode{:NearestTiesAway})
×
148
    y = trunc(x)
×
149
    ifelse(x==y,y,trunc(2*x-y))
×
150
end
151
# Java-style round
152
function round(x::T, ::RoundingMode{:NearestTiesUp}) where {T <: AbstractFloat}
×
153
    copysign(floor((x + (T(0.25) - eps(T(0.5)))) + (T(0.25) + eps(T(0.5)))), x)
×
154
end
155

156
function Base.round(x::AbstractFloat, ::typeof(RoundFromZero))
×
157
    signbit(x) ? round(x, RoundDown) : round(x, RoundUp)
×
158
end
159

160
# isapprox: approximate equality of numbers
161
"""
162
    isapprox(x, y; atol::Real=0, rtol::Real=atol>0 ? 0 : √eps, nans::Bool=false[, norm::Function])
163

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

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

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

181
The binary operator `≈` is equivalent to `isapprox` with the default arguments, and `x ≉ y`
182
is equivalent to `!isapprox(x,y)`.
183

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

194
!!! compat "Julia 1.6"
195
    Passing the `norm` keyword argument when comparing numeric (non-array) arguments
196
    requires Julia 1.6 or later.
197

198
# Examples
199
```jldoctest
200
julia> isapprox(0.1, 0.15; atol=0.05)
201
true
202

203
julia> isapprox(0.1, 0.15; rtol=0.34)
204
true
205

206
julia> isapprox(0.1, 0.15; rtol=0.33)
207
false
208

209
julia> 0.1 + 1e-10 ≈ 0.1
210
true
211

212
julia> 1e-10 ≈ 0
213
false
214

215
julia> isapprox(1e-10, 0, atol=1e-8)
216
true
217

218
julia> isapprox([10.0^9, 1.0], [10.0^9, 2.0]) # using `norm`
219
true
220
```
221
"""
222
function isapprox(x::Number, y::Number;
×
223
                  atol::Real=0, rtol::Real=rtoldefault(x,y,atol),
224
                  nans::Bool=false, norm::Function=abs)
225
    x′, y′ = promote(x, y) # to avoid integer overflow
×
226
    x == y ||
×
227
        (isfinite(x) && isfinite(y) && norm(x-y) <= max(atol, rtol*max(norm(x′), norm(y′)))) ||
228
         (nans && isnan(x) && isnan(y))
229
end
230

231
function isapprox(x::Integer, y::Integer;
×
232
                  atol::Real=0, rtol::Real=rtoldefault(x,y,atol),
233
                  nans::Bool=false, norm::Function=abs)
234
    if norm === abs && atol < 1 && rtol == 0
×
235
        return x == y
×
236
    else
237
        # We need to take the difference `max` - `min` when comparing unsigned integers.
238
        _x, _y = x < y ? (x, y) : (y, x)
×
239
        return norm(_y - _x) <= max(atol, rtol*max(norm(_x), norm(_y)))
×
240
    end
241
end
242

243
"""
244
    isapprox(x; kwargs...) / ≈(x; kwargs...)
245

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

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

250
!!! compat "Julia 1.5"
251
    This method requires Julia 1.5 or later.
252
"""
253
isapprox(y; kwargs...) = x -> isapprox(x, y; kwargs...)
×
254

255
const ≈ = isapprox
256
"""
257
    x ≉ y
258

259
This is equivalent to `!isapprox(x,y)` (see [`isapprox`](@ref)).
260
"""
261
≉(args...; kws...) = !≈(args...; kws...)
×
262

263
# default tolerance arguments
264
rtoldefault(::Type{T}) where {T<:AbstractFloat} = sqrt(eps(T))
×
265
rtoldefault(::Type{<:Real}) = 0
×
266
function rtoldefault(x::Union{T,Type{T}}, y::Union{S,Type{S}}, atol::Real) where {T<:Number,S<:Number}
×
267
    rtol = max(rtoldefault(real(T)),rtoldefault(real(S)))
×
268
    return atol > 0 ? zero(rtol) : rtol
×
269
end
270

271
# fused multiply-add
272

273
"""
274
    fma(x, y, z)
275

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

294
""" 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"""
295
@inline function splitbits(x::Float64)
296
    hi = reinterpret(Float64, reinterpret(UInt64, x) & 0xffff_ffff_f800_0000)
×
297
    return hi, x-hi
×
298
end
299

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

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

357
# Disable LLVM's fma if it is incorrect, e.g. because LLVM falls back
358
# onto a broken system libm; if so, use a software emulated fma
359
@assume_effects :consistent function fma(x::T, y::T, z::T) where {T<:IEEEFloat}
360
    Core.Intrinsics.have_fma(T) ? fma_float(x,y,z) : fma_emulated(x,y,z)
×
361
end
362

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