306. powi — Integer Powers Without the powf Detour
x * x * x doesn’t scale, and powf(3.0) routes an integer exponent through the full floating-point pow machinery. powi is the method built for exactly this case.
Three ways to cube a float:
| |
The chain stops being readable past the third power and can’t handle a runtime exponent at all. powf works, but it’s the general xʸ routine — built to handle any real exponent, when all you have is a small integer.
powi takes an i32 and computes the power by repeated multiplication. Negative exponents give you the reciprocal, no 1.0 / dance:
| |
The quieter win is negative bases. As bite 302 showed with cbrt, powf on a negative base is one fractional exponent away from NaN. With powi the exponent is an integer by construction, so the sign question always has an answer:
| |
Compound growth is the everyday use case — the exponent is a year count, not a real number:
| |
If the exponent in your powf call ends in .0, it wants to be a powi.