The floating-point numbers serve as rough approximations of mathematical real numbers. They do not represent the exact value. For this reason, we compare the arithmetic results of float variables with a minimum tolerance value.
The expected output is 0.09 as output. But, the output is not 0.09. To understand this, you first have to know how a computer works with float values. When a float variable is initialized, the computer treats it as an exponential value and allocates 4 bytes(32 bits) memory where the mantissa part occupies 24 bits, the exponent part occupies 7 bits, and the remaining 1 bit is used to denote sign.
For type double, the computer does the same but allocates larger memory compared to the float type. In the decimal system, every position from(left to right) in the fractional part is one-tenth of the position to its left. If we move from right to left then every position is 10 times the position to its right.
In a binary system, the factor is two as shown in the table:
To simplify things, let us think of a mythical type named small float(see the above image) which consists of only 5 bits – very small compared to float and double. The first three bits of the type small float will represent mantissa, the last 2 bits will represent the exponent part. For the sake of simplicity, we do not think about the sign. So the mantissa part can have only 8 possible values and the exponent part can only have 4 possible values. See the tables below:
|bit pattern||binary value||decimal value|
|Binary pattern||Binary value||Decimal value|
So, one combination of mantissa and exponent part can be 11100 where the leftmost two bits represent the exponent part and the remaining three bits represent the mantissa part. The value is calculated as:
From the two tables, we can easily say that a small float can contain only 32 numbers and the range of the mythical type is 0 to 7. The range is not equally dense. If you see the following image carefully you will see most values lie between 0 and 1. The more you move from right to left the more sparse the numbers will be.
The small float can not represent 1.3, 2.4, 5.6, etc. In that case, small float approximates them. It can not represent numbers bigger than 7. Besides many combinations represent the same value. For example: 00000, 00001, 00010, 00011 represent the same decimal value i.e., (0.000). Twelve of the 32 combinations are redundant.
If we increase the number of bits allocated for small float, the denser portion will increase. As float values reserve 32 bits, float value can represent more numbers compared to small float. But some issues can be observed with float values and double values. There is no path to overcome this. Computers with infinite memory and fast preprocessor can only compute exact float or double values which is a fantasy for us.
- Represent Tree using graphics in C/C++
- How to return multiple values from a function in C or C++?
- Program to find the product of ASCII values of characters in a string
- Count the number of words having sum of ASCII values less than and greater than k
- How to delete a range of values from the Set using Iterator
- How to swap Keys with Values of a Map in C++?
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Efficient ways to compare a variable with multiple values
- How to generate a vector with random values in C++?
- Program to check if a number belongs to a particular base or not
- C Program to Check Whether a Number is Prime or not
- Check whether count of distinct characters in a string is Prime or not
- Count of Numbers such that difference between the number and sum of its digits not less than L
- C program to check if a given string is Keyword or not
- Program to check if an Array is Palindrome or not using STL in C++
- Check if all the 1's in a binary string are equidistant or not
- Check whether Quadrilateral is valid or not if angles are given
- Check if two given Circles are Orthogonal or not
- C program to check whether the file is JPEG file or not
- Check if product of Array elements in given range are M-th root or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.