C++ Numbers

Usually, when we need to use numbers, we use primitive data types such as int, short, long, float, and double, etc. We have already discussed the possible values and ranges of these data types for numbers in the C++ Data Types chapter.

C++ Defining Numbers

We have already defined numbers in various examples in previous chapters. Below is a comprehensive example of defining various types of numbers in C++:

Example

#include <iostream> using namespace std; int main () { // Number definition short s; int i; long l; float f; double d; // Number assignment s = 10; i = 1000; l = 1000000; f = 230.47; d = 30949.374; // Number output cout << "short s :" << s << endl; cout << "int i :" << i << endl; cout << "long l :" << l << endl; cout << "float f :" << f << endl; cout << "double d :" << d << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

short  s :10
int    i :1000
long   l :1000000
float  f :230.47
double d :30949.4

C++ Math Operations

In C++, in addition to creating various functions, it also includes various useful functions for you to use. These functions are written in the standard C and C++ libraries, calledBuilt-infunctions. You can reference these functions in your program.

C++ has built-in rich mathematical functions that can operate on various numbers. The following table lists some useful built-in mathematical functions in C++.

To utilize these functions, you need to include the math header file<cmath>。

Serial NumberFunction & Description
1double cos(double);
This function returns the cosine of a radian angle (double type).
2double sin(double);
This function returns the sine of a radian angle (double type).
3double tan(double);
This function returns the tangent of a radian angle (double type).
4double log(double);
This function returns the natural logarithm of the argument.
5double pow(double, double);
Assuming the first argument is x and the second argument is y, this function returns x raised to the power of y.
6double hypot(double, double);
This function returns the square root of the sum of squares of two arguments; that is, if the arguments are the two legs of a right triangle, the function returns the length of the hypotenuse.
7double sqrt(double);
This function returns the square root of the argument.
8int abs(int);
This function returns the absolute value of the integer.
9double fabs(double);
This function returns the absolute value of any floating-point number.
10double floor(double);
This function returns the largest integer less than or equal to the passed argument.

Below is a simple example of math operations:

Example

#include <iostream> #include <cmath> using namespace std; int main () { // Number definition short s = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; // Math operations cout << "sin(d) :" << sin(d) << endl; cout << "abs(i) :" << abs(i) << endl; cout << "floor(d) :" << floor(d) << endl; cout << "sqrt(f) :" << sqrt(f) << endl; cout << "pow( d, 2) :" << pow(d, 2) << endl; return 0; }

When the above code is compiled and executed, it produces the following results:

sin(d) :-0.634939
abs(i)  :1000
floor(d) :200
sqrt(f) :15.1812
pow( d, 2 ) :40149.7

C++ Random Number

In many cases, random numbers need to be generated. There are two related functions for the random number generator. One isrand(), this function returns only a pseudo-random number. Before generating a random number, you must callsrand()function.

Below is a simple example of generating random numbers. The example usestime()function to obtain the number of seconds of system time, and generate random numbers by calling the rand() function:

Example

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () { int i,j; // Set seed srand( (unsigned)time( NULL ) ); /*Generate 10 random numbers*/ for( i = 0; i < 10; i++ ) { // Generate actual random numbers j= rand(); cout <<"Random numbers:" << j << endl; } return 0; }

When the above code is compiled and executed, it produces the following results:

随机数: 1748144778
随机数: 630873888
随机数: 2134540646
随机数: 219404170
随机数: 902129458
随机数: 920445370
随机数: 1319072661
随机数: 257938873
随机数: 1256201101
随机数: 580322989

C++ Math Constants

In C++, mathematical constants (such as π, e, the golden ratio, etc.) are an indispensable part of many algorithms and applications. Although earlier versions of C++ did not directly provide these constants, starting from C++20, the standard library introduced several commonly used mathematical constants and provided a more efficient and unified way to access them.

For more information, refer to:C++ Standard Library<numbers>。

π

  • Constants:std::numbers::pi
  • Type:std::float32_t(32-bit floating point),std::float64_t(64-bit floating point)

Example

#include <cmath>
#include <iostream>

int main() {
    std::cout << "pi: " << std::numbers::pi << std::endl;
}

The base of natural logarithm e (Euler's Number)

  • Constants:std::numbers::e
  • Type:std::float32_t、std::float64_t
std::cout << "e: " << std::numbers::e << std::endl;

Golden ratio φ (Golden Ratio)

  • Constants:std::numbers::phi
  • Type:std::float32_t、std::float64_t
std::cout << "phi: " << std::numbers::phi << std::endl;

Example

#include <iostream>
#include <cmath>
#include <numbers>

int main() {
    std::cout << "pi: " << std::numbers::pi << std::endl;
    std::cout << "e: " << std::numbers::e << std::endl;
    std::cout << "phi: " << std::numbers::phi << std::endl;

    return 0;
}

The output result is:

pi: 3.14159
e: 2.71828
phi: 1.61803
other extensions