Open In App

Abnormal behavior of floating point and double values

Improve
Improve
Like Article
Like
Save
Share
Report

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;
}


Output:

it's movie time

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;
}


Output:

it's geek time

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;
}


Output:

it's geek time

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;
}


Output:

it's geek time

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;
}


Output:

it's geek time

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
Previous
Next
Share your thoughts in the comments
Similar Reads