Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
exp2
Calculates the base 2 exponential of a number
#include <math.h> doubleexp2( doublex); floatexp2f( floatx); long doubleexp2l( long doublex);
The return value of the exp2() function is 2 raised to the power
of the function's argument, or 2x. If the
result is beyond the range of the function's type, a range error
occurs.
// The famous grains-of-rice-on-a-chessboard problem.
// The sultan loses a chess game. The wager was one grain for square 1
// on the chessboard, then double the last number for each successive
// square. How much rice in all?
int squares = 64;
long double gramspergrain = 0.0025L; // A grain of rice weighs 25 mg.
long double sum = 0.0L;
for ( int i = 0; i < squares; i++ )
sum += gramspergrain *exp2l( (long double)i );
printf( "The sultan's wager costs him %.3Lf metric tons of rice.\n",
sum / 1000000.0L ); // A million grams per ton.