In the world of programming, floating-point numbers are crucial for representing and performing arithmetic on real numbers. However, their inherent limitations can lead to unexpected results, as we’ll explore with a common JavaScript example.

## The Problem:

Consider the following calculation:

```
E = 3 * (1 - ((15/100)*2))
```

While the expected answer for “E” is 2.1, when evaluated in JavaScript, you might see 2.0999999999999996. This discrepancy arises from the way JavaScript handles floating-point arithmetic.

## Reason for the discrepancy

The discrepancy we are encountering arises from the way JavaScript (and many other programming languages) handle floating-point arithmetic. JavaScript uses a double-precision floating-point format (64-bit IEEE 754) for all its numeric values. This format cannot represent all decimal fractions exactly, leading to small precision errors.

Here’s a breakdown of the calculation:

- Compute the percentage operation: 15 / 100 which equals 0.15.
- Multiply by 2: 0.15 × 2 which equals 0.3.
- Subtract from 1: 1 − 0.3 which equals 0.7.
- Multiply by 3: 3 × 0.7 which should equal 2.1.

However, due to the floating-point precision issue, each of these operations might introduce tiny errors. By the time you reach the final result, these small errors can accumulate, leading to the slight difference you see (2.0999999999999996 instead of 2.1).

## What is Floating Point Precision exactly?

Floating-point precision refers to the way computers represent and perform arithmetic on real numbers using a format that approximates the numbers within a certain range and precision. This is especially relevant in programming languages like JavaScript, which use the IEEE 754 standard for floating-point arithmetic.

## Floating point representation

*Components of a Floating-Point Number:*

- Sign bit: Indicates whether the number is positive or negative.
- Exponent: Determines the range of the number.
- Mantissa (or significand): Represents the precision of the number.

*IEEE 754 Standard:*

JavaScript uses double-precision floating-point format, which is a 64-bit representation.

Breakdown of 64 bits:

- 1 bit for the sign.
- 11 bits for the exponent.
- 52 bits for the mantissa.

## Precision Issues

Floating-point numbers cannot precisely represent all real numbers due to their finite number of bits. This limitation leads to several issues:

*Rounding Errors:*

- Numbers that cannot be exactly represented are rounded to the nearest representable number.
- For example, the decimal number 0.1 cannot be precisely represented in binary, leading to a small rounding error.

*Precision Loss:*

Operations involving floating-point numbers can accumulate errors, especially when performing many operations in sequence.

*Representation Limits:*

Some numbers are too large or too small to be represented within the finite range of the floating-point format, leading to overflow or underflow.

## Mitigating Precision Issues

*Rounding:*

Round the result to the desired precision using methods like `Math.round()`

.

```
let E = 3 * (1 - ((15 / 100) * 2));
E = Math.round(E * 10) / 10; // Rounds to one decimal place
console.log(E); // Outputs 2.1
```

In this code:

`E * 10`

scales the number up by a factor of 10.`Math.round(E * 10)`

rounds the scaled number to the nearest whole number.- Dividing by 10 scales it back down to one decimal place.

This approach ensures that your final output is exactly 2.1, avoiding the small floating-point errors that can occur with direct arithmetic operations.

This is the common and practical solution to floating-point precision issues.

*Integer Arithmetic:*

Perform calculations using integers when possible and convert to floating-point at the end.

For example, you could represent percentages as integers and adjust the scale at the end.

```
let E = 3 * (1 * 100 - 15 * 2) / 100;
console.log(E); // Outputs 2.1
```

In this example:

`1 * 100`

represents 1 as 100.`15 * 2`

represents 15% multiplied by 2 as an integer operation.- Subtracting these results within integer space avoids intermediate floating-point calculations.
- Finally, dividing by 100 scales the result back down to the correct decimal value.

*Arbitrary-Precision Libraries:*

Use libraries such as `Big.js`

or `decimal.js`

for calculations requiring higher precision.

```
const Big = require('big.js');
let E = Big(3).times(Big(1).minus(Big(15).div(100).times(2)));
console.log(E.toString()); // Outputs "2.1"
```

In this example:

`Big.js`

is used to create`Big`

objects for precise arithmetic.- Operations are performed using the methods provided by
`Big.js`

, which ensures precision.

*Avoid Repeated Calculations:*

Simplify your expression to minimize the number of operations.

```
let percentageReduction = 0.3; // 15% * 2 = 30% => 0.3
let E = 3 * (1 - percentageReduction);
console.log(E); // Outputs 2.1
```

By precomputing the percentage reduction and storing it in a variable, you reduce the number of floating-point operations.

These methods except rounding can help you mitigate the effects of floating-point precision errors, though rounding is often the most straightforward and reliable method for ensuring the precision of your results.

## Conclusion

Floating-point precision refers to the representation and arithmetic of real numbers in a way that approximates their values within the limits of the format used by the computer. Understanding and mitigating precision issues is crucial for accurate numerical computations in programming.

*Happy Coding….*