Why floating-point values do not represent exact value
Last Updated :
28 Mar, 2023
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.
Example:
C++
// C++ program to illustrate the
// floating point values
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
double num1 = 10000.29;
double num2 = 10000.2;
// Output should be 0.0900000000
cout << std::setprecision(15)
<< (num1 - num2);
return 0;
}
Java
// Java program to illustrate the
// floating point values
import java.text.DecimalFormat;
class GFG{
// Driver Code
public static void main(String[] args)
{
double num1 = 10000.29;
double num2 = 10000.2;
// Output should be 0.0900000000
DecimalFormat df = new DecimalFormat(
"#.################");
System.out.println(df.format(num1 - num2));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to illustrate
# the floating povalues
# Driver Code
if __name__ == '__main__':
num1 = 10000.29;
num2 = 10000.2;
# Output should be 0.0900000000
print ("{0:.10f}".format(num1 - num2));
# This code is contributed by Rajput-Ji
C#
// C# program to illustrate the
// floating point values
using System;
class GFG{
// Driver Code
public static void Main(String[] args)
{
double num1 = 10000.29;
double num2 = 10000.2;
// Output should be 0.0900000000
Console.WriteLine(
string.Format("{0:F15}",
Decimal.Parse((num1 - num2).ToString())));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to illustrate the
// floating point values
let num1 = 10000.29;
let num2 = 10000.2;
document.write(parseFloat(num1-num2));
// This code is contributed by lokeshmvs21.
</script>
Output: 0.0900000000001455
The time complexity of this program is O(1) as it only involves simple arithmetic operations and printing the result.
The space complexity is also O(1) as the program only uses a fixed amount of memory to store the two double values and the output.
Explanation:
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:
... | 16 | 8 | 4 | 2 | 1 | . | \frac{1}{2} | \frac{1}{4} | \frac{1}{8} |
... | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 | . | 2^{-1} | 2^{-2} | 2^{-3} |

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 |
---|
000 | (0.000)2 | 0.000 |
001 | (0.001)2 | 0.125 |
010 | (0.010)2 | 0.250 |
011 | (0.011)2 | 0.375 |
100 | (0.100)2 | 0.500 |
101 | (0.101)2 | 0.625 |
110 | (0.110)2 | 0.750 |
111 | (0.111)2 | 0.875 |
Binary pattern | Binary value | Decimal value |
---|
00 | (00)2 | 1 |
01 | (01)2 | 2 |
10 | (10)2 | 4 |
11 | (11)2 | 8 |
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:
(1\times 2^{-1}+1\times 2^{-2}+1\times 2^{-2})\times 2^{(0\times 2^1+0\times 2^0)} = 0.875
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.
Similar Reads
Abnormal behavior of floating point and double values
Float is a 32 bit IEEE 754 single-precision Floating Point Number 1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision. Double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and
6 min read
How Do I Print a Double Value with Full Precision Using cout?
The double value in C++ has a precision of up to 15 digits but while printing it using cout, it only prints six significant digits. In this article, we will learn how to print the double value with full precision. For Example, Input: double var = 12.3456789101112 Output: var = 12.3456789101112Print
2 min read
Value of Pi(Π) up to 50 decimal places
Given a number N(where N <= 50), the task is to find the value of Pi (?) up to N decimals places.Examples: Input: N = 2 Output: 3.14 Input: N = 10 Output: 3.1415926536 Approach: 1. The value of ? is calculated using acos() function which returns a numeric value between [-?, ?]. 2. Since using aco
3 min read
How to Compare Float and Double While Accounting for Precision Loss?
In C++ programming, real numbers are represented in a limited amount of memory so accuracy loss is a frequent worry when dealing with floating-point and double numbers in C++ making direct comparison of two such values unreliable. In this article, we will discuss how to compare two float or double v
3 min read
Difference between std::numeric_limits<T> min, max, and lowest in C++
The std::numeric_limits<T> class in the limit header provides min(), max(), and lowest() function for all numeric data types along with the other member functions. std::numeric_limits<T>::max(): The std::numeric_limits<T>::max() for any type T gives the maximum finite value represe
5 min read
Pi(π) in C++ with Examples
In this article, we will discuss some of the mathematical function which is used to derive the value of Pi(Ï) in C++. Method 1: Using acos() function: Approach: The value of Î is calculated using acos() function which returns a numeric value between [-Î , Î ].Since using acos(0.0) will return the valu
2 min read
Convert a Char Array to Double in C
Converting a char array to a double is a common operation in C programming. It involves taking a string of characters that represent a numerical value and converting it to a double-precision floating-point value. This can be done by using various approaches listed below: Convert char array into doub
4 min read
fdim() Function in C
The fdim() function in C is part of the standard math library <math.h> and is used to compute the positive difference between two floating-point numbers. This function is particularly useful when we need to determine the non-negative difference between two given values. It returns the differen
3 min read
Problem in comparing Floating point numbers and how to compare them correctly?
In this article, we will see what is the problem in comparing floating-point numbers and we will discuss the correct way to compare two floating-point numbers. What is the problem in comparing Floating-Point Numbers usually?Let us first compare two floating-point numbers with the help of relational
7 min read
Constants of Maths in Python
Math module is a standard in-built module in Python that can be used for performing the mathematical tasks. The math module has a set of methods and constants. Note: For more information, refer to Math-library-functions 1. Python math.e constant: The math.e constant returns the Euler's number: 2.718
2 min read