Skip to content
Advertisement

Unexpected result in the numerical calculation – (invalid value encountered in double_scalar )

JavaScript

The result is

JavaScript

I am having troubling to understand the problem here. It’s clear that I can print the x, but I cannot take the power of it.

Meanwhile when I write the code as

JavaScript

I get

JavaScript

I am literally shocked. I cannot understand what is going on. If I just put the numbers manually I can calculate the result. But if I do them computationally I get an error ???

Advertisement

Answer

Well they look the same by they are not the same, with the code:

JavaScript

you have a variable ‘x’ of type <class 'numpy.float64'>, whereas with:

JavaScript

the variable ‘x’ is of type <class 'float'>.

Then you applied the exponentiation with a value of different types, namely <type one of type ‘numpy.float64’> (i.e., ‘x’) with another of type <class ‘float’> (i.e., ‘y’). You want to avoid the problems is to convert ‘x’ to float was well:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement