Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate setrounding #27166

Merged
merged 6 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,8 @@ Deprecated or removed

* `signif` has been deprecated in favor of the `sigdigits` keyword argument to `round`.

* `setrounding` has been deprecated for `Float32` and `Float64`, as the behaviour was too unreliable ([#26935]).

Command-line option changes
---------------------------

Expand Down Expand Up @@ -1560,3 +1562,4 @@ Command-line option changes
[#26670]: https://github.com/JuliaLang/julia/issues/26670
[#26775]: https://github.com/JuliaLang/julia/issues/26775
[#26932]: https://github.com/JuliaLang/julia/issues/26932
[#26935]: https://github.com/JuliaLang/julia/issues/26935
5 changes: 5 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,11 @@ end
@deprecate next(s::AbstractString, i::Integer) iterate(s, i)
@deprecate done(s::AbstractString, i::Integer) i > ncodeunits(s)

function Rounding.setrounding(::Type{T}, r::RoundingMode) where {T<:Union{Float32,Float64}}
depwarn("""`setrounding` for `Float32` and `Float64` has been deprecated, and will not be available in future versions.""", :setrounding)
Rounding.setrounding_raw(T, Rounding.to_fenv(r))
end

# #27140, #27152
@deprecate_moved cor "StatsBase"
@deprecate_moved cov "StatsBase"
Expand Down
31 changes: 1 addition & 30 deletions base/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,7 @@ arithmetic functions ([`+`](@ref), [`-`](@ref), [`*`](@ref),
functions may give incorrect or invalid values when using rounding modes other than the
default `RoundNearest`.

Note that this may affect other types, for instance changing the rounding mode of
[`Float64`](@ref) will change the rounding mode of [`Float32`](@ref).
See [`RoundingMode`](@ref) for available modes.

!!! warning

This feature is still experimental, and may give unexpected or incorrect values.
Note that this is currently only supported for `T == BigFloat`.
"""
setrounding(T::Type, mode)

Expand All @@ -142,7 +136,6 @@ See [`RoundingMode`](@ref) for available modes.
setrounding_raw(::Type{<:Union{Float32,Float64}}, i::Integer) = ccall(:fesetround, Int32, (Int32,), i)
rounding_raw(::Type{<:Union{Float32,Float64}}) = ccall(:fegetround, Int32, ())

setrounding(::Type{T}, r::RoundingMode) where {T<:Union{Float32,Float64}} = setrounding_raw(T,to_fenv(r))
rounding(::Type{T}) where {T<:Union{Float32,Float64}} = from_fenv(rounding_raw(T))

"""
Expand All @@ -157,28 +150,6 @@ equivalent to:
setrounding(T, old)

See [`RoundingMode`](@ref) for available rounding modes.

!!! warning

This feature is still experimental, and may give unexpected or incorrect values. A
known problem is the interaction with compiler optimisations, e.g.

julia> setrounding(Float64,RoundDown) do
1.1 + 0.1
end
1.2000000000000002

Here the compiler is *constant folding*, that is evaluating a known constant
expression at compile time, however the rounding mode is only changed at runtime, so
this is not reflected in the function result. This can be avoided by moving constants
outside the expression, e.g.

julia> x = 1.1; y = 0.1;

julia> setrounding(Float64,RoundDown) do
x + y
end
1.2
"""
function setrounding(f::Function, ::Type{T}, rounding::RoundingMode) where T
old_rounding_raw = rounding_raw(T)
Expand Down
18 changes: 0 additions & 18 deletions doc/src/manual/integers-and-floating-point-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,27 +496,9 @@ appropriate representable value. However, the manner in which this rounding is d
changed if required according to the rounding modes presented in the [IEEE 754
standard](https://en.wikipedia.org/wiki/IEEE_754-2008).

```jldoctest
julia> x = 1.1; y = 0.1;

julia> x + y
1.2000000000000002

julia> setrounding(Float64,RoundDown) do
x + y
end
1.2
```

The default mode used is always [`RoundNearest`](@ref), which rounds to the nearest representable
value, with ties rounded towards the nearest value with an even least significant bit.

!!! warning
Rounding is generally only correct for basic arithmetic functions ([`+`](@ref), [`-`](@ref),
[`*`](@ref), [`/`](@ref) and [`sqrt`](@ref)) and type conversion operations. Many other
functions assume the default [`RoundNearest`](@ref) mode is set, and can give erroneous results
when operating under other rounding modes.

### Background and References

Floating-point arithmetic entails many subtleties which can be surprising to users who are unfamiliar
Expand Down
71 changes: 8 additions & 63 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,6 @@ using Test
@test a - b === -c
@test b - a === c
end
@testset "RoundToZero" begin
setrounding(Float64,RoundToZero) do
@test a + b === d
@test - a - b === -d
@test a - b === -c
@test b - a === c
end
# Sanity check to see if we have returned to RoundNearest
@test a + b === 1.
@test - a - b === -1.
@test a - b == -c
@test b - a == c
end

@testset "RoundUp" begin
setrounding(Float64,RoundUp) do
@test a + b === 1.
@test - a - b === -d
@test a - b === -c
@test b - a === c
end
end
@testset "RoundDown" begin
setrounding(Float64,RoundDown) do
@test a + b === d
@test - a - b === -1.
@test a - b === -c
@test b - a === c
end
end
end

@testset "Float32 checks" begin
Expand All @@ -63,49 +33,24 @@ end
@test a32 - b32 === -c32
@test b32 - a32 === c32
end
@testset "RoundToZero" begin
setrounding(Float32,RoundToZero) do
@test a32 + b32 === d32
@test - a32 - b32 === -d32
@test a32 - b32 === -c32
@test b32 - a32 === c32
end

# Sanity check to see if we have returned to RoundNearest
@test a32 + b32 === 1.0f0
@test - a32 - b32 === -1.0f0
@test a32 - b32 == -c32
@test b32 - a32 == c32
end
@testset "RoundUp" begin
setrounding(Float32,RoundUp) do
@test a32 + b32 === 1.0f0
@test - a32 - b32 === -d32
@test a32 - b32 === -c32
@test b32 - a32 === c32
end
end
@testset "RoundDown" begin
setrounding(Float32,RoundDown) do
@test a32 + b32 === d32
@test - a32 - b32 === -1.0f0
@test a32 - b32 === -c32
@test b32 - a32 === c32
end
end
end

@testset "convert with rounding" begin
for v = [sqrt(2),-1/3,nextfloat(1.0),prevfloat(1.0),nextfloat(-1.0),
prevfloat(-1.0),nextfloat(0.0),prevfloat(0.0)]

pn = Float32(v,RoundNearest)
@test pn == convert(Float32,v)

pz = Float32(v,RoundToZero)
@test pz == setrounding(()->convert(Float32,v), Float64, RoundToZero)
@test abs(pz) <= abs(v) < nextfloat(abs(pz))
@test signbit(pz) == signbit(v)

pd = Float32(v,RoundDown)
@test pd == setrounding(()->convert(Float32,v), Float64, RoundDown)
@test pd <= v < nextfloat(pd)

pu = Float32(v,RoundUp)
@test pu == setrounding(()->convert(Float32,v), Float64, RoundUp)
@test prevfloat(pu) < v <= pu

@test pn == pd || pn == pu
@test v > 0 ? pz == pd : pz == pu
Expand Down