简体   繁体   中英

How can I get the 5th root in Java?

I am trying for my program to get the 5th root of a number. I have used the Math.pow(a, b); method to get this, however it isn't working for some reason.

Lets say I do something like double z = Math.pow(5, 5) . I do a System.out on this once it gets the value and it will print a result of 3125 . When I do z = Math.Pow(z, 1/5) afterwards on it and do a System.out, it doesn't give me a result of 5, but rather 1. Can anyone explain to me why this is happening?

Thanks!

try z = Math.Pow(z, 1.0/5)

1/5 == 0 in java

It's because you are doing an integer division: 1/5 is 0 hence the result (x^0 is always 1).

Try: z = Math.Pow(z, 1.0d/5);

1/5 rounds to 0 as an integer division.

Try 1.0/5, or just put 0.2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM