Do Floating point operations follow property of associativity? In other words, do we alway get 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 the cases.

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 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 contribute@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

Attention reader! Don’t stop learning now. Get hold of all the important **Java Foundation** and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.