Skip to content

Fallback math methods for ::Real are likely to call themselves #26552

@tkoolen

Description

@tkoolen

On 0.6.2:

julia> using ForwardDiff

julia> ForwardDiff.derivative(sinpi, 1.)
0.0

on b571283:

julia> using ForwardDiff

julia> ForwardDiff.derivative(sinpi, 1.)
ERROR: StackOverflowError:
Stacktrace:
 [1] sinpi(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(sinpi),Float64},Float64,1}) at ./special/trig.jl:865 (repeats 80000 times)

This is because of

sinpi(x::Real) = sinpi(float(x))

which seems unfortunate. Similarly, log1p required a workaround in DiffRules. I wholeheartedly agree with JuliaDiff/ForwardDiff.jl#261 (comment):

Looks like Base.Math.JuliaLibm.log1p(x::Real) just calls Base.Math.JuliaLibm.log1p(float(x)), which basically guarantees infinite recursion in the general case

log and cospi also have this issue. Maybe others as well?

Metadata

Metadata

Assignees

No one assigned

    Labels

    mathsMathematical functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions