Skip to content

Commit e948b41

Browse files
simonbyrnetkelman
authored andcommitted
Expand docs for eps (JuliaLang#20597)
* Expand docs for eps Fixed JuliaLang#20542 and JuliaLang#13447. * add doctests * clarify rationale, US spelling, use where
1 parent cb1aae9 commit e948b41

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed

base/docs/helpdb/Base.jl

-22
Original file line numberDiff line numberDiff line change
@@ -1127,28 +1127,6 @@ Delete the mapping for the given key in a collection, and return the collection.
11271127
"""
11281128
delete!
11291129

1130-
"""
1131-
eps(T)
1132-
1133-
The distance between 1.0 and the next larger representable floating-point value of
1134-
`DataType` `T`. Only floating-point types are sensible arguments.
1135-
"""
1136-
eps(::Union{Type{BigFloat},Type{Float64},Type{Float32},Type{Float16}})
1137-
1138-
"""
1139-
eps()
1140-
1141-
The distance between 1.0 and the next larger representable floating-point value of `Float64`.
1142-
"""
1143-
eps()
1144-
1145-
"""
1146-
eps(x)
1147-
1148-
The distance between `x` and the next larger representable floating-point value of the same
1149-
`DataType` as `x`.
1150-
"""
1151-
eps(::AbstractFloat)
11521130

11531131
"""
11541132
searchsortedfirst(a, x, [by=<transform>,] [lt=<comparison>,] [rev=false])

base/float.jl

+68
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,74 @@ end
711711
eps() = eps(Float64)
712712
end
713713

714+
"""
715+
eps(::Type{T}) where T<:AbstractFloat
716+
eps()
717+
718+
Returns the *machine epsilon* of the floating point type `T` (`T = Float64` by
719+
default). This is defined as the gap between 1 and the next largest value representable by
720+
`T`, and is equivalent to `eps(one(T))`.
721+
722+
```jldoctest
723+
julia> eps()
724+
2.220446049250313e-16
725+
726+
julia> eps(Float32)
727+
1.1920929f-7
728+
729+
julia> 1.0 + eps()
730+
1.0000000000000002
731+
732+
julia> 1.0 + eps()/2
733+
1.0
734+
```
735+
"""
736+
eps(::Type{<:AbstractFloat})
737+
738+
"""
739+
eps(x::AbstractFloat)
740+
741+
Returns the *unit in last place* (ulp) of `x`. This is the distance between consecutive
742+
representable floating point values at `x`. In most cases, if the distance on either side
743+
of `x` is different, then the larger of the two is taken, that is
744+
745+
eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
746+
747+
The exceptions to this rule are the smallest and largest finite values
748+
(e.g. `nextfloat(-Inf)` and `prevfloat(Inf)` for `Float64`), which round to the smaller of
749+
the values.
750+
751+
The rationale for this behavior is that `eps` bounds the floating point rounding
752+
error. Under the default `RoundNearest` rounding mode, if ``y`` is a real number and ``x``
753+
is the nearest floating point number to ``y``, then
754+
755+
```math
756+
|y-x| \\leq \\operatorname{eps}(x)/2.
757+
```
758+
759+
```jldoctest
760+
julia> eps(1.0)
761+
2.220446049250313e-16
762+
763+
julia> eps(prevfloat(2.0))
764+
2.220446049250313e-16
765+
766+
julia> eps(2.0)
767+
4.440892098500626e-16
768+
769+
julia> x = prevfloat(Inf) # largest finite Float64
770+
1.7976931348623157e308
771+
772+
julia> x + eps(x)/2 # rounds up
773+
Inf
774+
775+
julia> x + prevfloat(eps(x)/2) # rounds down
776+
1.7976931348623157e308
777+
```
778+
"""
779+
eps(::AbstractFloat)
780+
781+
714782
## byte order swaps for arbitrary-endianness serialization/deserialization ##
715783
bswap(x::Float32) = bswap_int(x)
716784
bswap(x::Float64) = bswap_int(x)

doc/src/stdlib/base.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ Base.realmin
9797
Base.realmax
9898
Base.maxintfloat
9999
Base.sizeof(::Type)
100-
Base.eps(::Union{Type{BigFloat},Type{Float64},Type{Float32},Type{Float16}})
101-
Base.eps()
100+
Base.eps(::Type{<:AbstractFloat})
102101
Base.eps(::AbstractFloat)
103102
Base.promote_type
104103
Base.promote_rule

0 commit comments

Comments
 (0)