1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double ipow( int base, int exponent )
{
if ( exponent < 0 ) return 1.0 / ipow( base, -exponent );
double result = 1.0;
for ( ; exponent; base *= base, exponent /= 2 ) if ( exponent % 2 ) result *= base;
return result;
}
int main()
{
#define FORMAT << fixed << showpos << setw( 12 ) <<
for ( int base : { -5, -2, -1, 0, 1, 2, 5 } )
{
for ( int exponent : { -5, -2, -1, 0, 1, 2, 5 } )
{
cout << base << " ^ " << exponent
<< " = " FORMAT ipow( base, exponent )
<< " = " FORMAT pow( base, exponent ) << '\n';
}
}
}
|