79348584

Date: 2025-01-11 17:48:05
Score: 0.5
Natty:
Report link

For integer powers of a number, the following function is faster (because it applies repeated squarings of number according to the binary representation of the exponent) and more accurate (because it performs fewer multiplications with less accumulated round-off error). It also handles negative powers without issue. The only missing aspect is that it returns 1 for 0.0^0, as opposed to NaN or undef.

double ipow (double x, int iy) {
  double z;
  if (iy < 0) {
    iy = -iy;
    x = 1.0 / x;
  } 
  if (iy & 1) 
    z = x;
  else
    z = 1.0;
  iy = iy >> 1;
  while (iy > 0) {
    x *= x;
    if (iy & 1)
      z *= x;
    iy = iy >> 1;
  return (z);
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Brian