December 2005
Beginner to intermediate
618 pages
20h 19m
English
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 base 2 exponential function exp2() is the inverse of the base 2
logarithm function, log2().
// 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.This code produces the following output:
The sultan's wager costs him 46116860184.274 metric tons of rice.
Read now
Unlock full access