Floating Point Operations & Associativity in C, C++ and Java
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. Consider below C/C++ program.
C
// C/C++ program to demonstrate that floating point // addition may not be associative. #include <stdio.h> int main() { // A and B have same values but opposite 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
// 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 same values but opposite 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:
C++
#include <iostream> using namespace std; int main() { // A and B have same values but opposite signs int A = -500000000; int B = 500000000; int C = 1; cout << " A + (B + C) is equal to " << A + (B + C) << endl; cout << "(A + B) + C is equal to " << (A + B) + C << endl; return 0; } // This code is contributed by sarajadhav12052009 |
C
#include <stdio.h> int main() { // A and B have same values but opposite 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; } |
Java
import java.io.*; class Main { public static void main (String[] args) { // A and B have same values but opposite signs int A = - 500000000 ; int B = 500000000 ; int 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)); } } // This code is contributed by sarajadhav12052009 |
A + (B + C) is equal to 1 (A + B) + C is equal to 1
Time Complexity: O(1).
Auxiliary Space: O(1).