Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

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.

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


 Â