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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if a given string is a valid number (Integer or Floating Point) in Java
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Rounding Floating Point Number To two Decimal Places in C and C++
- Problem in comparing Floating point numbers and how to compare them correctly?
- Write a one line C function to round floating point numbers
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Can we use % operator on floating point numbers?
- C++ Floating Point Manipulation (fmod(), remainder(), remquo() ... in cmath)
- C Program to Multiply two Floating Point Numbers
- Rotation of a point about another point in C++
- sizeof() for Floating Constant in C
- Floating Action Button (FAB) in Android with Example
- How To Avoid Snackbar Overlap Floating Action Button in Android?
- Extended Floating Action Button in Android with Example
- Java Robot Class | Get the pixel Color of a given point
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- Java.util.Bitset class | Logical operations
- File Handling in Java with CRUD operations
- JavaFX | Light.Point Class