/* * File: * expt.c * Author: * Samuel A. Rebelsky * Version: * 1.0 of March 2003 * Summary: * An implementation of the logarithmic time exponentiation * procedure. */ #include "expt.h" /********************************************************************* * Exported Procedures * ***********************/ double expt(double val, int power) { double result = 1; double x = val; int n = power; /* result*x^n == val^power */ while (n > 0) { if (n % 2 == 0) { x = x*x; n = n/2; /* result*x^n == val^power */ } else { result = result * x; n = n-1; /* result*x^n == val^power */ } } /* while */ /* result*x^n == val^power */ /* n = 1. */ /* Therefore, result = val^power. */ return result; } /* expt(double, int) */