Abnormal behavior of floating point and double values
Last Updated :
03 May, 2023
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 52* bits for the value), i.e. double has 15 decimal digits of precision.
This article focuses on discussing the abnormal behavior of floating-point and double values.
Program 1:
C
// C program to illustrate the abnormal
// behaviours of floating point value
#include <stdio.h>
// Driver Code
int main()
{
float f = 0.2;
if (f == 0.2)
printf("it's geek time");
else if (f < 0.2)
printf("it's party time");
else
printf("it's movie time");
return 0;
}
C++
// C++ program to illustrate the abnormal
// behaviours of floating point value
#include <iostream>
using namespace std;
// Driver Code
int main()
{
float f = 0.2;
if (f == 0.2)
cout << "it's geek time";
else if (f < 0.2)
cout << "it's party time";
else
cout << "it's movie time";
return 0;
}
Time complexity of this program is O(1), since it only contains a few simple operations that execute in constant time.
Space complexity is also O(1), since the program only declares a single float variable and a few integer literals which do not consume significant memory.
Explanation: In the above program, the output is "it's movie time" instead of "it's geek time". So here is the explanation for that as f is a floating-point value and '0.2' is a double value so due to the precision difference of both data types, this abnormal behavior can be seen. In the statement, if(f==0.2), f is being compared with 0.2 thus internally it's written like this:
if((double)(float)(0.2) == (0.2))
As 0.2 is the first cast into float while declaring and assigning in the above line (due to which the precision decreases and it gets rounded after some value and loss of some info/precision occurs) and after that, it is again cast into the double in the if statement but the lost value can not be fetched back. Hence, this behavior can be seen.
For a more clear understanding, let's take an example(analogy) of the base 10 number system.
Let's take 10/3 and represent it to 5 significant figures: 3.3333 or 3.3334 (in the case of rounding off). That's the "float" f. Now convert the value of f to a value with 8 significant figures (f to "double")- 3.3333000 or 3.3334000 (in the case of rounding off). That's not equal to 3.3333333 (The value of 10/3 directly as a double).
Ways To Avoid Abnormal Behavior:
Replace float f = 0.2 with double f = 0.2 or long double f = 0.2 while declaring/initializing the variable:
Below is the C/C++ program to illustrate the above idea:
C
// C program to illustrate the above idea
#include <stdio.h>
// Driver Code
int main()
{
// Declare as double or long double
// or long double f = 0.2;
double f = 0.2;
if (f == 0.2)
printf("it's geek time");
else if (f < 0.2)
printf("it's party time");
else
printf("it's movie time");
return 0;
}
C++
// C program to illustrate the above idea
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Declare as double or long double
// or long double f = 0.2;
double f = 0.2;
if (f == 0.2)
cout << "it's geek time";
else if (f < 0.2)
cout << "it's party time";
else
cout << "it's movie time";
return 0;
}
The time complexity of this program is O(1), since it performs a constant number of operations regardless of the value of f.
The space complexity is also O(1), since the program only uses a constant amount of memory to store the double variable f and the output string.
Typecast 0.2 to a float while comparing inside the if statement:
Below is the C/C++ program to illustrate the above idea:
C
// C program to illustrate the above idea
#include <stdio.h>
// Driver Code
int main()
{
float f = 0.2;
// Typecast 0.2 as a float
if (f == (float)0.2)
printf("it's geek time");
else if (f < (float)0.2)
printf("it's party time");
else
printf("it's movie time");
return 0;
}
C++
// C++ program to illustrate the above idea
#include <iostream>
using namespace std;
// Driver Code
int main()
{
float f = 0.2;
// Typecast 0.2 as a float
if (f == (float)0.2)
cout << "it's geek time";
else if (f < 0.2)
cout << "it's party time";
else
cout << "it's movie time";
return 0;
}
Write 0.2 as a floating-point value:
Below is the C/C++ program to illustrate the above idea:
C
// C program to illustrate the above idea
#include <stdio.h>
// Driver Code
int main()
{
float f = 0.2;
// Replace 0.2 with 0.2f
if (f == 0.2f)
printf("it's geek time");
else if (f < 0.2f)
printf("it's party time");
else
printf("it's movie time");
return 0;
}
C++
// C++ program to illustrate the above idea
#include <iostream>
using namespace std;
// Driver Code
int main()
{
float f = 0.2;
// Replace 0.2 with 0.2f
if (f == 0.2f)
cout << "it's geek time";
else if (f < 0.2)
cout << "it's party time";
else
cout << "it's movie time";
return 0;
}
By checking if the difference between both the values is very minute(1e-9) by declaring an acceptableDifference variable:
Below is the C/C++ program to illustrate the above idea:
C
// C program to illustrate the above idea
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main()
{
float f = 0.2;
// Declare a variable with very small value
// float acceptableDifference = 1e-9;
float acceptableDifference = 0.00000001;
// Check if the diff of both values is
// less than this variable
if (abs(f - 0.2) < acceptableDifference)
printf("it's geek time");
else if (f < 0.2)
printf("it's party time");
else
printf("it's movie time");
return 0;
}
C++
// C++ program to illustrate the above idea
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
float f = 0.2;
// Declare a variable with very small value
// float acceptableDifference = 1e-9;
float acceptableDifference = 0.00000001;
// Check if the diff of both values is
// less than this variable
if (abs(f - 0.2) < acceptableDifference)
cout << "it's geek time";
else if (f < 0.2)
cout << "it's party time";
else
cout << "it's movie time";
return 0;
}
Note: This last method is preferred when there is no requirement to change the data type of any variable anywhere in the program else any of the above methods listed can be followed too as they are easy, short, and less complicated. Be extra cautious and careful while comparing floating-point numbers in the if statements.
Similar Reads
C++ String to Float/Double and Vice-Versa
In this article, we will learn how to convert String To Float/Double And Vice-Versa. In order to do conversion we will be using the following C++ functions: std::stof() - convert string to floatstd::stod() - convert string to doublestd::atof() - convert a char array to doublestd::to_string - convert
3 min read
C++ Program to Find the Size of int, float, double and char
In this article, we will learn to write a C++ program to find the size of int, float, double, and char. It is important to know the size of different data types especially when working with large datasets to optimize memory usage. The size of a variable can be determined using sizeof() operator in C
2 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
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
Why floating-point values do not represent exact value
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 #
4 min read
How to Catch Floating Point Errors in C++?
In C++, a part of the code that may throw exceptions is enclosed in try-and-catch blocks to handle them when they arise. We can also use try...catch to catch floating point errors but it involves a bit different approach in comparison to catching standard exceptions. In this article, we will look at
2 min read
How to Round a Double to an int in C++?
In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double 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
C++ Program For Double to String Conversion
Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76
2 min read
How to Convert wstring to double in C++
In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou
2 min read