# What does this TypeError("invalid NaN comparison") mean? And how do I fix it?

**URL:** <https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095>\
**Category:** New to Julia\
**Tags:** question, package, numerics, sympy, nonlinearsolve\
**Created:** [April 27, 2022, 12:21am UTC](https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095 "2022-04-27T00:21:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grace\_Chuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grace_chuan/32/35724_2.png) [@Grace\_Chuan](https://discourse.julialang.org/u/Grace_Chuan)\
**Post date:** [April 27, 2022, 12:21am UTC](https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095/1 "2022-04-27T00:21:47Z")

</div>

I’m trying to use the function “solve” from sympy to solve for the values that satisfies a complicated equation. I am receiving the following error message: TypeError(“invalid NaN comparison”)

Does anyone know how to resolve this issue or understand where it is coming from? Any advice would be much appreciated!

Thanks

 ![image001](https://global.discourse-cdn.com/julialang/original/3X/1/e/1ee0b1185f9d40e485075760dd4f4d449f7aa00a.jpeg)

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 27, 2022, 12:56am UTC](https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095/2 "2022-04-27T00:56:34Z")

</div>

> [@Grace\_Chuan](#):
>
> I’m trying to use the function “solve” from sympy to solve for the values that satisfies a complicated equation. I am receiving the following error message: TypeError(“invalid NaN comparison”)
> 
> Does anyone know how to resolve this issue or understand where it is coming from? Any advice would be much appreciated!

hi, can you post what are `term2` and `x_hat`?

---

<div class="post-metadata">

**Author:** ![Grace\_Chuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grace_chuan/32/35724_2.png) [@Grace\_Chuan](https://discourse.julialang.org/u/Grace_Chuan)\
**Post date:** [April 27, 2022, 1:14am UTC](https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095/3 "2022-04-27T01:14:08Z")

</div>

x\_hat = [symbols(“x\_hat$i”,real=true) for i=1:n].reshape(n,1)  
term2 = find\_max(find\_min(Q\*x\_hat,d),zeros(n,1))  
n is an integer, Q is a nxn matrix of floats, d is also an nx1 vector

1. term2 is in vague terms. In the real code it’s actually more complicated ☹ But, hopefully this gets the main point across
2. find\_max and find\_min are functions I made myself - the continuous form of minimum and maximum functions. (the reason why I wrote my own vs. the built-in max and min functions is because I originally wrote this in matlab and in matlab, it couldn’t take the minimum or maximum of symbolic terms)
3. also in matlab, I used the function vpasolve() and it worked there. I am translating my matlab code into Julia to speed the process up

# reference for find\_min and find\_max functions:

function find\_min(A,B)  
val = A - (abs.(A-B)+A-B)./2;  
return val  
end

function find\_max(A,B)  
val = B + (abs.(A-B)+A-B)./2;  
return val  
end

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 27, 2022, 1:28am UTC](https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095/4 "2022-04-27T01:28:50Z")

</div>

hmm, got the same error. this is the full code to reproduce that error

```julia
import Sympy
n = 10

Q = rand(n,n)
d = rand(n,1)

function find_min(A,B)
  return A - (abs.(A-B)+A-B)./2;
end

function find_max(A,B)
  return B + (abs.(A-B)+A-B)./2;
end

x_hat = [Sympy.symbols("x_hat$i",real=true) for i=1:n].reshape(n,1)}
term2 = find_max(find_min(Q*x_hat,d),zeros(n,1))
Sympy.solve(term2-x_hat,x_hat)

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 27, 2022, 1:36am UTC](https://discourse.julialang.org/t/what-does-this-typeerror-invalid-nan-comparison-mean-and-how-do-i-fix-it/80095/5 "2022-04-27T01:36:45Z")

</div>

@Grace_Chuan i think that the error lies in the `find_min` and `find_max` errors. you are creating an expression with Abs(sym) on term2-x\_hat. changing those functions to:

```julia

function find_min(A,B)
    res = zeros(eltype(A),size(A)) #create an Array of the same size and element type
    for i in eachindex(A)
        if A[i] < B[i] #compare an store the correct branch instead of the abs function
            res[i] = A[i]
        else
            res[i] = B[i]
        end
    end
    return res
  end
  
  function find_max(A,B)
    res = zeros(eltype(A),size(A))
    for i in eachindex(A)
        if A[i] > B[i]
            res[i] = A[i]
        else
            res[i] = B[i]
        end
    end
    return res
 end

```

results in:

```julia
julia> Sympy.solve(term2-x_hat,x_hat)
Dict{Any, Any} with 10 entries:
  x_hat6 => 0.926800682408201 
  x_hat10 => 0.962863463691171 
  x_hat7 => 0.497643557077236 
  x_hat3 => 0.211146695704730 
  x_hat2 => 0.568369033677095 
  x_hat5 => 0.187652949522494 
  x_hat4 => 0.392861232103788 
  x_hat9 => 0.559299830282101 
  x_hat1 => 0.431629987943204 
  x_hat8 => 0.320110979997330

```

of course, there is the corner case of `A[i] == B[i]` but that is outside the scope of this problem, and more of the specific domain that are you working on
