3.3. Math Functions¶
In mathematics, you have probably seen functions like
Then you can evaluate the function itself, either by looking it up in a
table or by performing various computations. The
This process can be applied repeatedly to evaluate more complicated
expressions like
C++ provides a set of built-in functions that includes most of the mathematical operations you can think of. The math functions are invoked using a syntax that is similar to mathematical notation:
This program performs calculations using some of the built-in functions from the cmath library.
The first example sets log to the logarithm of 17, base log10
that takes logarithms base 10.
The second example finds the sine of the value of the variable angle.
C++ assumes that the values you use with sin
and the other trigonometric
functions (cos
, tan
) are in radians.
Note
To convert from degrees to radians, you can divide by 360 and multiply by 2 * pi.
If you don’t happen to know acos
function. The arccosine (or inverse cosine) of -1 is
This program also uses built-in functions from the cmath library, specifically the functions that deal with angles. As you can see, we have a line of code that converts the default radians value to degrees.
Before you can use any of the math functions, you have to include the math header file. Header files contain information the compiler needs about functions that are defined outside your program. For example, in the “Hello, world!” program we included a header file named iostream using an include statement:
#include <iostream>
using namespace std;
iostream contains information about input and output (I/O) streams,
including the object named cout
. C++ has a powerful feature called
namespaces, that allow you to write your own implementation of cout. But
in most cases, we would need to use the standard implementation. To
convey this to the compiler, we use the line
using namespace std;
Note
As a rule of the thumb, you should write using namespace std;
whenever
you use iostream.
Similarly, the math header file contains information about the math functions. You can include it at the beginning of your program along with iostream:
#include <cmath>
Such header files have an initial ‘c’ to signify that these header files have been derived from the C language.
-
Q-3: Match the statement to its description.
This is feedback.
- cmath
- allows the use of functions like log and sin
- iostream
- contains information about input and output streams
- namespace std
- the standard implementation of cout
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
cos
-
This function computes the cosine of an angle.
arctan
-
The arc tangent function is actually called
atan
. log10
-
This function computes the common logarithm.
pow
-
This function raises an expression to a power.
ln
-
The natural log function is actually called
log
.
Q-5: Multiple Response Select all correct cmath functions.