From d07417fe32358803f76f11f796034b92359e0719 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 8 Mar 2022 23:15:41 +0100 Subject: [PATCH 1/4] Define `precision` for `Dual` --- Project.toml | 2 +- src/dual.jl | 9 +++++++++ test/DualTest.jl | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 25c3ed0d..61e31efa 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ForwardDiff" uuid = "f6369f11-7733-5829-9624-2563aa707210" -version = "0.10.25" +version = "0.10.26" [deps] CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950" diff --git a/src/dual.jl b/src/dual.jl index 31a7ce99..d20d3ced 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -262,6 +262,15 @@ Base.copy(d::Dual) = d Base.eps(d::Dual) = eps(value(d)) Base.eps(::Type{D}) where {D<:Dual} = eps(valtype(D)) +# We overload Base._precision to support the base keyword in Julia 1.8 +# https://github.com/JuliaLang/julia/pull/42428 +let precision = VERSION >= v"1.8.0-DEV.725" ? :_precision : :precision + @eval begin + Base.$precision(d::Dual) = Base.$precision(value(d)) + Base.$precision(::Type{D}) where {D<:Dual} = Base.$precision(valtype(D)) + end +end + function Base.nextfloat(d::ForwardDiff.Dual{T,V,N}) where {T,V,N} ForwardDiff.Dual{T}(nextfloat(d.value), d.partials) end diff --git a/test/DualTest.jl b/test/DualTest.jl index 5a4a8350..06e574ac 100644 --- a/test/DualTest.jl +++ b/test/DualTest.jl @@ -105,6 +105,17 @@ for N in (0,3), M in (0,4), V in (Int, Float32) @test eps(NESTED_FDNUM) === eps(PRIMAL) @test eps(typeof(NESTED_FDNUM)) === eps(V) + @test precision(FDNUM) === precision(PRIMAL) + @test precision(typeof(FDNUM)) === precision(V) + @test precision(NESTED_FDNUM) === precision(PRIMAL) + @test precision(typeof(NESTED_FDNUM)) === precision(V) + if VERSION >= v"1.8.0-DEV.725" # https://github.com/JuliaLang/julia/pull/42428 + @test precision(FDNUM; base=10) === precision(PRIMAL; base=10) + @test precision(typeof(FDNUM); base=10) === precision(V; base=10) + @test precision(NESTED_FDNUM; base=10) === precision(PRIMAL; base=10) + @test precision(typeof(NESTED_FDNUM); base=10) === precision(V; base=10) + end + @test floor(Int, FDNUM) === floor(Int, PRIMAL) @test floor(Int, FDNUM2) === floor(Int, PRIMAL2) @test floor(Int, NESTED_FDNUM) === floor(Int, PRIMAL) From 930694e90d26463a44b9b8bc60709b114f2db1eb Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 8 Mar 2022 23:30:03 +0100 Subject: [PATCH 2/4] Fix support of `base` keyword argument --- src/dual.jl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/dual.jl b/src/dual.jl index d20d3ced..c92a022b 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -262,12 +262,15 @@ Base.copy(d::Dual) = d Base.eps(d::Dual) = eps(value(d)) Base.eps(::Type{D}) where {D<:Dual} = eps(valtype(D)) -# We overload Base._precision to support the base keyword in Julia 1.8 +# The `base` keyword was added in Julia 1.8: # https://github.com/JuliaLang/julia/pull/42428 -let precision = VERSION >= v"1.8.0-DEV.725" ? :_precision : :precision - @eval begin - Base.$precision(d::Dual) = Base.$precision(value(d)) - Base.$precision(::Type{D}) where {D<:Dual} = Base.$precision(valtype(D)) +if VERSION < v"1.8.0-DEV.725" + Base.precision(d::Dual) = precision(value(d)) + Base.precision(::Type{D}) where {D<:Dual} = precision(valtype(D)) +else + Base.precision(d::Dual; base::Integer=2) = precision(value(d); base=base) + function Base.precision(::Type{D}; base::Integer=2) where {D Date: Tue, 8 Mar 2022 23:33:47 +0100 Subject: [PATCH 3/4] Fix typo --- src/dual.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dual.jl b/src/dual.jl index c92a022b..e0f6e7e9 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -269,7 +269,7 @@ if VERSION < v"1.8.0-DEV.725" Base.precision(::Type{D}) where {D<:Dual} = precision(valtype(D)) else Base.precision(d::Dual; base::Integer=2) = precision(value(d); base=base) - function Base.precision(::Type{D}; base::Integer=2) where {D Date: Tue, 8 Mar 2022 23:39:44 +0100 Subject: [PATCH 4/4] Yet another typo... --- src/dual.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dual.jl b/src/dual.jl index e0f6e7e9..286b2e86 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -270,7 +270,7 @@ if VERSION < v"1.8.0-DEV.725" else Base.precision(d::Dual; base::Integer=2) = precision(value(d); base=base) function Base.precision(::Type{D}; base::Integer=2) where {D<:Dual} - precision(valtype(d); base=base) + precision(valtype(D); base=base) end end