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 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
#include <stdio.h>
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++
#include <iostream>
using namespace std;
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
#include <stdio.h>
int main()
{
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++
#include <iostream>
using namespace std;
int main()
{
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
#include <stdio.h>
int main()
{
float f = 0.2;
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++
#include <iostream>
using namespace std;
int main()
{
float f = 0.2;
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
#include <stdio.h>
int main()
{
float f = 0.2;
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++
#include <iostream>
using namespace std;
int main()
{
float f = 0.2;
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
#include <stdio.h>
#include <stdlib.h>
int main()
{
float f = 0.2;
float acceptableDifference = 0.00000001;
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
float f = 0.2;
float acceptableDifference = 0.00000001;
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.
Last Updated :
03 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...