Skip to content

Latest commit

 

History

History
46 lines (33 loc) · 1.62 KB

Floating-Point_Precision.md

File metadata and controls

46 lines (33 loc) · 1.62 KB

Lesson 1: Advanced JavaScript Concepts

In this advanced section, we'll explore some concepts related to JavaScript that may go beyond the basics. These topics are important to understand as you become more proficient with JavaScript.

Floating-Point Precision

JavaScript, like many programming languages, has limitations in representing decimal numbers precisely. When working with floating-point numbers, you may encounter the following issues:

Subtracting Nearly Equal Numbers

let result = 1000000.1 - 1000000.0;
// Expected result: 0.1
// Actual result: 0.09999999997671694
// Actual result may not be exactly 0.1 due to precision limitations.

Large Numbers and Small Numbers

let result = 1e308 + 1e-308;
// Expected result: 1.0000000000000002e+308
// Actual result: 1e+308
// Actual result may not be exact due to precision limitations.

Comparing Very Close Numbers

let a = 0.1 + 0.2;
// Expected result: 0.3
// Actual result: 0.30000000000000004
let b = 0.3;
let isEqual = a === b;
// Expected result: true
// Actual result may not be true due to precision differences.

How to Avoid These Issues

To avoid precision issues, consider the following approaches:

  • Use specialized libraries for decimal arithmetic when precise calculations are required.
  • When comparing floating-point numbers, use a tolerance or threshold value to account for small differences.

These advanced topics are essential for understanding JavaScript's behavior in edge cases. As you continue your JavaScript journey, you'll encounter these issues and learn how to work around them effectively.