# NaN Detector

**URL:** <https://discourse.julialang.org/t/nan-detector/55024>\
**Category:** General Usage\
**Tags:** debugging, nan\
**Created:** [February 10, 2021, 8:25pm UTC](https://discourse.julialang.org/t/nan-detector/55024 "2021-02-10T20:25:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [February 10, 2021, 8:25pm UTC](https://discourse.julialang.org/t/nan-detector/55024/1 "2021-02-10T20:25:18Z")

</div>

I’m trying to find my NaN generator. I think it is due to an uninitialized input variable without proper protection in the code. I know the debugger doesn’t have a nan break and it is very difficult for me to get to the spot.

My thought was, can I somehow override the base./ function call to do a 0.0/0.0 check and alert me with a stack trace of the calling function or just throw an error and die right there with a stack trace.

I thought I could just override Base./ at the top level, but do I need to do this inside every package that uses math? How best to do this?

Best Regards,  
Allan Baker

---

<div class="post-metadata">

**Author:** ![moeddel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moeddel/32/18641_2.png) [@moeddel](https://discourse.julialang.org/u/moeddel)\
**Post date:** [February 10, 2021, 8:39pm UTC](https://discourse.julialang.org/t/nan-detector/55024/2 "2021-02-10T20:39:55Z")

</div>

Maybe you can use

[https://github.com/JeffreySarnoff/SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl)

to spot your problem. It Helped me to catch silent overflows.

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [February 11, 2021, 1:38am UTC](https://discourse.julialang.org/t/nan-detector/55024/3 "2021-02-11T01:38:29Z")

</div>

Thanks for the tip. I need it for floating point calculations though.

---

<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:** [February 11, 2021, 2:27am UTC](https://discourse.julialang.org/t/nan-detector/55024/4 "2021-02-11T02:27:47Z")

</div>

See this issue for discussion and some options: [https://github.com/JuliaLang/julia/issues/27705](https://github.com/JuliaLang/julia/issues/27705)

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 11, 2021, 4:15am UTC](https://discourse.julialang.org/t/nan-detector/55024/5 "2021-02-11T04:15:36Z")

</div>

You are welcome to [NaNcatcher.jl](https://gist.github.com/JeffreySarnoff/db217673bc220360dc388136ab170e76), an experiment of mine. There is some description within the file. If it seems helpful and you have questions … I am around.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 11, 2021, 3:05pm UTC](https://discourse.julialang.org/t/nan-detector/55024/6 "2021-02-11T15:05:45Z")

</div>

> [@Allan\_Baker](#):
>
> I know the debugger doesn’t have a nan break and it is very difficult for me to get to the spot.

If it’s NaNs in an AbstractArray, can you put a conditional breakpoint in the `setindex!` method for your specific array that’s receiving the NaNs?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 11, 2021, 3:23pm UTC](https://discourse.julialang.org/t/nan-detector/55024/7 "2021-02-11T15:23:05Z")

</div>

I have a half-completed attempt at using a Cassette pass to find the first `NaN` that gets returned from _any_ function. Needs some more work, but it found the NaN I was hunting for and then I moved on:

[https://github.com/mbauman/TheNaNTrap.jl/](https://github.com/mbauman/TheNaNTrap.jl/)

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [February 23, 2021, 3:24am UTC](https://discourse.julialang.org/t/nan-detector/55024/8 "2021-02-23T03:24:05Z")

</div>

I ended up brute forcing it and searching for all of the divides for 0/0 protection and using @info statements to bark about NaNs and working my way through the call tree to find where things went wrong. Running in the interactive debugger is too cumbersome because I can’t set breakpoints multi-levels down in “accelerated/compiled” code. I think it was uninitialized parameters from my namelist reader. The defaults I used in some missed C++ code conversions were set to 0.0 instead of 1.0 for things like “factor”. I think if I could figure out how to overload the divide to check for 0/0 I could dump a stack when it happens.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 23, 2021, 7:23am UTC](https://discourse.julialang.org/t/nan-detector/55024/9 "2021-02-23T07:23:47Z")

</div>

This may help, if nothing is precompiled or handed to C and you work in the REPL, setting this first then including other functions. I have not tried this strategy – so here’s hoping.

```julia
julia> (/)(x::Float64, y::Float64) =
    iszero(y) && iszero(x) ? "found it" : Base.:(/)(x,y)

julia> 1.0 / 0.0, 0.0 / 1.0
(Inf, 0.0)

julia> 0.0 / 0.0
"found it"

```

---

<div class="post-metadata">

**Author:** ![Allan\_Baker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/allan_baker/32/42645_2.png) [@Allan\_Baker](https://discourse.julialang.org/u/Allan_Baker)\
**Post date:** [February 24, 2021, 2:09pm UTC](https://discourse.julialang.org/t/nan-detector/55024/10 "2021-02-24T14:09:09Z")

</div>

I was thinking this might work, but if I’m pulling in packages, do I need to go into each package and define this?

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [February 25, 2021, 2:23am UTC](https://discourse.julialang.org/t/nan-detector/55024/11 "2021-02-25T02:23:39Z")

</div>

The easiest way to answer these kinds of questions is empiricism. Don’t hesitate to just try this out.

One session:

```julia
julia> Base.:(/)(x::Float64, y::Float64) = 1.0

julia> using Distributions

julia> pdf(Normal(0.0, 1.0), 5.0)
1.0

julia> pdf(Normal(0.0, 1.0), 10.0)
1.0

```

Another session:

```julia
julia> using Distributions

julia> pdf(Normal(0.0, 1.0), 5.0)
1.4867195147342977e-6

julia> pdf(Normal(0.0, 1.0), 10.0)
7.69459862670642e-23

```
