Skip to content

Commit 71b5e83

Browse files
simonbyrneJeffBezanson
authored andcommitted
Deprecate setrounding (#27166)
Fixes #26935. This is the "minimal" change of deprecating Float32/Float64 support.
1 parent 082be9a commit 71b5e83

File tree

5 files changed

+17
-111
lines changed

5 files changed

+17
-111
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,8 @@ Deprecated or removed
12481248

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

1251+
* `setrounding` has been deprecated for `Float32` and `Float64`, as the behaviour was too unreliable ([#26935]).
1252+
12511253
Command-line option changes
12521254
---------------------------
12531255

@@ -1560,3 +1562,4 @@ Command-line option changes
15601562
[#26670]: https://github.com/JuliaLang/julia/issues/26670
15611563
[#26775]: https://github.com/JuliaLang/julia/issues/26775
15621564
[#26932]: https://github.com/JuliaLang/julia/issues/26932
1565+
[#26935]: https://github.com/JuliaLang/julia/issues/26935

base/deprecated.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,11 @@ end
16601660
@deprecate next(s::AbstractString, i::Integer) iterate(s, i)
16611661
@deprecate done(s::AbstractString, i::Integer) i > ncodeunits(s)
16621662

1663+
function Rounding.setrounding(::Type{T}, r::RoundingMode) where {T<:Union{Float32,Float64}}
1664+
depwarn("""`setrounding` for `Float32` and `Float64` has been deprecated, and will not be available in future versions.""", :setrounding)
1665+
Rounding.setrounding_raw(T, Rounding.to_fenv(r))
1666+
end
1667+
16631668
# #27140, #27152
16641669
@deprecate_moved cor "StatsBase"
16651670
@deprecate_moved cov "StatsBase"

base/rounding.jl

+1-30
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,7 @@ arithmetic functions ([`+`](@ref), [`-`](@ref), [`*`](@ref),
118118
functions may give incorrect or invalid values when using rounding modes other than the
119119
default `RoundNearest`.
120120
121-
Note that this may affect other types, for instance changing the rounding mode of
122-
[`Float64`](@ref) will change the rounding mode of [`Float32`](@ref).
123-
See [`RoundingMode`](@ref) for available modes.
124-
125-
!!! warning
126-
127-
This feature is still experimental, and may give unexpected or incorrect values.
121+
Note that this is currently only supported for `T == BigFloat`.
128122
"""
129123
setrounding(T::Type, mode)
130124

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

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

148141
"""
@@ -157,28 +150,6 @@ equivalent to:
157150
setrounding(T, old)
158151
159152
See [`RoundingMode`](@ref) for available rounding modes.
160-
161-
!!! warning
162-
163-
This feature is still experimental, and may give unexpected or incorrect values. A
164-
known problem is the interaction with compiler optimisations, e.g.
165-
166-
julia> setrounding(Float64,RoundDown) do
167-
1.1 + 0.1
168-
end
169-
1.2000000000000002
170-
171-
Here the compiler is *constant folding*, that is evaluating a known constant
172-
expression at compile time, however the rounding mode is only changed at runtime, so
173-
this is not reflected in the function result. This can be avoided by moving constants
174-
outside the expression, e.g.
175-
176-
julia> x = 1.1; y = 0.1;
177-
178-
julia> setrounding(Float64,RoundDown) do
179-
x + y
180-
end
181-
1.2
182153
"""
183154
function setrounding(f::Function, ::Type{T}, rounding::RoundingMode) where T
184155
old_rounding_raw = rounding_raw(T)

doc/src/manual/integers-and-floating-point-numbers.md

-18
Original file line numberDiff line numberDiff line change
@@ -496,27 +496,9 @@ appropriate representable value. However, the manner in which this rounding is d
496496
changed if required according to the rounding modes presented in the [IEEE 754
497497
standard](https://en.wikipedia.org/wiki/IEEE_754-2008).
498498

499-
```jldoctest
500-
julia> x = 1.1; y = 0.1;
501-
502-
julia> x + y
503-
1.2000000000000002
504-
505-
julia> setrounding(Float64,RoundDown) do
506-
x + y
507-
end
508-
1.2
509-
```
510-
511499
The default mode used is always [`RoundNearest`](@ref), which rounds to the nearest representable
512500
value, with ties rounded towards the nearest value with an even least significant bit.
513501

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

522504
Floating-point arithmetic entails many subtleties which can be surprising to users who are unfamiliar

test/rounding.jl

+8-63
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,6 @@ using Test
1919
@test a - b === -c
2020
@test b - a === c
2121
end
22-
@testset "RoundToZero" begin
23-
setrounding(Float64,RoundToZero) do
24-
@test a + b === d
25-
@test - a - b === -d
26-
@test a - b === -c
27-
@test b - a === c
28-
end
29-
# Sanity check to see if we have returned to RoundNearest
30-
@test a + b === 1.
31-
@test - a - b === -1.
32-
@test a - b == -c
33-
@test b - a == c
34-
end
35-
36-
@testset "RoundUp" begin
37-
setrounding(Float64,RoundUp) do
38-
@test a + b === 1.
39-
@test - a - b === -d
40-
@test a - b === -c
41-
@test b - a === c
42-
end
43-
end
44-
@testset "RoundDown" begin
45-
setrounding(Float64,RoundDown) do
46-
@test a + b === d
47-
@test - a - b === -1.
48-
@test a - b === -c
49-
@test b - a === c
50-
end
51-
end
5222
end
5323

5424
@testset "Float32 checks" begin
@@ -63,49 +33,24 @@ end
6333
@test a32 - b32 === -c32
6434
@test b32 - a32 === c32
6535
end
66-
@testset "RoundToZero" begin
67-
setrounding(Float32,RoundToZero) do
68-
@test a32 + b32 === d32
69-
@test - a32 - b32 === -d32
70-
@test a32 - b32 === -c32
71-
@test b32 - a32 === c32
72-
end
73-
74-
# Sanity check to see if we have returned to RoundNearest
75-
@test a32 + b32 === 1.0f0
76-
@test - a32 - b32 === -1.0f0
77-
@test a32 - b32 == -c32
78-
@test b32 - a32 == c32
79-
end
80-
@testset "RoundUp" begin
81-
setrounding(Float32,RoundUp) do
82-
@test a32 + b32 === 1.0f0
83-
@test - a32 - b32 === -d32
84-
@test a32 - b32 === -c32
85-
@test b32 - a32 === c32
86-
end
87-
end
88-
@testset "RoundDown" begin
89-
setrounding(Float32,RoundDown) do
90-
@test a32 + b32 === d32
91-
@test - a32 - b32 === -1.0f0
92-
@test a32 - b32 === -c32
93-
@test b32 - a32 === c32
94-
end
95-
end
9636
end
9737

9838
@testset "convert with rounding" begin
9939
for v = [sqrt(2),-1/3,nextfloat(1.0),prevfloat(1.0),nextfloat(-1.0),
10040
prevfloat(-1.0),nextfloat(0.0),prevfloat(0.0)]
41+
10142
pn = Float32(v,RoundNearest)
10243
@test pn == convert(Float32,v)
44+
10345
pz = Float32(v,RoundToZero)
104-
@test pz == setrounding(()->convert(Float32,v), Float64, RoundToZero)
46+
@test abs(pz) <= abs(v) < nextfloat(abs(pz))
47+
@test signbit(pz) == signbit(v)
48+
10549
pd = Float32(v,RoundDown)
106-
@test pd == setrounding(()->convert(Float32,v), Float64, RoundDown)
50+
@test pd <= v < nextfloat(pd)
51+
10752
pu = Float32(v,RoundUp)
108-
@test pu == setrounding(()->convert(Float32,v), Float64, RoundUp)
53+
@test prevfloat(pu) < v <= pu
10954

11055
@test pn == pd || pn == pu
11156
@test v > 0 ? pz == pd : pz == pu

0 commit comments

Comments
 (0)