Skip to content
Related Articles

Related Articles

Floating Point Operations & Associativity in C, C++ and Java

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 14 Jun, 2022
Improve Article
Save Article

Do Floating point operations follow property of associativity? In other words, do we always get the same results for expressions “(A + B) + C” and “A + (B + C)” One may expect that floating numbers to follow the rule of associativity in programming languages as they are associative mathematically. However, this is not true in all cases. Consider below C/C++ program. 

C




// C/C++ program to demonstrate that floating point
// addition may not be associative.
#include <stdio.h>
 
int main()
{
    // A and B have same values but opposite signs
    float A = -500000000;
    float B =  500000000;
 
    float C = 1;
 
    printf("A + (B + C) is equal to %f \n", A + (B + C));
    printf("(A + B) + C is equal to %f", (A + B) + C);
 
    return 0;
}

Output:

A + (B + C) is equal to 0.000000 
(A + B) + C is equal to 1.000000

It is evident from the above-given output that the floating-point arithmetic may not follow the law of associativity in every case. This is due to the format in which the floating-point numbers are stored and represented, it rounds off the numbers during calculations, hence, the associative laws of algebra do not necessarily hold for floating-point numbers. In this case,

Explanation for above output:

A + (B + C):
(B + C) = 500000000.0 + 1.0
        = 500000000.0 
(rounded off during floating point arithmetic)

A + (B + C) = -500000000.0 + 500000000.0 
            =  0.000000

(A + B) + C:
(A + B) = -500000000.0 + 500000000.0 
        = 0.000000

(A + B) + C = 0.000000 + 1 
            = 1.000000

How about Java? We get the same results in Java as Java also uses similar representation for floating-point numbers. 

Java




// Java program to demonstrate that floating-point
// addition may not be associative
import java.io.*;
 
class Main
{
    public static void main (String[] args)
    {
        // A and B have same values but opposite signs
        float A = -500000000;
        float B =  500000000;
 
        float C = 1;
 
        System.out.println("A + (B + C) is equal to " +
                          (A + (B + C)));
        System.out.println("(A + B) + C is equal to " +
                          ((A + B) + C));
    }
}

Output:

A + (B + C) is equal to 0.000000 
(A + B) + C is equal to 1.000000

How about integers? Now let’s try the same calculations when the data type is integer. Here is a piece of code for your observation: 

C++




#include <iostream>
using namespace std;
 
int main()
{
   // A and B have same values but opposite signs
   int A = -500000000;
   int B =  500000000;
 
   int C = 1;
 
   cout << " A + (B + C) is equal to " << A + (B + C) << endl;
   cout << "(A + B) + C is equal to " << (A + B) + C << endl;
 
   return 0;
}
 
// This code is contributed by sarajadhav12052009

C




#include <stdio.h>
 
int main()
{
   // A and B have same values but opposite signs
   int A = -500000000;
   int B =  500000000;
 
   int C = 1;
 
   printf(" A + (B + C) is equal to %d \n", A + (B + C));
   printf("(A + B) + C is equal to %d", (A + B) + C);
 
   return 0;
}

Java




import java.io.*;
 
class Main
{
    public static void main (String[] args)
    {
        // A and B have same values but opposite signs
        int A = -500000000;
        int B =  500000000;
 
        int C = 1;
 
        System.out.println("A + (B + C) is equal to " +
                          (A + (B + C)));
        System.out.println("(A + B) + C is equal to " +
                          ((A + B) + C));
    }
}
 
// This code is contributed by sarajadhav12052009

Output:

 A + (B + C) is equal to 1 
(A + B) + C is equal to 1

This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!