Programming and Graphics

(Kiana) #1

3.5 The mathematical library 69


m= abs(n) Absolute value of an integer,n
y=acos(x) Arc cosine, 0 ≤y≤π
y=asin(x) Arc sine,−π/ 2 ≤y≤π/ 2
y= atan(x) Arc tangent,−π/ 2 ≤y≤π/ 2
y= atan2(x, z) Arc tangent,y= atan(y/z)
y= ceil(x) Ceiling ofx(smallest integer larger than or equal tox)
y=cos(x) Cosine
y=cosh(x) Hyperbolic cosine
y=exp(x) Exponential
y=fabs(x) Absolute value of a real number,x
y=floor(x) Floor ofx(smallest integer smaller than or equal tox)
y= log(x) Natural log
y= log10(x) Base-ten log
y=pow(x, a) z=xa,wherexandaare real
y=sin(x) Sine
y= sinh(x) Hyperbolic sine
y=sqrt(x) Square root
y= tan(x) Tangent
y= tanh(x) Hyperbolic tangent

Table 3.5.1Common C++ mathematical functions. The statement#include


must be included at the preamble of the program.

The argument and return of the mathematical functions are registered
in double precision (double). If an argument is in single precision (float), it
is automatically converted to double precision, but only for the purpose of
function evaluation.


A calculator


An ingenious code due to Fred Swartz implements a simple calculator
(seehttp://www.fredosaurus.com/notes-cpp):


// Fred Swartz 10 Aug 2003
// Not robust: does not check for division by 0

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double left, right; // Operands
Free download pdf