C++ provides many useful math function inside the cmath
header.
As with Python, in C/C++, a function is a collection of statements which can perform a task with a single call, instead of having to write them all out. We’ll look at how to create our own functions later on.
Trigonometric Functions
Function | Description |
---|---|
sin(radians) | Return the trig. sine of the angle in radians |
cos (radians) | Return the trig. cosine of the angle in radians |
tan (radians) | Return the trig. tangent of an angle in radians |
asin(a) | Return the angle in radians, for the inverse sine. |
acos(a) | Return the angle in radians, for the inverse cosine. |
atan(a) | Return the angle in radians, for the inverse tangent. |
For those who don’t remember, radians is a measurement of an angle, however it is between -PI/2 and PI/2. One degree is equal to PI/180 in radians. 30 degrees is PI/6, 45 degrees is PI/4, and 90 degrees is PI/2.
Exponent Functions
Function Name | Description |
---|---|
pow(x,y) | Returns the x to the power of y, i.e. exponent |
exp(x) | Returns the e (natural number) raised to the power of x. |
log(x) | Returns the natural log of x (in math this is ln(x) |
log10(x) | Returns the log base 10 of x |
sqrt(x) | Returns the square root of x. |
Rounding Function
Function | Description |
---|---|
ceil(x) | Returns x rounded up to the nearest integer |
floor(x) | Returns x rounded down to the nearest integer |
Min, Max, and Abs Functions
Min and Max return the minimum or maximum value of two numbers (int, long, float, or double).
Abs returns absolute value of a number (int, long, float, or double).
Mathematical Functions in C++ was originally found on Access 2 Learn