Return to Topic Menu | Computer Science Main Page | | Terms of Use


Math Functions

Required header:  #include <math.h>

C++ compilers offer basic mathematical functions.

Function Prototype Purpose
abs(x) int abs(int x); returns the absolute value of an integer.
fabs(x) double fabs(double x); returns the absolute value of a floating point number
ceil(x) double ceil(double x); rounds up to a whole number
(prints 12)
(not normal rounding)
floor(x) double floor(double x); rounds down to a whole number
      (prints 11)
(not normal rounding)
hypot(a,b) double hypot(double a,
          double b);
calculates the hypotenuse (c) of a right triangle where a and b are the legs.
pow(x,y) double pow(double x,
         double y);
calculates x to the power of y.  If x is negative, y must be an integer.  If x is zero, y must be a positive integer.
pow10(x) double pow10(int x); calculates 10 to the power of x.
sqrt(x) double sqrt(double x); calculates the positive square root of x.
(x is >=0)
fmod(x,y) double fmod(double x,
         double y);
returns floating point remainder of x/y with same sign as x.  Y cannot be zero.  Because the modulus operator(%) works only with integers, this function is used to find the remainder of floating point number division.




The trigonometric functions work with angles in radians rather than degrees.

 All of the trigonometric functions take double arguments and have double return types. 

cos(x) cosine of x
sin(x) sine of x
tan(x) tangent of x
acos(x) arc cosine x
asin(x) arc sine of x
atan(x) arc tangent x
cosh(x) hyperbolic cosine of x
sinh(x) hyperbolic sine of x
tanh(x) hyperbolic tangent of x
exp(x) exponential function
log(x) natural logarithm
log10(x) base 10 logarithm