- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

In C, C++, and java, we do some mathematical operations with floating point numbers. Now here we will check whether the floating point numbers are following the associativity rule or not.

The answer is NO. The floating point numbers does not follow the associativity rules in some cases. Here we will see some examples.

## Example Code

#include<iostream> using namespace std; main() { float x = -500000000; float y = 500000000; float z = 1; cout << "x + (y + z) is: " << x + (y + z) << endl; cout << "(x + y) + z is "<< (x + y) + z << endl; }

## Output

x + (y + z) is: 0 (x + y) + z is 1

Here, we can see the results are not same, but theoretically we can say that they will be 1 always. Now the question comes, how this is done?

In the first case x + (y + z), the (500000000 + 1) is performing. But for the floating point round off, it is converted to 500000000 again. Now by adding -500000000 with it, it becomes 0. In the second expression, the value is (-500000000 + 500000000) = 0, then add 1 so the final result is 1.

If we use the integers, then both the expression will return the same result, which is 1.

- Related Questions & Answers
- Floating point operators and associativity in Java
- Fixed Point and Floating Point Number Representations
- Decimal fixed point and floating point arithmetic in Python
- Floating-point hexadecimal in Java
- C++ Floating Point Manipulation
- Floating point comparison in C++
- Operator Precedence and Associativity in C
- Format floating point number in Java
- Floating-point conversion characters in Java
- C++ Operators with Precedence and Associativity
- Format floating point with Java MessageFormat
- What is the difference between integer and floating point literals in Java?
- What are C++ Floating-Point Constants?
- What is C Operator Precedence and Associativity?
- What are floating point literals in C#?