Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Library - <cmath>



The C++ <cmath> header in C++, provides a set of built-in functions that perform common mathematical calculations. These functions cover a range of operations, including trigonometric, exponential, logarithmic, and power functions.

These functions can be applied to both individual values (scalars) and collections of values (vectors), making <cmath> a versatile tool for programmers.

Including <cmath> Header

To use the cmath in C++, we need to include the cmath header file as shown below.

#include <cmath>

Functions from <cmath>

Below is list of all functions from <cmath> header.

Trigonometric Functions

These functions handle standard trigonometric calculations like sine, cosine, and tangent.

S.NO Functions & Description
1

sin(x)

This function returns the cosine of an angle of x radians.

2

cos(x)

This function returns the sine of an angle of x radians.

3

tan(x)

This function returns the tangent of an angle of x radians.

4

acos(x)

This function returns the principle value of the arc cosine of x, expressed in radians.

5

asin(x)

This function returns the principal value of the arc sine of x, expressed in radians.

6

atan2(x)

This function returns the principal value of the arc tangent of x, expressed in radians.

Computing the Sine of an Angle in Radians

In the following example we are going to use sin() to compute the sine of an angle in radians.

#include <iostream>
#include <cmath>

int main() {
   double angle = 1.0; // Angle in radians
   double result = sin(angle);
   std::cout << "The sine of " << angle << " radians is " << result << std::endl;
   return 0;
}

Output

The sine of 1 radians is 0.841471

Hyperbolic Functions

These functions are the hyperbolic counterparts of the trigonometric functions.

S.NO Functions & Description
1

cosh(x)

This function returns the hyperbolic cosine of x.

2

sinh(x)

This function returns the hyperbolic sine of x.

3

tanh(x)

This function returns the hyperbolic tangent of x.

4

acosh(x)

This function returns the non-negative area hyperbolic cosine of x.

5

asinh(x)

This function returns the hyperbolic cosine of x.

6

atanh(x)

This function returns the area hyperbolic tangent of x.

Calculating the Hyperbolic Sine of a Number

In the following example we are going to use sinh() computes the hyperbolic sine of a number.

#include <iostream>
#include <cmath>
int main() {
   double value = 1.0;
   double result = sinh(value);
   std::cout << "The hyperbolic sine of " << value << " is " << result << std::endl;
   return 0;
}

Output

The hyperbolic sine of 1 is 1.1752

Exponential and logarithmic Functions

These functions handling with exponential and logarithmic calculations.

S.NO Functions & Description
1

exp(x)

This function return the base-e exponential function of x, which is e raised to the power x: e^x.

2

frexp(x)

This function Breaks the floating point number x into its binary significant and an integral exponent for 2.

3

log(x)

This function returns the natural logarithm of x.

4

log10(x)

This function returns the common (base-10) logarithm of x.

5

modf(x)

Breaks x into an integral and a fractional part.

6

exp2(x)

This function returns the base-2 exponential function of x, which is 2 raised to the power x: 2^x.

7

ilogb(x)

This function returns the integral part of the logarithm of |x|, using RADIX as base for the logarithm.

8

log1p(x)

This function returns the natural logarithm of one plus x.

Evaluating the Exponential Function

In the following example we are going to use exp() to compute e^2. calculates the exponential value of e (Euler's number) raised to the power of the given input.

#include <iostream>
#include <cmath>
int main() {
   double exponent = 2.0;
   double result = exp(exponent);
   std::cout << "The value of e^" << exponent << " is " << result << std::endl;
   return 0;
}

Output

The value of e^2 is 7.38906

Power Functions

These functions handles with calculations related to power and roots.

S.NO Functions & Description
1

pow(x,y)

This function returns the base raised to the power exponent.

2

sqrt(x)

This function returns the square root of x.

3

cbrt(x)

This function returns the cubic root of x.

4

hypot(x,y)

This function returns the hypotenuse of a right-angled triangle whose legs are x and y.

Raising a Base to a Given Exponent

In the following example we are going to use pow() function to return the result of raising base to the power exponent.

#include <iostream>
#include <cmath>
int main() {
   double base = 2.0, exponent = 3.0;
   double result = pow(base, exponent);
   std::cout << base << " raised to the power of " << exponent << " is " << result << std::endl;
   return 0;
}

Output

2 raised to the power of 3 is 8

Error and gamma Functions

The cmath library includes special functions like error function (erf) and gamma function (tgamma), used in various fields such as statistics, physics etc.

S.NO Functions & Description
1

erf(x)

This function return the Returns the error function value for x.

2

erfc(x)

This function returns the complementary error function.

3

tgamma(x)

This function returns the gamma function of x.

4

lgamma(x)

This function returns the natural logarithm of the absolute value of the gamma function of x.

Rounding and remainder Functions

These functions handle rounding and remainder calculations.

S.NO Functions & Description
1

ceil(x)

Rounds x upward, returning the smallest integral value that is not less than x.

2

floor(x)

Rounds x downward, returning the largest integral value that is not greater than x.

3

round(x)

This function returns the integral value that is nearest to x, with halfway cases rounded away from zero.

4

trunc(x)

Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x.

5

fmod(x, y)

Returns the floating-point remainder of numer/denom (x/y) rounded towards zero.

Finding the Ceiling of a given Number

In the following example we are going to use ceil() which rounds a floating-point number upward, returning the smallest integer greater than or equal to the input.

#include <iostream>
#include <cmath>
int main() {
   double value = 3.45;
   double result = ceil(value);
   std::cout << "The ceiling of " << value << " is " << result << std::endl;
   return 0;
}

Output

The ceiling of 3.45 is 4

Floating-point manipulation Functions

These floating point manipulation functions work on handling floating-point numbers.

S.NO Functions & Description
1

copysign(x, y)

This function returns a value with the magnitude of x and the sign y.

2

nan()

This function returns a quiet NaN (Not-A-Number) value of type double.

3

nextafter(x, y)

This function returns the next representable value after x in the direction of y.

4

nexttoward(x, y)

This function returns the next representable value after x in the direction of y.

Minimum, Maximum, Difference Functions

These functions finds the minimum, maximum, and absolute differences between two numbers.

S.NO Functions & Description
1

fmin(x, y)

This function returns the smaller of its arguments: either x or y.

2

fmax(x,y)

This function returns the larger of its arguments: either x or y.

3

fdim(x, y)

This function returns the positive difference between x and y.

Other Functions

The cmath header provides other functions, especially to handle floating-point numbers.

S.NO Functions & Description
1

fabs(x)

This function returns the absolute value of x: |x|.

2

abs(x)

This function computes the absolute value for integers or floating-point types.

3

fma(x,y,z)

This multiply-add function computes the product of x and y, then adds z to the result (x*y+z).

Advertisements