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

In C, mathematical operations with floating point numbers do not always follow the associativity rule. This means that (a + b) + c may not equal a + (b + c) due to precision limitations and rounding errors in floating-point arithmetic.

Syntax

float result1 = a + (b + c);    /* Right associativity */
float result2 = (a + b) + c;    /* Left associativity */

Example: Floating Point Associativity Issue

Here's an example demonstrating how floating point operations violate associativity −

#include <stdio.h>

int main() {
    float x = -500000000;
    float y = 500000000;
    float z = 1;
    
    printf("x + (y + z) is: %.0f\n", x + (y + z));
    printf("(x + y) + z is: %.0f\n", (x + y) + z);
    
    return 0;
}
x + (y + z) is: 0
(x + y) + z is: 1

How It Works

The different results occur due to floating-point precision limitations:

  • First case: x + (y + z) − When (500000000 + 1) is computed, the result is rounded back to 500000000 due to limited precision. Then -500000000 + 500000000 = 0.
  • Second case: (x + y) + z − First (-500000000 + 500000000) = 0, then 0 + 1 = 1.

Example: Integer Operations Follow Associativity

For comparison, integer operations maintain associativity −

#include <stdio.h>

int main() {
    int x = -50;
    int y = 50;
    int z = 1;
    
    printf("x + (y + z) is: %d\n", x + (y + z));
    printf("(x + y) + z is: %d\n", (x + y) + z);
    
    return 0;
}
x + (y + z) is: 1
(x + y) + z is: 1

Key Points

  • Floating-point arithmetic has limited precision, causing rounding errors.
  • Large numbers can lose precision when added to small numbers.
  • Integer arithmetic maintains associativity within the range of the data type.
  • Always consider order of operations when working with floating-point numbers.

Conclusion

Floating-point operations in C do not guarantee associativity due to precision limitations and rounding errors. Understanding this behavior is crucial for accurate numerical computations and avoiding unexpected results.

Updated on: 2026-03-15T10:43:07+05:30

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements