Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

JavaScript EPSILON & Floating point precision

In this tutorial, we will explain the JavaScript Number.EPSILON property. It represents the difference between 1 and the smallest floating-point number greater than 1. It exists because not all decimal numbers can be represented accurately and exactly in the binary system.

Floating-point precision problem

The computers use the binary system to store numbers and use the floating-point arithmetic, while in real-world we use the decimal system. Not all decimal numbers can be represented correctly in binary numbers.

For Example

0.1 when converted from decimal to binary results in 0.0001100110011001101. Now convert it back from binary to decimal will result in 0.1000003814697265625

The above case is similar to how 1/3 ( 0.33333) cannot be represented as accurately in decimal.

The decimal 10 is represented as 1010 in binary. Now divide 1 by 1010 and you will find out that the division goes on forever just like 1/3.

Because of the above loss of precision occurs, the strange results like the following happens

                            
console.log(0.1 + 0.2);
console.log(0.2 + 0.4);
console.log(0.2 + 0.7);
 
//output 
//0.30000000000000004        
//0.6000000000000001
//0.8999999999999999
 
                            
                        
                            
                        

The difference is very minute, but this definitely causes a problem when you compare two results. For Example, the following results in false rather than true.

                            
console.log(0.1 + 0.2);
console.log(0.2 + 0.4);
console.log(0.2 + 0.7);
 
//output 
//0.30000000000000004        
//0.6000000000000001
//0.8999999999999999
 
                            
                        
                            
                        

Number.EPSILON

One of the ways in which to avoid such problems is to use a very small number as a tolerance for comparison. If the difference between the compared numbers is less than the tolerance, then they are considered equal.

The JavaScript provides EPSILON static property of the Number object as predefined tolerance. It is the difference between 1 and the smallest floating-point number greater than 1.

                            
console.log(Number.EPSILON)
 
//output
//2.220446049250313e-16
 
 
 
                            
                        
                            
                        

If the difference between the two numbers is less than the EPSILON , then they are considered equal.

                            
function numberEquals(x:number, y:number) {
  return Math.abs(x - y) < Number.EPSILON;
}
 
console.log(numberEquals(0.1 + 0.2, 0.3)); // true
 
 
 
                            
                        
                            
                        

Number.EPSILON is a non-writable, non-enumerable, and non-configurable property.

Summary

The conversion from a decimal number to binary is not always accurate. It can result in loss of precision. It is evident when you compare two number (ex: (0.1 + 0.2) ==.3), who are supposed to be equal but results in false. Number.EPSILON is used as tolerance for the number comparison. If the difference between the numbers is less than the Number.EPSILON then they are considered equal.

Read More

  1. JavaScript Tutorial
  2. Data Types in JavaScript
  3. JavaScript String
  4. Template Strings & String Interpolation
  5. Tagged Templates
  6. String to Number
  7. String
  8. Min, Max & Safe Values
  9. Infinity
  10. BigInt
  11. BintInt Vs Number
  12. Boolean Data Type
  13. Undefined
  14. null
  15. null vs undefined