c-plus-plus
  1. c-plus-plus-math-functions

C++ Object Class Math Functions

The C++ Object Class Math Functions are built-in functions that allow us to perform mathematical operations in our code. These functions are defined in the header file.

Syntax

double acos(double x);
double asin(double x);
double atan(double x);
double atan2(double y, double x);
double cos(double x);
double sin(double x);
double tan(double x);
double exp(double x);
double log(double x);
double log10(double x);
double pow(double x, double y);
double sqrt(double x);
double ceil(double x);
double floor(double x);
double fabs(double x);

Example

#include <iostream>
#include <cmath>

using namespace std;

int main() {
   double x = 3, y = 4;

   // Using the atan2 function
   double angle = atan2(y, x);
   cout << "The angle of the vector (3, 4) is: " << angle << endl;

   // Using the pow function
   double result = pow(x, y);
   cout << "3 raised to the power of 4 is: " << result << endl;

   return 0;
}

Output

The angle of the vector (3, 4) is: 0.93
3 raised to the power of 4 is: 81

Explanation

  • The atan2 function returns the angle (in radians) between the positive x-axis and the vector (y,x).
  • The pow function calculates x raised to the power of y.

Use

The C++ Object Class Math Functions can be used whenever we need to perform mathematical calculations in our code. They are especially useful in scientific computing, graphics programming, and game development.

Important Points

  • All functions in the header file are defined in the std namespace.
  • The argument of the acos, asin, and atan functions must be in the range [-1, 1].
  • The atan2 function takes two arguments - the y-coordinate and x-coordinate of a point - and returns the angle (in radians) between the positive x-axis and the vector from the origin to the point.
  • The pow function returns a double value, even if the base and exponent are both integers.

Summary

The C++ Object Class Math Functions provide us with a range of built-in functions for performing mathematical operations in our code. These functions are defined in the header file and can be used in a wide variety of applications. Whether we are working with vectors, calculating angles, or raising numbers to a power, the C++ Object Class Math Functions provide us with a set of powerful tools for working with numbers and mathematical concepts.

Published on: