Pow() doesn't handle fractional exponents

Why doesn’t pow() properly handle fractional exponents, and is there a way to force it to? Decimal exponents such as 0.667 are handled correctly, but not fractions such as 2/3. For example, I’d expect

float x = pow(8, 2/3);

to return 4.0 for the value of x, but it returns 1.0. As another example,

float x = pow(8, 3/2);

returns 8.0. It seems pow() replaces a fraction with the next lowest integer.

2/3 is 0.

Try using 2.0/3.0 instead.

3 Likes

This is referred as an integer division in java as the division returns an integer. Easiest way to solve this is to make at least one of the terms float.

Kf

1 Like

One way is to change it to float x = pow(8, 2.0/3);