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

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming languages, C, C++, and Java, we will once again revise the concepts of associativity, not going into much detail with the simple expression given below.

Associative law: A + (B + C) = (A + B) + C

We usually expect the floating-point operation numbers to follow the concept of associativity in the programming languages as we come from a mathematics background where we are taught the very famous BODMAS rule. But as we see in the below code of different programming languages, C, C++, and Java programming languages, as they are associative, the validation of associative law may sometimes work or even sometimes fails. Here is one particular case working with C++ programming language we have taken especially two points where in one case we have passed the parameters where the numbers or so to say the floating-point values we have considered giving the importance of the same but with the opposite in sign and I another case with same code snippet we have passed the parameters or the floating point values being in different characters also with other numbers.

C++ Code-1

``````//C++ program to demonstrate the floating-point operations and associativity
//Here in this case the addition may not be associative
#include <iostream>
using namespace std;
int main(){
// A and B have the same values but opposite signs
int A = -70000000;
int B =  70000000;
int C = 21;

cout << "The value of the expression A + (B + C) is equal to " << A + (B + C) << endl;
cout << "The value of the expression (A + B) + C is equal to " << (A + B) + C << endl;
return 0;
}
``````

Output

``````The value of the expression A + (B + C) is equal to 21
The value of the expression (A + B) + C is equal to 21
``````

C++ Code-2

``````//C++ program to demonstrate the floating point operations and associativity
//Here in this case the addition may note be associative
#include <iostream>
using namespace std;
int main(){
//Here A and B have different values and opposite signs
int A = -8880000000;
int B =  70000000;
int C = 21;

cout << "The value of expressioon A + (B + C) is equal to " << A + (B + C) << endl;
cout << "The value of expressioon (A + B) + C is equal to " << (A + B) + C << endl;
return 0;
}
``````

Output

``````The value of expressioon A + (B + C) is equal to -220065387
The value of expressioon (A + B) + C is equal to -220065387``````

C Code

``````//This is the C program to demonstrate that floating point operation//associativity
//Here in this case the addition may not be associative.
#include <stdio.h>
int main()
{
//Here the variables A and B have same values but opposite signs
float A = -800000000;
float B =  800000000;

float C = 7;

printf("The value of expression A + (B + C) is equal to %f \n", A + (B + C));
printf("The value of expression (A + B) + C is equal to %f", (A + B) + C);

return 0;
}``````

Output

``````The value of expression A + (B + C) is equal to 0.000000
The value of expression (A + B) + C is equal to 7.000000``````

Java Code

``````//This is the Java program to demonstrate that floating-point operations and
//Here in this case the addition may not be associative
import java.io.*;

class Main
{
public static void main (String[] args)
{
//Here in this case the varaibles A and B have same values but //opposite signs
float A = -300000000;
float B =  300000000;

float C = 8;

System.out.println("The value of expression A + (B + C) is equal to " +(A + (B + C)));
System.out.println("The value of expression (A + B) + C is equal to " +((A + B) + C));
}
}``````

Output

``````The value of expression A + (B + C) is equal to 0.0
The value of expression (A + B) + C is equal to 8.0
``````