Some of the functions in math.hCopyright © 2004 by Dorothy L. Nixon. All rights reserved
- Absolute value
- Ceiling and floor
- Exponential
- Logarithm
- Power
- Square root
- Trigonometric functions
- Inverse trignometric functions
For some preliminary information about the use of C++ library functions, see the section on Library functions in the Tutorial on Unix, vi, gcc, and elementary C++ syntax, and review your lecture notes. For an example of the use of one math function in a simple program, see sineDemo.cpp.
- Absolute value
Given an argument x, the abs function returns |x|.
Prototypes:
double abs(double x); // C++ only float abs(float x); // C++ only long double abs(long double x); // C++ only double fabs(double x); float fabs(float x); // C++ only long double fabs(long double x); // C++ only float fabsf(float x); // required with C99 long double fabsl(long double x); // required with C99Examples of use:
float a = -3; float b = abs(a); // value of b is now 3.0 double p = -5.4; double q = abs(p); // value of q is now 5.4 double z = abs(-100.0); // value of z is now 100.0 float r; cin >> r; cout << abs(r) << endl;
Ceiling and floor Given a floating-point argument x, the ceil function returns the lowest integer greater than or equal to x. The floor function returns the highest integer less than or equal to x. The returned value is still of a floating-point type.
Prototypes:
double ceil(double x); float ceil(float x); // C++ only long double ceil(long double x); // C++ only float ceilf(float x); // required with C99 long double ceill(long double x); // required with C99 double floor(double x); float floor(float x); // C++ only long double floor(long double x); // C++ only float floorf(float x); // required with C99 long double floorl(long double x); // required with C99Examples of use:
float a = 5.3; int m = (int) floor(a); // value of m is now 5 double b = 10.4; long n = (long) ceil(b); // value of n is now 11
Exponential Given an argument x, the exp function returns the exponential of x, i.e. it returns e to the x power.
Prototypes:
double exp(double x); float exp(float x); // C++ only long double exp(long double x); // C++ only float expf(float x); // required with C99 long double expl(long double x); // required with C99Examples of use:
float a = 0; float b = exp(a); // value of b is now 1 double y = exp(1); // value of y is now e double z = exp(exp(0)); // value of z is now e cout << exp(2) << endl; // outputs the square of e cout << exp(1+1) << endl; // outputs the square of e cout << exp(1+2) << endl; // outputs the cube of e
Logarithm Given an argument x, the log function returns the natural (base-e) logarithm of x, and the log10 function returns the base-10 logarithm of x.
Prototypes:
double log(double x); float log(float x); // C++ only long double log(long double x); // C++ only float logf(float x); // required with C99 long double logl(long double x); // required with C99 double log10(double x); float log10(float x); // C++ only long double log10(long double x); // C++ only float log10f(float x); // required with C99 long double log10l(long double x); // required with C99Examples of use:
double a = 100; double b = log10(a); // value of b is now 2 cout << log10(0.001) << endl; // outputs -3.0 cout << log(exp(2)) << endl; // outputs 2.0 cout << exp(log(2)) << endl; // outputs 2.0
Power Given two arguments x and y, the pow function returns x raised to the y power.
Prototypes:
double pow(double x, double y); float pow(float x, float y); // C++ only long double pow(long double x, long double y); // C++ only double pow(double x, int y); // C++ only float pow(float x, int y); // C++ only long double pow(long double x, int y); // C++ only float powf(float x, float y); // required with C99 long double powl(long double x, long double y); // required with C99Examples of use:
cout << pow(2, 3) << endl; // outputs 8.0 cout << pow(2, -3) << endl; // outputs 0.125 cout << log10(pow(10, 3)) << endl; // outputs 3.0 cout << pow(10, log10(3)) << endl; // outputs 3.0 cout << pow(10, 2 + log10(3)) << endl; // outputs 300.0
Square root Given an argument x which must be greater than or equal to zero, the sqrt function returns the square root of x.
Prototypes:
double sqrt(double x); float sqrt(float x); // C++ only long double sqrt(long double x); // C++ only float sqrtf(float x); // required with C99 long double sqrtl(long double x); // required with C99Examples of use: No example provided. You'll be asked to develop an example in the homework.
Trigonometric functions Given an argument x representing an angle in radians, the cos function returns the cosine of x, the sin function returns the sine of x, and the tan function returns the tangent of x.
Prototypes:
double cos(double x); float cos(float x); // C++ only long double cos(long double x); // C++ only float cosf(float x); // required with C99 long double cosl(long double x); // required with C99 double sin(double x); float sin(float x); // C++ only long double sin(long double x); // C++ only float sinf(float x); // required with C99 long double sinl(long double x); // required with C99 double tan(double x); float tan(float x); // C++ only long double tan(long double x); // C++ only float tanf(float x); // required with C99 long double tanl(long double x); // required with C99Examples of use: For one example in the context of a simple complete program, see sineDemo.cpp.
Inverse trignometric functions Given an argument x, the acos function returns the angle (in radians) whose cosine is x, in the range 0 to pi radians. The asin function returns the angle whose sine is x, in the range -pi/2 to +pi/2 radians. The atan function returns the angle whose tangent is x, in the range -pi/2, +pi/2 radians.
Given two arguments x and y, the atan2 function returns the angle whose tangent is y/x, in the full angular range -pi to +pi radians. (This function is handy when converting from rectangular coordinates to polar coordinates.)
Prototypes:
double acos(double x); float acos(float x); // C++ only long double acos(long double x); // C++ only float acosf(float x); // required with C99 long double acosl(long double x); // required with C99 double asin(double x); float asin(float x); // C++ only long double asin(long double x); // C++ only float asinf(float x); // required with C99 long double asinl(long double x); // required with C99 double atan(double x); float atan(float x); // C++ only long double atan(long double x); // C++ only float atanf(float x); // required with C99 long double atanl(long double x); // required with C99 double atan2(double y, double x); float atan2(float y, float x); // C++ only long double atan2(long double y, long double x); // C++ only float atan2f(float y, float x); // required with C99 long double atan2l(long double y, long double x); // required with C99Examples of use: No example provided. Develop some examples on your own, and test them in a simple program, as additional practice for Quiz 1.
Back to: