Raising number to fractional power java [duplicate]
Asked Answered
S

1

6

I used the following code:

double pow = 3/7;

double num = 85;

System.out.println(Math.pow(num, pow));

Expected result:

6.71...

The output is

1.0

Any idea why?

Synagogue answered 26/4, 2015 at 11:29 Comment(0)
O
7

3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.

Change it to 3.0/7 in order to get a floating point result.

Obtrusive answered 26/4, 2015 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.