×

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

Related Topics

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.

Vector in C++

Vector in C++ In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++. So basically, vector is very...

3 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.