Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Medium
  • Last Updated : 12 Oct, 2021

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.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

Consider below C/C++ program.




// C/C++ program to demonstrate that floating point
// addition may not be associative.
#include<stdio.h>
int main()
{
    // A and B have sane values but apposite 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 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 sane values but apposite 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:




#include<stdio.h>
#include<stdio.h>
int main()
{
   // A and B have sane values but apposite 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;
}

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
Recommended Articles
Page :