Floating point and Double Data Type

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 and data type to represent real numbers.

Advertisements

A real number is declared using keyword – or . The main difference between and is the size. The size of float is bytes or bits, where the size of type is bytes or bits. There is a long version of the data type which is about bytes or bytes in size.

Data TypesByte SizeBit Size (1 byte = 8 bits)
float432
double864
long double12 or 1696 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 – and .  The equation to represent the numbers in is shown below.

\begin{aligned}&\pm M * 10^E\\ \\ &where \\ \\ &0 \leq M \leq 10\end{aligned}

For example, suppose you want to represent in exponential notation then it becomes

\begin{aligned}&2.0 \cdot 10^4\\ \\&where \\ \\ &0 \leq 2 \leq 10\end{aligned}

If you want to represent in scientific notation, then

\begin{aligned}&1.33 \cdot 10^3\\ \\&where \\ \\&0 \leq 1.33 \leq 10\end{aligned}

The number can be represented as

\begin{aligned}&5.454 \cdot 10^{-5} \\ \\&where\\ \\&0 \leq 5.454 \leq 10\end{aligned}
Advertisements

The table isolates the different parts of the examples given above.

MantissaExponentE-notation
2.042.0E4
1.3331.33E3
5.454-55.454E-5

The above notation is suitable for human, but the computer needs a binary representation of numbers and that too, in format.

Since we already know that bytes or bit is required to store a floating point number in a computer. The number is divided into parts – bits for the mantissa, bit for sign, and bit for exponents.

The sign bit means positive number and means a negative number.

The exponent can store values between to .

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 .

Figure 1 – Computer Representation of Floating-Point Number

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 and though they are represented in the same way in a computer. The double precision is longer than the float in terms of allowing the real part of a floating number.

\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}
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version