| type | usual size | usual lower limit | usual upper limit |
|---|---|---|---|
char | 8 bits | –27 = –128 | 27–1 = 127 |
short | 16 bits | –215 = –32768 | 215–1 = 32767 |
int | 32 bits | –231 ≈ –2 · 109 | 231–1 ≈ 2 · 109 |
long | 64 bits | –263 ≈ –9 · 1018 | 263–1 ≈ 9 · 1018 |
long long | 64 bits | –263 ≈ –9 · 1018 | 263–1 ≈ 9 · 1018 |
A common choice for integer type is int,
which is sufficient in most cases.
If larger numbers are required, the type
long (or long long) may be used.
| type | usual size | usual lower limit | usual upper limit |
|---|---|---|---|
float | 32 bits | ≈ –3.4 · 1038 | ≈ 3.4 · 1038 |
double | 64 bits | ≈ –1.8 · 10308 | ≈ 1.8 · 10308 |
long double | 128 bits | ≈ –1.2 · 104932 | ≈ 1.2 · 104932 |
A common choice for floating point type is double.
The following code shows two ways to use it:
double a = 5.72; double b = 2e5;In the first method,
5.72 has a decimal point,
where it is interpreted as a floating point.
Another way is to use e:
for example, 2e5 corresponds to a floating point 200000.
Floating point numbers can represent very small and large numbers, but many numbers cannot be accurately represented this way, which can lead to rounding errors in calculations.
sizeof,
which gives the size in bytes.
For example, the following code prints the size of the int type:
cout << sizeof(int) << "\n";The following output corresponds to a 32-bit type:
4
typedef to give a new name to a type.
For example, the following line gives the type long long
shorter alternative name ll.
typedef long long ll;You can then use the new type name as follows:
ll x = 5;Another newer way to do the same thing is to use
using as follows:
using ll = long long;
cout << 1/2 << "\n"; // 0 cout << 1.0/2 << "\n"; // 0.5 cout << 1/2.0 << "\n"; // 0.5 cout << 1.0/2.0 << "\n"; // 0.5In the first calculation, both numbers are integers, so the calculation is done with integers. In other calculations, at least one number is a floating point, so the calculation happens with floating points.
You can also change the type by setting the desired type with parentheses before the value to be changed. You can put parentheses around the value or type you want to change:
cout << double(1)/2 << "\n"; // 0.5
cout << (double)1/2 << "\n"; // 0.5This is known as changing cast or casting.
int value.
When you add one, a variable will appear
the smallest value of int .
int x = 2147483647; x++; cout << x << "\n"; // -2147483648Floating point numbers
inf and -inf,
indicate positive and negative "infinity".
If the number becomes too large or too small,
it becomes like this.
For example, in the following code, the value of the variable
exceeds double,
so it becomes inf:
double x = 1e308; x *= 2; cout << x << "\n"; // inf
int a, b; cin >> a >> b; cout << a/b << "\n";If
b is 0,
the program ends in an error because dividing by zero is impossible:
Floating point exception (core dumped)Then let's look at the corresponding code with floating points:
double a, b; cin >> a >> b; cout << a/b << "\n";Now if
b is 0,
the program does not end in error,
but the calculation will result in a special value.
If a is positive,
the result is inf,
and if a is negative,
the result is -inf.
If a is 0,
the result is -nan,
which is the abbreviation for "not a number",
that is, it is not a number (nor infinite).
double a = 0.3*3+0.1; double b = 1; cout << a << " " << b << "\n"; if (a == b) cout << "numbers are the same\n"; if (a < b) cout << "a is smaller\n"; if (a > b) cout << "a is greater\n";The code produces a surprising result:
1 1 a is smallerAlthough it looks like both variables have a value of 1, the variable
a is slightly smaller.
The reason is that 0.3*3+0.1
cannot be calculated accurately and will result in rounding error.
When using floating point numbers, take care that the results are not accurate. If there is no good reason to use floating point numbers, it is preferable to use integers.