Name

exp2

Synopsis

Calculates the base 2 exponential of a number

#include <math.h>
doubleexp2( 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.

Tip

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.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.