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
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 Number | Function & Description |
|---|---|
| 1 | double cos(double); This function returns the cosine of a radian angle (double type). |
| 2 | double sin(double); This function returns the sine of a radian angle (double type). |
| 3 | double tan(double); This function returns the tangent of a radian angle (double type). |
| 4 | double log(double); This function returns the natural logarithm of the argument. |
| 5 | double 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. |
| 6 | double 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. |
| 7 | double sqrt(double); This function returns the square root of the argument. |
| 8 | int abs(int); This function returns the absolute value of the integer. |
| 9 | double fabs(double); This function returns the absolute value of any floating-point number. |
| 10 | double 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
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
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 <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 <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.61803other extensions