msg325139 - (view) |
Author: Floris van Manen (klankschap) |
Date: 2018-09-12 14:27 |
when using numpy defined values i get nan results. when using math defined values, no nan errors occur. check2 bb [5.0, 2.285379077161093, nan, nan, 2.285379077161092] check2 cc [5.0, 2.285379077161093, 4.341186402706317, 4.341186402706317, 2.285379077161092] how to get identical results? |
|
|
msg325142 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2018-09-12 16:10 |
Your code gives runtime warnings of invalid values. You should fix that. If your values are invalid, there's probably a bug in your code. RuntimeWarning: invalid value encountered in double_scalars Your code is also very complex. You ought to simplify the example so that it is easier to understand, all the business with linspace and duplicated code just adds complexity and makes it hard to understand. I simplified your code to this: import numpy as np n=2.758 n2 = 2.0 / n ct = np.cos(2 * np.pi * 2.0 / 5) print("numpy", ct, abs(ct ** n2) * 5.0) which gives this output: __main__:1: RuntimeWarning: invalid value encountered in double_scalars ('numpy', -0.80901699437494734, nan) So there's a problem. You're trying to raise a negative number to a positive value, and numpy doesn't like it and returns a NAN. But using the standard math library, raising a negative number to a positive value gives you a complex number: ct = math.cos(2 * math.pi * 2.0 / 5) print(ct**n2) print("math", ct, abs(ct ** n2) * 5.0) which gives this output: (-0.5572617094280153+0.6517928032447587j) math -0.8090169943749473 4.287698890886272 So the behaviour is correct and this is not a bug in either math nor numpy. They're just doing different things. |
|
|
msg325143 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2018-09-12 16:12 |
Oops, sorry, I mistyped. I said: So there's a problem. You're trying to raise a negative number to a positive value I meant to say a *fractional* value. Sorry for the confusion. |
|
|
msg325144 - (view) |
Author: Floris van Manen (klankschap) |
Date: 2018-09-12 16:21 |
Well, the thing is that i pass two (apparent) identical values into the same function, and get two different results. Apparent as in one value generated via np.linspace() and one directly retrieved from a list. > On 12 Sep 2018, at 18:12, Steven D'Aprano <report@bugs.python.org> wrote: > > > Steven D'Aprano <steve+python@pearwood.info> added the comment: > > Oops, sorry, I mistyped. I said: > > So there's a problem. You're trying to raise a negative number > to a positive value > > > I meant to say a *fractional* value. Sorry for the confusion. > > ---------- > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue34645> > _______________________________________ |
|
|
msg325145 - (view) |
Author: Floris van Manen (klankschap) |
Date: 2018-09-12 16:27 |
Well, the thing is that i pass two (apparent) identical values into the same function, and get two different results. Apparent as in one value generated via np.linspace() and one directly retrieved from a list. If i pass the np variable into to function, it will generate an NAN error. If i pass the non np variable into the function, it will work. .F > On 12 Sep 2018, at 18:10, Steven D'Aprano <report@bugs.python.org> wrote: > > > Steven D'Aprano <steve+python@pearwood.info> added the comment: > > Your code gives runtime warnings of invalid values. You should fix that. If your values are invalid, there's probably a bug in your code. > > RuntimeWarning: invalid value encountered in double_scalars > > Your code is also very complex. You ought to simplify the example so that it is easier to understand, all the business with linspace and duplicated code just adds complexity and makes it hard to understand. > > I simplified your code to this: > > import numpy as np > n=2.758 > n2 = 2.0 / n > ct = np.cos(2 * np.pi * 2.0 / 5) > print("numpy", ct, abs(ct ** n2) * 5.0) > > > which gives this output: > > __main__:1: RuntimeWarning: invalid value encountered in double_scalars > ('numpy', -0.80901699437494734, nan) > > So there's a problem. You're trying to raise a negative number to a positive value, and numpy doesn't like it and returns a NAN. > > But using the standard math library, raising a negative number to a positive value gives you a complex number: > > ct = math.cos(2 * math.pi * 2.0 / 5) > print(ct**n2) > print("math", ct, abs(ct ** n2) * 5.0) > > > which gives this output: > > (-0.5572617094280153+0.6517928032447587j) > math -0.8090169943749473 4.287698890886272 > > So the behaviour is correct and this is not a bug in either math nor numpy. They're just doing different things. > > ---------- > nosy: +steven.daprano > resolution: -> not a bug > stage: -> resolved > status: open -> closed > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue34645> > _______________________________________ |
|
|
msg325152 - (view) |
Author: Steven D'Aprano (steven.daprano) *  |
Date: 2018-09-12 18:19 |
> Well, the thing is that i pass two (apparent) identical values into the same function, Even if they have the same *numeric* value, they aren't the same kind of value, and they aren't the same function. One is <type 'numpy.float64'> and the other is <class 'float'>. When you call ** (exponentiation), that calls two different methods. One raises a warning and returns NAN, the other converts to complex. This has nothing to do with linspace. See my simplifed example code which doesn't use it. |
|
|
msg325168 - (view) |
Author: Floris van Manen (klankschap) |
Date: 2018-09-12 18:58 |
I know it has nothing todo with linspace. But there seems to be a link to using numpy generated variables and not using them. From a naive point of view i’d expect the same results. But it does not. There is two functions, and two variables. And from the four combinations, only one gives a non NAN error. > On 12 Sep 2018, at 20:19, Steven D'Aprano <report@bugs.python.org> wrote: > > > Steven D'Aprano <steve+python@pearwood.info> added the comment: > >> Well, the thing is that i pass two (apparent) identical values into the same function, > > Even if they have the same *numeric* value, they aren't the same kind of > value, and they aren't the same function. > > One is <type 'numpy.float64'> and the other is <class 'float'>. When you > call ** (exponentiation), that calls two different methods. One raises > a warning and returns NAN, the other converts to complex. > > This has nothing to do with linspace. See my simplifed example code > which doesn't use it. > > ---------- > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue34645> > _______________________________________ |
|
|