Previous Page
Next Page

exp2

Calculates the base 2 exponential of a number

#include <math.h>
double exp2 ( double x  );
float exp2f ( float x  );
long double exp2l ( long double x  );

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( ).


Example

// 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.

See Also

exp( ), expm1( ), log( ), log1p( ), log2( ), log10( )


Previous Page
Next Page