×

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++ Tricks for Competitive Programming

If you are interested in Computer science or Information technology, you must have heard about competitive programming. Competitive programming is a way to improve your problem-solving skills. There are various...

7 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

Print Table Using For-loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

3 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

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.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 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.

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.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

3 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.