Numbers can be represented using Integer data types in C++. But some numbers like real numbers cannot be stored like integers because there is a decimal part associated with the real numbers.
C++ programming language has
A real number is declared using keyword –
Data Types | Byte Size | Bit Size (1 byte = 8 bits) |
float | 4 | 32 |
double | 8 | 64 |
long double | 12 or 16 | 96 or 128 |
Computer Representation of Floating Point
The real numbers are represented in scientific notation (or exponential notation) because it is easier to perform arithmetic involving real values.
The Exponential Notation
The exponential notation has two parts –
\begin{aligned}&\pm M * 10^E\\ \\ &where \\ \\ &0 \leq M \leq 10\end{aligned}
For example, suppose you want to represent
\begin{aligned}&2.0 \cdot 10^4\\ \\&where \\ \\ &0 \leq 2 \leq 10\end{aligned}
If you want to represent
\begin{aligned}&1.33 \cdot 10^3\\ \\&where \\ \\&0 \leq 1.33 \leq 10\end{aligned}
The number
\begin{aligned}&5.454 \cdot 10^{-5} \\ \\&where\\ \\&0 \leq 5.454 \leq 10\end{aligned}
The table isolates the different parts of the examples given above.
Mantissa | Exponent | E-notation |
2.0 | 4 | 2.0E4 |
1.33 | 3 | 1.33E3 |
5.454 | -5 | 5.454E-5 |
The above notation is suitable for human, but the computer needs a binary representation of
Since we already know that
The sign bit
The
The computer representation of exponential notation is:
\begin{aligned}&(b_0. b_1 b_2 b_3 \cdots ) \cdot 2^E\\ \\&where \\ \\&b_{0} = 1\end{aligned}
This representation is called the
Declaring Floating Type and Double Type
Declaring a floating type and double data type variabe in a C program is similar.
float PI 3.14;
double radius 5,33;
There is little difference between
\begin{aligned}&3.244440\\ \\&3.244440000000000 \hspace{3px}(double \hspace{3px} is \hspace{3px} has \hspace{3px} longer \hspace{3px} decimal \hspace{3px} part )\end{aligned}