8
def ellipse(numPoints, genX=np.linspace, HALF_WIDTH=10, HALF_HEIGHT=6.5):
    xs = 10.*genX(-1,1,numPoints)
    ys = 6.5*np.sqrt(1-(xs**2))
    return(xs, ys, "-")

I am getting an error that states that an invalid value was encountered in a squareroot. I can't see what it is.

sqrt(0) = 0
6.5*sqrt(1- (-1**2)) = 0

They should work, but the y values are having problems, they are returning "nan"

Bob Unger
  • 341
  • 2
  • 4
  • 11
  • Does this answer your question? [I am getting a warning ](https://stackoverflow.com/questions/39123766/i-am-getting-a-warning-runtimewarning-invalid-value-encountered-in-sqrt) – iacob Mar 28 '21 at 12:12

1 Answers1

7

probably xs**2 returns a number > 1 sqrt with negative number will return nan (not a number)

>>> import numpy as np
>>> np.sqrt(-1)
nan

If i am right numpy provides complex numbers functionality which i think is the only way to represent sqrt(x) where x<0

Foo Bar User
  • 2,401
  • 3
  • 20
  • 26
  • There shouldn't be any values that are the squareroot of a negative number when i'm going from -1 to 1. nvm. edit* I see it now – Bob Unger Apr 09 '14 at 00:01
  • 1
    I should have 6.5*sqrt(100-(xs**2)) – Bob Unger Apr 09 '14 at 00:04
  • 2
    also if you want numpy to give an error on such cases you can use `np.seterr(all='raise')` and would give `FloatingPointError: invalid value encountered in sqrt` instead of `nan`. more info here http://docs.scipy.org/doc/numpy/user/misc.html#how-numpy-handles-numerical-exceptions – Foo Bar User Apr 09 '14 at 00:13