Zebra0.com

cpp arithmeticMath Functions

Math Functions

There are many math functions available when you include the math header file: #include <cmath>

The math functions most likely to be used by students learning C++ are explained in more detail here.

Power functions

pow: Raise to power

pow(n,3) returns n to the third, or n*n*n.
pow(2,4) returns 16
pow(81, .5) returns 9 (the square root of 81)

sqrt: square root

sqrt(81) returns 9.
sqrt(0.81) returns 0.9
Trying to find the square root of a negative number returns -1.#IND.

cbrt: cubic root

cbrt(8) returns 2.

hypot: hypotenuse

hypot(3,4) returns 5

Ceil (ceiling) and floor

The ceil function finds the nearest whole number that is >= the argument. You can think of this as moving to the right on a number line.
ceil(5.8) returns 6.
ceil(-5.8) returns -6.

The floor function finds the nearest whole number that is <= the argument. You can think of this as moving to the left on a number line.
floor(5.8) returns 5.
floor(-5.8) returns -6.
ceil-floor

There are also trigonometric functions: cos (cosine), sin (sine), tan (tangent), acos, (arc cosine), asin (arc sine), atan (arc tangent). All of these functions take one argument: the angle expressed in radians. There is also atan2 (arc tangent) which takes 2 parameters: x and y. It returns the arc tangent of y/x, expressed in radians.

There are hyperbolic functions: cosh (hyperbolic cosine), sinh (hyperbolic sine), tanh (hyperbolic tangent), acosh *hyperbolic cosine), asinh (arc hyperbolic sine), and atanh (arc hyperbolic tangent). All of these functions take one argument: the angle expressed in radians.

There are exponential and logarithmic functions: exp (exponential), frexp (significand and exponent), ldexp (significand and exponent), log (natural logarithm), log10 (common logarithm), modf (fractional and integral parts), exp2 (binary exponential, expm1 (exponential minus one), ilogb (integer binary logarithm), log1p (logarithm plus one), log2 (binary logarithm), logb (floating-point base logarithm), scalbn (Scale significand using floating-point base exponent), and scalbln (scale significand using floating-point base exponent).

NEXT: How many busses?