YouTip LogoYouTip

Cpp Numbers

# C++ Numbers In C++, when you need to work with numbers, you typically use primitive data types such as `short`, `int`, `long`, `float`, and `double`. These data types represent different ranges and precisions of numeric values. --- ## Defining Numbers in C++ You can define and initialize various numeric types in C++. Below is a comprehensive example demonstrating how to declare, assign, and print different types of numbers in C++: ### Example ```cpp #include using namespace std; int main() { // Number definitions short s; int i; long l; float f; double d; // Number assignments 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; } ``` ### Output When the above code is compiled and executed, it produces the following output: ```text short s :10 int i :1000 long l :1000000 float f :230.47 double d :30949.4 ``` --- ## Mathematical Operations in C++ In addition to user-defined functions, C++ provides a rich set of built-in mathematical functions. These functions are part of the standard C and C++ libraries and can be easily imported into your program. To use these mathematical functions, you must include the math header file **``**. ### Common Mathematical Functions | No. | Function & Description | | :--- | :--- | | 1 | **`double cos(double);`**
Returns the cosine of an angle (given in radians). | | 2 | **`double sin(double);`**
Returns the sine of an angle (given in radians). | | 3 | **`double tan(double);`**
Returns the tangent of an angle (given in radians). | | 4 | **`double log(double);`**
Returns the natural logarithm (base-e) of the argument. | | 5 | **`double pow(double base, double exponent);`**
Returns the base raised to the power of the exponent ($base^{exponent}$). | | 6 | **`double hypot(double x, double y);`**
Returns the square root of the sum of squares of its arguments ($\sqrt{x^2 + y^2}$). This represents the hypotenuse of a right-angled triangle. | | 7 | **`double sqrt(double);`**
Returns the square root of the argument. | | 8 | **`int abs(int);`**
Returns the absolute value of an integer. | | 9 | **`double fabs(double);`**
Returns the absolute value of a floating-point number. | | 10 | **`double floor(double);`**
Returns the largest integer value less than or equal to the argument. | ### Example: Mathematical Operations ```cpp #include #include using namespace std; int main() { // Number definitions short s = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; // Mathematical 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; } ``` ### Output When the above code is compiled and executed, it produces the following output: ```text sin(d) :-0.634939 abs(i) :1000 floor(d) :200 sqrt(f) :15.1812 pow(d, 2):40149.7 ``` --- ## Random Numbers in C++ Generating random numbers is a common requirement in programming. C++ provides two primary functions for basic pseudo-random number generation: **`rand()`** and **`srand()`**. * **`rand()`**: Returns a pseudo-random integer. * **`srand()`**: Seeds the pseudo-random number generator. To ensure the generated sequence is different on every run, it is common practice to seed the generator using the current system time via the `time()` function. ### Example: Generating Random Numbers ```cpp #include #include #include using namespace std; int main() { int i, j; // Seed the random number generator with the current system time srand((unsigned)time(NULL)); // Generate and print 10 random numbers for(i = 0; i < 10; i++) { // Generate a pseudo-random number j = rand(); cout << "Random Number: " << j << endl; } return 0; } ``` ### Output When the above code is compiled and executed, it produces a sequence of numbers similar to the following (values will vary on each run): ```text Random Number: 1748144778 Random Number: 630873888 Random Number: 2134540646 Random Number: 219404170 Random Number: 902129458 Random Number: 920445370 Random Number: 1319072661 Random Number: 257938873 Random Number: 1256201101 Random Number: 580322989 ``` --- ## C++ Mathematical Constants (C++20) Mathematical constants (such as $\pi$, $e$, and the golden ratio $\phi$) are essential for many scientific and engineering algorithms. While older versions of C++ required developers to define these manually (or use non-standard macros like `M_PI`), **C++20** introduced a standardized, highly efficient way to access these constants via the **``** header. ### Pi ($\pi$) * **Constant**: `std::numbers::pi` * **Type**: Double-precision float (with templates available for other types like `std::numbers::pi_v`). ```cpp #include #include int main() { std::cout << "pi: " << std::numbers::pi << std::endl; return 0; } ``` ### Euler's Number ($e$) * **Constant**: `std::numbers::e` * **Type**: Double-precision float. ```cpp #include #include int main() { std::cout << "e: " << std::numbers::e << std::endl; return 0; } ``` ### Golden Ratio ($\phi$) * **Constant**: `std::numbers::phi` * **Type**: Double-precision float. ```cpp #include #include int main() { std::cout << "phi: " << std::numbers::phi << std::endl; return 0; } ``` > **Note**: To compile code containing C++20 mathematical constants, make sure your compiler is configured to use the C++20 standard (e.g., using the flag `-std=c++20` in GCC or Clang).
← Cpp ArraysCpp Functions β†’