Logo

dev-resources.site

for different kinds of informations.

Do you know that 0.1 + 0.2 is not equal to 0.3?

Published at
3/20/2024
Categories
programming
javascript
beginners
tip
Author
doantrongnam
Author
12 person written this
doantrongnam
open
Do you know that 0.1 + 0.2 is not equal to 0.3?

Yes, this is a well-known issue in JavaScript (and many other programming languages) that use binary floating-point numbers. The problem arises from the fact that some numbers cannot be represented exactly in binary floating point, which leads to small rounding errors.
Here's an example:

console.log(0.1 + 0.2);  // Outputs: 0.30000000000000004
Enter fullscreen mode Exit fullscreen mode

Floating-point numbers are a way of representing real numbers in computer systems. They are used to approximate real numbers, but they can't always do it perfectly due to the way they are stored in memory.

In many programming languages, including JavaScript, floating-point numbers are represented according to the IEEE 754 standard. This standard represents numbers in binary, which means they are expressed as a sum of powers of 2.

While some numbers can be represented exactly in this system (like 0.5, which is 2^-1), others cannot. For example, the decimal number 0.1 cannot be represented exactly as a finite binary fraction. In binary, 1/10 is an infinitely repeating fraction.

When you perform arithmetic operations with these approximated numbers, the small errors can accumulate, leading to unexpected results. For example, in JavaScript, the expression 0.1 + 0.2 doesn't exactly equal 0.3.

This is not a flaw in the programming languages themselves, but a consequence of using binary floating-point numbers. To deal with this, programmers often use special libraries for high-precision arithmetic, or use techniques to compare floating-point numbers with a certain tolerance, rather than expecting them to be exactly equal.

Yes, there are libraries in JavaScript that can help with precision issues related to floating point arithmetic. One of them is decimal.js.

Here's an example of how you can use it:

First, install the library:

npm install decimal.js
Enter fullscreen mode Exit fullscreen mode

Then, use it in your code:

const Decimal = require('decimal.js');

let result = new Decimal(0.1).plus(new Decimal(0.2));

console.log(result.toString());  // Outputs: 0.3
Enter fullscreen mode Exit fullscreen mode

This library allows for much more precise arithmetic than JavaScript's native Number type. Try Playground

tip Article's
30 articles in total
Favicon
Ctrl+Alt+Arrow (Right, Left) not working on IntelliJ
Favicon
if locals == globals
Favicon
Version Control Best Practices with Git and GitHub
Favicon
Creating generic types for API (backend) responses
Favicon
Null or Nothing? Unmasking the Mystery of Parameters in Dart
Favicon
List of prompts for successful affiliate marketing
Favicon
My impressions about the book The Clean Coder ๐Ÿงน๐Ÿ“š
Favicon
Why You Should Use GraphQL Playground โฐ
Favicon
Automate WEBP To PNG With A Simple .Bat File
Favicon
In CMS Made Simple, how do you change the theme?
Favicon
A importancia de fazer testes
Favicon
Conditional Styles with CSS :has
Favicon
Do you know that 0.1 + 0.2 is not equal to 0.3?
Favicon
How to save datetime data that is relevant to multiple countries or timeย zones?
Favicon
What I've Learned About Git from Senior Colleagues (Part 1 - git stash)
Favicon
A (somewhat) deep dive into TypeScript constructor intricacies, step-by-step
Favicon
Faster Color picking in Tailwind
Favicon
Evita usar UpperCase o LowerCase C#
Favicon
#DeveloperTipOfTheWeek - Application Security
Favicon
Running out of space on a developer's machine
Favicon
Alert vs confirm in javascript
Favicon
Quick Tip: Counting up to a limit
Favicon
Quick Tip: findFile
Favicon
Time Saving Tip #2 - User Snippets in VSCode
Favicon
Notify Yourself After Completing a Long-Running Bash Process
Favicon
Time Saving Tip #1 - Use Voice Dictation
Favicon
๐Ÿš€ Unveiling the Power of OpenSearch in 202$: A Comprehensive Overview๐Ÿ˜Ž
Favicon
Quick Tip: Checking if a Number is in Range
Favicon
Building a TypeScript Simple Channel (Like Golang)
Favicon
Ubuntu Minimal Install

Featured ones: