# Bug in Forward diff for complex valued function

**URL:** <https://discourse.julialang.org/t/bug-in-forward-diff-for-complex-valued-function/100119>\
**Category:** General Usage\
**Tags:** forwarddiff, autodiff, complex-numbers\
**Created:** [June 9, 2023, 4:34pm UTC](https://discourse.julialang.org/t/bug-in-forward-diff-for-complex-valued-function/100119 "2023-06-09T16:34:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veenty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/veenty/32/50940_2.png) [@Veenty](https://discourse.julialang.org/u/Veenty)\
**Post date:** [June 9, 2023, 4:34pm UTC](https://discourse.julialang.org/t/bug-in-forward-diff-for-complex-valued-function/100119/1 "2023-06-09T16:34:06Z")

</div>

I have an extremely simple case scenario

```julia
r(s) = (1 + 0.3*cos(8π*s + 2*sin(2π*s)))*exp(1im*(2π*s - π/2))
r′(s) = ForwardDiff.derivative(r, s)
r′′(s) = ForwardDiff.derivative(r′, s)
h_ref = 1/(5*2^9)
T_ref = collect(0:h_ref:1 - h_ref)
lines(T_ref, real.(r′′.(T_ref)))

```

It happens that the plot has a weird singularity for a certain value, which I know is wrong (the function is continuous). So not sure what is going on here?

 ![Screenshot 2023-06-09 at 12.32.10 PM](https://global.discourse-cdn.com/julialang/original/3X/b/5/b5acde898e3eb6a052ca3e7f168a51a2b3b55774.png)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 9, 2023, 4:37pm UTC](https://discourse.julialang.org/t/bug-in-forward-diff-for-complex-valued-function/100119/2 "2023-06-09T16:37:53Z")

</div>

this is a numerical precision issue. specifically, I think the problem is cancellation in `exp(1im*(2π*s - π/2))`. does the issue go away if you write this as `cispi(2s-1/2)`?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 9, 2023, 5:02pm UTC](https://discourse.julialang.org/t/bug-in-forward-diff-for-complex-valued-function/100119/3 "2023-06-09T17:02:58Z")

</div>

It looks like the problem arises right at `s=0.25`:

```julia
julia> r′′(0.2499999999999)
65.84713137037353 - 86.15429650286814im

julia> r′′(0.25)
100.39690339578884 - 86.15429650270057im

```

> [@Oscar\_Smith](#):
>
> this is a numerical precision issue. specifically, I think the problem is cancellation in `exp(1im*(2π*s - π/2))`.

Indeed this seems to avoid the problem at `s=0.25`:

```julia
julia> r(s) = (1 + 0.3*cos(8π*s + 2*sin(2π*s)))*cispi(2s - 1/2)
r (generic function with 1 method)

julia> r′′(0.2499999999999)
65.84713137037355 - 86.15429650286814im

julia> r′′(0.25)
65.84713137081351 - 86.15429650270057im

```

But I’m still a little suspicious — it’s not obvious to me why there should be a huge cancellation error in this result right at `s=0.25`.

In particular, consider:

```julia
julia> p(s) = exp(1im*(2π*s - π/2));

julia> p′(s) = ForwardDiff.derivative(p, s);

julia> p″(s) = ForwardDiff.derivative(p′, s);

julia> p″(0.2499999999999)
-39.47841760435743 + 2.4807694081270598e-11im

julia> p″(0.25) # correct answer is -(2π)² ≈ -39.47841760435743
0.0 + 0.0im

```

This looks like a bug to me, not cancellation error. I filed it as [ForwardDiff.jl#653](https://github.com/JuliaDiff/ForwardDiff.jl/issues/653).
