Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 to500000000due to limited precision. Then-500000000 + 500000000 = 0. -
Second case:
(x + y) + z− First(-500000000 + 500000000) = 0, then0 + 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.
Advertisements
