Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define precision for Dual #580

Merged
merged 4 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
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