C++ provides various mathematical functions like fabs(), fmod(), log(), modf(), pow(), sqrt(), sin(), cos(), abs() etc. that aid in mathematical calculations.
Arguments are the values required by a function to work upon. Following table, lists some commonly used mathematical functions and their purpose :
Function | Prototype | Description | Example |
---|---|---|---|
acos | double acos(double arg) | The acos() function returns the arc cosine of arg. The argument to acos() must be in the range -1 to 1; otherwise, a domain error occurs. |
double val = -0.5; cout<<acos(val); |
asin | double asin(double arg) | The asin() function returns the arc sine of arg. The argument to asin() must be in the range -1 to 1 |
double val = -10; cout<<asin(val); |
atan | double atan(double arg) | The atan() function returns the arc tangent of arg | atan(val); (val is a double type identifier) |
atan2 | double atan2(double b, double a) | The atan2() function returns the arc tangent of b/a. | double val = -10; cout<<atan2(val, 1.0); |
ceil | double ceil(double num) | The ceil() function returns the smallest integer represented as a double not less than num | ceil(1.03) gives 2.0 ceil (-1.03) gives -1.0 |
cos | double cos(double arg) | The cos() function returns the cosine of arg. The value of arg must be in radians |
cos(val) (val is a double type identifier) |
cosh | double cosh(double arg) | The cosh() function returns the hyperbolic cosine of arg. The value of arg must be in radians |
cosh(val); (val is a double type identifier) |
exp | double exp(double arg) | The exp() function returns the natural logarithm e raised to the arg power | exp(2.0) gives the value of e2 |
fabs | double fabs(double num) | The fabs() function returns the absolute value of num | fabs(1.0) gives 1.0 fabs(-1.0) gives 1.0 |
floor | double floor(double num) | The floor() function returns the largest integer (represented by double) not greater than num | floor(1.03) gives 1.0 floor(-1.03) gives -2.0 |
fmod | double fmod(double x, double y) | The fmod() function returns the remainder of the division x/y | fmod(10.0, 4.0) returns 2.0 |
log | double log(double num) | The log() function returns the natural logarithm for num. A domain error occurs if num is negative and a range error occurs if the argument num is zero |
log(1.0) gives the natural logarithm for 1.0 |
log10 | double log10(double num) | The log10() function returns the base 10 logarithm for num. A domain error occurs if num is negative and a range error occurs if the argument is zero |
log10(1.0) gives base 10 logarithm for 1.0 |
pow | double pow(double base, double exp) | The pow() function returns base raised to exp power i.e., base exp. A domain error occurs if base = 0 and exp <= 0. Also if base < 0 and exp is not integer. |
pow(3.0, 0) gives value of 30 pow(4.0, 2.0) gives value of 42 |
sin | double sin(double arg) | The sin() function returns the sin of arg. The value of arg must be in radians |
sin(val) (val is a double type identifier) |
sinh | double sinh(double arg) | The sinh() function returns the hyperbolic sine of arg. The value of arg must be in radians |
sinh(val) (val is a double type identifier) |
sqrt | double sqrt(double num) | The sqrt() function returns the square root of num. If num < 0, domain error occurs |
sqrt(81.0) gives 9.0 |
tan | double tan(double arg) | The tan() function returns the tangent of arg. The value of arg must be in radians |
tan(val) |
tanh | double tanh(double arg) | The tanh() function returns the hyperbolic tangent of arg. The value of arg must be in radians |
tanh(val) |
Following is a simple example, shows few of the mathematical operations:
/* C++ Mathematical Functions */ #include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); short int si = 100; int i = -1000; long int li = 1300; float f = 230.47; double d = 200.347; cout<<"sqrt(si): "<<sqrt(si)<<endl; cout<<"pow(li, 3): "<<pow(li, 3)<<endl; cout<<"sin(d): "<<sin(d)<<endl; cout<<"abs(i) : "<<abs(i)<<endl; cout<<"floor(d): "<<floor(d)<<endl; cout<<"sqrt(f): "<<sqrt(f)<<endl; cout<<"pow(d, 2): "<<pow(d, 2)<<endl; getch(); }
Here is the sample output of the above C++ program: