Use round(::Dual, ::RoundingMode) for rounding functions on 1.11+ - #829
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #829 +/- ##
==========================================
- Coverage 90.71% 90.68% -0.03%
==========================================
Files 11 11
Lines 1055 1052 -3
==========================================
- Hits 957 954 -3
Misses 98 98 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks, this is a nice cleanup — getting rid of these invalidations is definitely worth it. I think the version bound can be narrower than 1.11 though. Base has defined trunc(x::Real; kwargs...) = round(x, RoundToZero; kwargs...)
floor(x::Real; kwargs...) = round(x, RoundDown; kwargs...)
ceil(x::Real; kwargs...) = round(x, RoundUp; kwargs...)for much longer than that (
I checked on 1.10.6 with a dummy # Base derives floor/ceil/trunc/round from `round(x, ::RoundingMode)`:
# https://docs.julialang.org/en/v1/manual/interfaces/#man-rounding-interface
Base.round(d::Dual, r::RoundingMode) = round(value(d), r)
# Julia 1.11 added the generic `f(::Type{T}, x)` fallbacks, so these can be
# dropped once 1.11 is the minimum supported version.
if VERSION < v"1.11"
Base.floor(::Type{R}, d::Dual) where {R<:Real} = floor(R, value(d))
Base.ceil(::Type{R}, d::Dual) where {R<:Real} = ceil(R, value(d))
Base.trunc(::Type{R}, d::Dual) where {R<:Real} = trunc(R, value(d))
Base.round(::Type{R}, d::Dual) where {R<:Real} = round(R, value(d))
endTwo advantages besides the smaller diff: the invalidations are fixed on the LTS as well (as written, 1.10 users keep all eight methods), and Also, AFAICT Could you add a few tests as well? The existing ones only cover
Something like for r in (RoundDown, RoundUp, RoundToZero, RoundNearest,
RoundNearestTiesAway, RoundNearestTiesUp, RoundFromZero)
@test round(FDNUM, r) === round(PRIMAL, r)
@test round(NESTED_FDNUM, r) === round(PRIMAL, r)
end
@test floor(Float32, FDNUM) === floor(Float32, PRIMAL)
@test ceil(Float32, FDNUM) === ceil(Float32, PRIMAL)(The failures on |
|
Sure, changed the implementation and added those tests in cdafa99. |
The other functions like floor/trunc are already implemented in terms of that function in 1.11.
The other functions like floor/trunc are already implemented in terms of that function in 1.11: https://docs.julialang.org/en/v1/manual/interfaces/#man-rounding-interface
Fixes these invalidations seen when loading CurveFit.jl on 1.13: