Home  >  Article  >  Backend Development  >  Math function library in C++ and how to use it

Math function library in C++ and how to use it

PHPz
PHPzOriginal
2023-08-21 21:17:156554browse

C is a programming language widely used in computer programming. Its mathematical function library can help programmers effectively perform various mathematical calculations. This article will introduce commonly used mathematical function libraries in C and how to use them.

1. cmath function library

The cmath function library is a commonly used mathematical function library in C, which contains various mathematical functions required for mathematical calculations, such as trigonometric functions, exponential functions, and Number functions, power functions, etc. To use the cmath function library, you need to add #include 5f0e0135be24bb6e777387dff70c8994 at the beginning of the program. Here are some commonly used functions:

  1. abs() function

abs() function is used Calculates the absolute value of any number. The return value type is integer, floating point, or double precision floating point.

Example:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   int a = -10;
   float b = -3.14;
   double c = -99.99;
   
   cout << "abs(a) = " << abs(a) << endl;
   cout << "abs(b) = " << abs(b) << endl;
   cout << "abs(c) = " << abs(c) << endl;
   
   return 0;
}

Output result:

abs(a) = 10

abs(b) = 3.14

abs( c) = 99.99

  1. sin() function

#sin() function is used to calculate the sine value of an angle. Its parameter is a radian value and the function returns a float Point value.

Example:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   double radian = 0.5236;
   double sin_value = sin(radian);
   
   cout << "sin(30) = " << sin_value << endl;
   
   return 0;
}

Output result:

sin(30) = 0.5

  1. pow() function

The pow() function is used to calculate any power of a number. Its parameters are two double-precision floating-point numbers, one is the base and the other is the exponent. The function returns a double-precision floating-point value.

Example:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   double base = 2;
   double exponent = 5;
   double pow_value = pow(base, exponent);
   
   cout << base << "的" << exponent << "次幂为:" << pow_value;
   
   return 0;
}

Output result:

2 raised to the 5th power is: 32

2. Complex function library

complex The function library is used for mathematical calculations of complex numbers, including commonly used functions such as complex multiplication, complex addition, real part, imaginary part, etc.

To use the complex function library, you need to add #include c3d338aa2113cd3b8b314b99755e0e1d at the beginning of the program. Here are some commonly used functions:

  1. complex() function

The complex() function is used to return a value of complex type. Its parameters are two (optional one) double-precision floating-point numbers. The first value represents the real part of the complex number, and the second is the imaginary part.

Example:

#include <iostream>
#include <complex>

using namespace std;

int main()
{
   complex<double> c1 (1,2);
   
   cout << "c1 = " << c1 << endl;
   
   return 0;
}

Output result:

c1 = (1,2)

  1. norm() function

The norm() function is used to calculate the square of the modulus of a complex number. Its parameter is a complex number type value, and the function returns a double-precision floating-point value.

Example:

#include <iostream>
#include <complex>

using namespace std;

int main()
{
   complex<double> c1 (3,4);
   double norm_value = norm(c1);
   
   cout << "The square of the norm of " << c1 << " is " << norm_value << endl;
   
   return 0;
}

Output result:

The square of the norm of (3,4) is 25

  1. polar() function

polar() function is used to convert a complex number in polar coordinates into a regular complex number form. Its parameters are two double-precision floating-point numbers, the first is the modulus, and the second is For the phase angle, the function returns a complex value.

Example:

#include <iostream>
#include <complex>

using namespace std;

int main()
{
   double radius = 5;
   double phase = 1.0472; //约等于60度
   complex<double> c1 = polar(radius, phase);
   
   cout << "The complex number is " << c1 << endl;
   
   return 0;
}

Output result:

The complex number is (2.5, 4.33013)

3. Random function library

The random function library can be used to generate various types of random numbers, including integers, real types, Boolean types, character types, etc.

To use the random function library, you need to add #include ae60ea20068672260f4d24c8d73e974d at the beginning of the program. Here are some commonly used functions:

  1. rand() function

The rand() function is used to generate a random integer value between 0 and RAND_MAX, where RAND_MAX is a constant in the C standard library, usually equal to 32767.

Example:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
   srand(time(NULL)); //设置种子
   
   for(int i = 0; i < 5; ++i)
   {
      int random_num = rand();
      
      cout << "Random number " << i << ": " << random_num << endl;
   }
   
   return 0;
}

Output result:

Random number 0: 1804289383

Random number 1: 846930886

Random number 2: 1681692777

Random number 3: 1714636915

Random number 4: 1957747793

    ##uniform_real_distribution() function
uniform_real_distribution() function is used Generates a random real value between the specified range, takes two double-precision floating-point numbers as arguments, and returns a double-precision floating-point random value.

Example:

#include <iostream>
#include <random>

using namespace std;

int main()
{
   random_device rd;
   mt19937 gen(rd());
   uniform_real_distribution<> distribution(-1, 1); //生成[-1, 1)范围内的随机实数
   
   for(int i = 0; i < 5; ++i)
   {
      double random_num = distribution(gen);
      
      cout << "Random number " << i << ": " << random_num << endl;
   }
   
   return 0;
}

Output result:

Random number 0: 0.156381

Random number 1: -0.868275

Random number 2 : -0.648533

Random number 3: -0.904021

Random number 4: -0.285259

Summary:

cmath function library, complex function library and random The function library contains many commonly used mathematical calculation functions. Using these function libraries and functions can perform various mathematical operations more efficiently. This article only introduces common function libraries and functions, not all of them. Readers can refer to relevant literature as needed to learn how to use the corresponding functions.

The above is the detailed content of Math function library in C++ and how to use it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn