# Evaluation of function gives NaN

**URL:** <https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240>\
**Category:** General Usage\
**Created:** [March 6, 2024, 1:28pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240 "2024-03-06T13:28:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 6, 2024, 1:28pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/1 "2024-03-06T13:28:38Z")

</div>

Hello,  
The division of two functions gives `NaN` when it is not simplified.  
Here is an example code

```julia
using ForwardDiff
f(x) = (x-1)^2
df(x) = ForwardDiff.derivative(f,x)
val = f(1)/df(1)

```

Mathematically this should be 0 but gives NaN.  
How do I address such a problem?  
Side note: I get the expected value on all other `x`.

Thank you

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [March 6, 2024, 1:40pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/2 "2024-03-06T13:40:25Z")

</div>

If I am not mistaken, you have \text{val} = \frac{(x-1)^2}{2(x-1)} for x=1. That simplifies to \frac{0}{0}, which is often considered undefined.

EDIT: Technically, you could evaluate the expression in the limit as x \to 0. Then \lim\_{x \to 0} \frac{(x-1)^2}{2(x-1)} = \lim\_{x \to 0} \frac{2(x - 1)}{2} = 0 (L’Hôpital’s rule). But this is not done automatically.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 6, 2024, 1:43pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/3 "2024-03-06T13:43:58Z")

</div>

Indeed, running `0.0 / 0.0` in the REPL will return NaN, as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754). This is what happens here since `f(1) == 0` and `df(1) == 0` and the division promotes the values to Float64.

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 6, 2024, 1:44pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/4 "2024-03-06T13:44:58Z")

</div>

Yes, but that is when it is not simplified.  
It should be \frac{x-1}{2} on simplification.

Hence my question: how do I address such problems in Julia?

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 6, 2024, 1:46pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/5 "2024-03-06T13:46:18Z")

</div>

Yes, thank you. I am aware of this. But how should one proceed in such a case?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 6, 2024, 1:46pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/6 "2024-03-06T13:46:31Z")

</div>

> [@Sushrut\_Deshpande](#):
>
> Yes, but that is when it is not simplified.  
> It should be \frac{x-1}{2}x−12\frac{x-1}{2} on simplification.

Julia is not a CAS; expressions are not automatically simplified. You may be interested in Symbolics.jl instead.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [March 6, 2024, 1:59pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/7 "2024-03-06T13:59:52Z")

</div>

> [@Sushrut\_Deshpande](#):
>
> Mathematically this should be 0 but gives NaN.

> [@Sushrut\_Deshpande](#):
>
> It should be \frac{x-1}{2}x−12\frac{x-1}{2} on simplification.

Not so, the function is undefined at x=1. The (two-sided) limit exists there, making it a removable singularity.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [March 6, 2024, 2:05pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/8 "2024-03-06T14:05:18Z")

</div>

> [@Sushrut\_Deshpande](#):
>
> But how should one proceed in such a case?

Define a small function:

```julia
julia> f_over_df(x) = isone(x) ? zero(x) : f(x) / df(x)
f_over_df (generic function with 1 method)

```

---

<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:** [March 6, 2024, 2:06pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/9 "2024-03-06T14:06:22Z")

</div>

> [@Sushrut\_Deshpande](#):
>
> How do I address such a problem?

You can take the limit analytically yourself, do the algebraic simplification yourself (or via a computer-algebra system like Symbolics.jl), or take a limit numerically, e.g. with Richardson.jl:

```julia
julia> using Richardson

julia> extrapolate(x -> f(x) / df(x), 0.5, x0=1)
(0.0, 0.0)

```

(an estimated limit and an estimated error bar). So it correctly finds that the limit is zero. (Depending on the starting `x`, there may be some roundoff error, e.g. if you replace `0.5` with `0.1` it finds a limit of about `-4.77e-17` due to roundoff errors).

Obviously, your particular example in this thread is so simple that you should just use the analytical form manually, but I’m presuming that you are also interested in other cases that can’t be handled analytically so easily.

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 6, 2024, 2:20pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/10 "2024-03-06T14:20:57Z")

</div>

Thank you. I like this

---

<div class="post-metadata">

**Author:** ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)\
**Post date:** [March 6, 2024, 2:21pm UTC](https://discourse.julialang.org/t/evaluation-of-function-gives-nan/111240/11 "2024-03-06T14:21:53Z")

</div>

Thanks
