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
Comparison of a float with a value in C Predict the output of the following C program. C #include<stdio.h> int main() { float x = 0.1; if (x == 0.1) printf("IF"); else if (x == 0.1f) printf("ELSE IF"); else printf("ELSE"); } The output of above program is "ELSE IF" which means the expression "x == 0.1"
4 min read
Difference between float and double in C/C++ To represent floating point numbers, we use float, double, and long double. What's the difference? double has 2x more precision than float. 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. float has 7 decimal digi
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
C Float and Double Float and double are two primitive data types in C programming that are used to store decimal values. They both store floating point numbers but they differ in the level of precision to which they can store the values.In this article, we will study each of them in detail, their memory representation
5 min read
Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision()) The decimal equivalent of 1/3 is 0.33333333333333â¦. An infinite length number would require infinite memory to store, and we typically have 4 or 8 bytes. Therefore, Floating point numbers store only a certain number of significant digits, and the rest are lost. The precision of a floating-point numb
4 min read
sizeof() for Floating Constant in C In C language, we have three floating data types i.e. float, double and long double. And the exact size of each of these 3 types depends on the C compiler implementation/platform. The following program can be used to find out the size of each floating data type on your machine. C #include "
2 min read