# Diagnosing NaNs from ForwardDiff

**URL:** <https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 15, 2018, 9:39pm UTC](https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364 "2018-10-15T21:39:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 15, 2018, 9:39pm UTC](https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364/1 "2018-10-15T21:39:20Z")

</div>

I am getting a lot of NaNs from ForwardDiff.gradient on a fairly complicated function. I have an analytical function to compute the gradient that works fine (doesn’t give NaN), and coincides with ForwardDiff’s result when it is not NaN.

I already enabled the NANSAFE\_MODE\_ENABLED setting, but it is not helping. Is there a way to find the point in my code which makes ForwardDiff return a NaN?

Thanks!

---

<div class="post-metadata">

**Author:** ![natschil](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@natschil](https://discourse.julialang.org/u/natschil)\
**Post date:** [October 15, 2018, 10:01pm UTC](https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364/2 "2018-10-15T22:01:39Z")

</div>

Do you have calls do divrem (or similar?). That’s the only place I’ve seen ForwardDiff produce NaNs so far (and only at isolated points where the remainder is zero)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 16, 2018, 7:36am UTC](https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364/3 "2018-10-16T07:36:47Z")

</div>

For any computed real, you can check for `NaN` partials, eg

```julia
noNaNs(x::Real) = true
noNaNs(x::ForwardDiff.Dual) = !any(isnan, ForwardDiff.partials(x))

```

then

```julia
@assert noNaNs(some_value)

```

in your code.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 16, 2018, 12:06pm UTC](https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364/4 "2018-10-16T12:06:45Z")

</div>

No, I’m not using `divrem`. But the function I’m trying to differentiate goes through finding a root of a nonlinear equation.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 16, 2018, 12:07pm UTC](https://discourse.julialang.org/t/diagnosing-nans-from-forwarddiff/16364/5 "2018-10-16T12:07:21Z")

</div>

Ah, this seems like a nice and dirty idea. I will springle `noNaNs` and see where it fails first! Thanks.
