Skip to content

Commit e11936c

Browse files
authored
Define precision for Dual (#580)
* Define `precision` for `Dual` * Fix support of `base` keyword argument * Fix typo * Yet another typo...
1 parent c85db12 commit e11936c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/dual.jl

+12
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ Base.copy(d::Dual) = d
262262
Base.eps(d::Dual) = eps(value(d))
263263
Base.eps(::Type{D}) where {D<:Dual} = eps(valtype(D))
264264

265+
# The `base` keyword was added in Julia 1.8:
266+
# https://github.com/JuliaLang/julia/pull/42428
267+
if VERSION < v"1.8.0-DEV.725"
268+
Base.precision(d::Dual) = precision(value(d))
269+
Base.precision(::Type{D}) where {D<:Dual} = precision(valtype(D))
270+
else
271+
Base.precision(d::Dual; base::Integer=2) = precision(value(d); base=base)
272+
function Base.precision(::Type{D}; base::Integer=2) where {D<:Dual}
273+
precision(valtype(D); base=base)
274+
end
275+
end
276+
265277
function Base.nextfloat(d::ForwardDiff.Dual{T,V,N}) where {T,V,N}
266278
ForwardDiff.Dual{T}(nextfloat(d.value), d.partials)
267279
end

test/DualTest.jl

+11
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
105105
@test eps(NESTED_FDNUM) === eps(PRIMAL)
106106
@test eps(typeof(NESTED_FDNUM)) === eps(V)
107107

108+
@test precision(FDNUM) === precision(PRIMAL)
109+
@test precision(typeof(FDNUM)) === precision(V)
110+
@test precision(NESTED_FDNUM) === precision(PRIMAL)
111+
@test precision(typeof(NESTED_FDNUM)) === precision(V)
112+
if VERSION >= v"1.8.0-DEV.725" # https://github.com/JuliaLang/julia/pull/42428
113+
@test precision(FDNUM; base=10) === precision(PRIMAL; base=10)
114+
@test precision(typeof(FDNUM); base=10) === precision(V; base=10)
115+
@test precision(NESTED_FDNUM; base=10) === precision(PRIMAL; base=10)
116+
@test precision(typeof(NESTED_FDNUM); base=10) === precision(V; base=10)
117+
end
118+
108119
@test floor(Int, FDNUM) === floor(Int, PRIMAL)
109120
@test floor(Int, FDNUM2) === floor(Int, PRIMAL2)
110121
@test floor(Int, NESTED_FDNUM) === floor(Int, PRIMAL)

0 commit comments

Comments
 (0)