How can I calculate the degree of fractions or the nth square of the root in numpy?

I am trying to calculate the 8th root square of a value or its ^ 1/8, but numpy always returns the wrong value

temp = 141.18 h2 = temp ** (1/8) h2_ = np.power(temp, (1/8)) 

my output is always 1.0. I also tried a square command. I need to use numpy, I use other numpy arrays in mycode to maintain compatibility.

+5
source share
2 answers
 >>> 1/8 0 >>> 1./8 0.125 

And, of course, everything that has a value of 0 leads to 1.

+4
source

Understand the number tower.

Rule 1 For two operands of the same type, the result will be of this type.

eg. int / int = int

temp**(1/8) does not give the 8th root of temp, because:

 >>>1/8 0 

Rule 2 If the operands are mixed, one of them will loop into the number tower: integer → rational → float → complex.

eg. float / int = float

 >>>1./8 # 1. is a float 0.125 

Note There may be times when these rules do not apply to true division / and division of sex //, but I do not understand them completely. See Link.

"They did research that you know, they work 60% of the time ... every time." - Brian Fantana

Trap . In the OPs question, the expression temp**(1/8) made of mixed operands (temp is a float), so why not a (1/8) float?

The operands are evaluated in accordance with BODMAS / BIDMAS, therefore, first evaluated (1/8) , the resulting expression becomes temp**0 , and at that moment 0 forced to float.

Any positive int or float for a power of 0.0 is 1.0 .

0
source

Source: https://habr.com/ru/post/1205192/


All Articles