CIS 150 Math functions

Objectives

  • Include the math library for use in C++ programs
  • Use math library functions

Including the math library

  • Include the math library using this directive: #include <cmath>
  • No "using" statement is needed to access the math library functions

Commonly used math functions

Function   Description Example
fabs(x)absolute value of xfabs(-5.7) returns 5.7
abs(x)absolute value of xabs(-5.7) returns 5.7
pow(x, y)x to the y powerpow(7.0, 2.0) returns 49.0
sqrt(x)the square root of xsqrt(25.0) returns 5.0
ceil(x)rounds up to next integral valueceil(12.3) returns 13
floor(x)rounds x down to next integral valuefloor(72.997) returns 72
fmod(x, y)floating point remainder of x/yfmod(2.9, 1.3) returns 0.3
log(x)returns the natural log of xlog(100) returns 4.60517
log10(x)returns the log (base 10) of xlog10(100) returns 2
cos(x)cosine of x (in radians)cos(0.0) returns 1
sin(x)sine of x (in radians)sin(90.0 * 0.0174533) returns 1
tan(x)tan of x (in radians)tan(45.0 * 0.0174533) returns 1