Skip to content

Commit

Permalink
Define precision for Dual (#580)
Browse files Browse the repository at this point in the history
* Define `precision` for `Dual`

* Fix support of `base` keyword argument

* Fix typo

* Yet another typo...
  • Loading branch information
devmotion authored Apr 24, 2022
1 parent c85db12 commit e11936c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ Base.copy(d::Dual) = d
Base.eps(d::Dual) = eps(value(d))
Base.eps(::Type{D}) where {D<:Dual} = eps(valtype(D))

# The `base` keyword was added in Julia 1.8:
# https://github.com/JuliaLang/julia/pull/42428
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<:Dual}
precision(valtype(D); base=base)
end
end

function Base.nextfloat(d::ForwardDiff.Dual{T,V,N}) where {T,V,N}
ForwardDiff.Dual{T}(nextfloat(d.value), d.partials)
end
Expand Down
11 changes: 11 additions & 0 deletions test/DualTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

2 comments on commit e11936c

@mcabbott
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/59013

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.26 -m "<description of version>" e11936c5c8572f99be2fdc3ff99db66ab79b807c
git push origin v0.10.26

Please sign in to comment.