PDFVersion
No ads? No problem! Download the PDF book of this tutorial for just $24.99. Your support will help us reach more readers.
Thank You!
Numbers in C++:
In C++, we typically utilize primitive data types like int, short, long, float, and double to handle numeric values. These data types, along with their potential values and ranges, have been elucidated during the discussion on C++ Data Types.
We've previously defined numbers through various examples in preceding chapters. Here's a comprehensive example showcasing the definition of different types of numbers in C++.
#include<iostream>
using namespace std;
main() {
// Defining numbers:
short s;
int i;
long l;
float f;
double d;
// Assigning values to numbers:
s = 10;
i = 100;
l = 100000;
f = 25.5;
d = 2455.52;
// Displaying results:
cout << "short: " << s << endl;
cout << "int: " << i << endl;
cout << "long: " << l << endl;
cout << "float: " << f << endl;
cout << "double: " << d << endl;
return 0;
}
Aside from the various functions we can create, C++ also offers useful built-in functions, available in the Standard C and C++ libraries. These functions are commonly referred to as built-in functions and can be incorporated into our programs for use.
C++ provides a rich set of mathematical operations that can be performed on various numbers. The following table lists some essential built-in mathematical functions available in C++.
Note:
To utilize these functions, we need to include the math header file <math.h>
FUNCTIONS | SYNTAX | DESCRIPTION |
---|---|---|
cos: | double cos(double x); |
Returns the cosine of an angle of x radians. |
sin: | double sin(double x); |
Returns the sine of an angle of x radians. |
tan: | double tan(double x); |
Returns the tangent of an angle of x radians. |
acos: | double acos(double x); |
Returns the principal value of the arc cosine of x, expressed in radians. |
asin: | double asin(double x); |
Returns the principal value of the arc sine of x, expressed in radians. |
atan: | double atan(double x); |
Returns the principal value of the arc tangent of x, expressed in radians. |
atan2: | double atan2(double y, double x); |
Returns the principal value of the arc tangent of y/x, expressed in radians. |
Numerous other functions for diverse mathematical operations are encompassed within the math.h library. Explore the link provided to delve deeper into these functions.
#include<iostream>
#include<cmath> // Required for math operations
using namespace std;
main() {
// Number Definitions:
short s = 10;
int i = -100;
long l = 10000;
float f = 230.52;
double d = 200.332;
// Mathematical Operations:
cout << "sin(d): " << sin(d) << endl;
cout << "cos(d): " << cos(d) << endl;
cout << "acos(d): " << acos(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;
cout << "cosh(d): " << cosh(d) << endl;
cout << "exp(d): " << exp(d) << endl;
cout << "log(d): " << log(d) << endl;
cout << "trunc(d): " << trunc(d) << endl;
cout << "rint(d): " << rint(d) << endl;
return 0;
}
Generating random numbers is often necessary in many scenarios. In C++, there are two primary functions for random number generation. The first function, rand()
returns a pseudo-random number within the range of 0 to 32,767. The maximum value may vary depending on the library but is guaranteed to be at least 32767 in standard implementations. This can be checked using RAND_MAX.
#include <iostream>
#include <cstdlib> // Required for rand() function
using namespace std;
main() {
int i = 0;
while (i++ < 10) {
int r = (rand() % 100) + 1;
cout << r << " | ";
}
return 0;
}
However, the numbers generated by rand()
are not truly random as it produces the same sequence each time the code is executed. To generate a different sequence, we need to provide a seed to the srand()
function.
srand(54545);
To achieve a constantly changing sequence, it's better to seed the rand()
function using the current time as an argument to srand()
This ensures a seemingly random sequence each time the code is executed, unless it's called within the same second.
#include <iostream>
#include <cstdlib> // Required for rand() function
#include <ctime> // Required for time function
using namespace std;
main() {
srand((int)time(0));
int i = 0;
while (i++ < 10) {
int r = (rand() % 100) + 1;
cout << r << " | ";
}
return 0;
}
With this implementation, running the code multiple times yields different number sequences.
Sardar Omar
I did my hardest to present you with all of the information you need on this subject in a simple and understandable manner. However, if you have any difficulties understanding this concept or have any questions, please do not hesitate to ask. I'll try my best to meet your requirements.