Skip to content

Commit 0d2a36f

Browse files
RafaelAvelar14LilithHafner
andauthoredMar 21, 2025··
add missing _convert_rounding methods for RoundFromZero (#57802)
Implemented `_convert_rounding` method for `RoundingMode{:FromZero}` similar to other `_convert_rounding` methods for other rounding modes. Fix #55820 Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
1 parent a9c654d commit 0d2a36f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎base/rounding.jl

+9
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ function _convert_rounding(::Type{T}, x::Real, r::RoundingMode{:ToZero}) where T
281281
y < x ? nextfloat(y) : y
282282
end
283283
end
284+
function _convert_rounding(::Type{T}, x::Real, r::RoundingMode{:FromZero}) where T<:AbstractFloat
285+
y = convert(T,x)::T
286+
if x < 0.0
287+
y > x ? prevfloat(y) : y
288+
else
289+
y < x ? nextfloat(y) : y
290+
end
291+
end
292+
284293

285294
# Default definitions
286295

‎test/rounding.jl

+13
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,16 @@ end
495495
end
496496
end
497497
end
498+
499+
@testset "Rounding to floating point types with RoundFromZero #55820" begin
500+
@testset "Testing float types: $f" for f (Float16, Float32, Float64, BigFloat)
501+
@testset "Testing value types: $t" for t (Bool, Rational{Int8})
502+
@test iszero(f(zero(t), RoundFromZero))
503+
end
504+
end
505+
@test Float16(100000, RoundToZero) === floatmax(Float16)
506+
@test Float16(100000, RoundFromZero) === Inf16
507+
@test Float16(-100000, RoundToZero) === -floatmax(Float16)
508+
@test Float16(-100000, RoundFromZero) === -Inf16
509+
@test Float32(nextfloat(0.0), RoundFromZero) === nextfloat(0.0f0)
510+
end

0 commit comments

Comments
 (0)
Please sign in to comment.