Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:40:19+00:00 2026-06-02T23:40:19+00:00

I have always been coding in java and have recently started coding in javascript

  • 0

I have always been coding in java and have recently started coding in javascript (node.js to be precise). One thing that’s driving me crazy is add operation on decimal numbers;

Consider the following code

var a=0.1, b=0.2, c=0.3;
var op1 = (a+b)+c;
var op2 = (b+c)+a;

To my amazement I find out op1 != op2 ! console.logging op1 and op2 print out the following:

console.log(op1); 0.6000000000000001
console.log(op2); 0.6

This does not make sense. This looks like a bug to me because javascript simply cannot ignore rules of arithmetic. Could someone please explain why this happens?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:40:20+00:00Added an answer on June 2, 2026 at 11:40 pm

    This is a case of floating-point error.

    The only fractional numbers that can be exactly represented by a floating-point number are those that can be written as an integer fraction with the denominator as a power of two.

    For example, 0.5 can be represented exactly, because it can be written 1/2. 0.3 can’t.

    In this case, the rounding errors in your variables and expressions combined just right to produce an error in the first case, but not in the second case.

    Neither way is represented exactly behind the scenes, but when you output the value, it rounds it to 16 digits of precision. In one case, it rounded to a slightly larger number, and in the other, it rounded to the expected value.

    The lesson you should learn is that you should never depend on a floating-point number having an exact value, especially after doing some arithmetic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always been a bit unclear on the type of tasks that should
I have modified a working Windows service that had always been starting beforehand. After
I have previously been told that I should always use Randomize() before I use
I have been under the assumption for a while that viewDidUnload is always called
I have been searching for days for a way to always display just one
I have been coding in Java(Mainly) and .Net for a while. What I found
I have always been taught to have one exit from functions and write code
I have been having an issue with Visual Studio 2010 that is driving me
I have always had an interest in coding, and a while back a started
I have always been kind of confused by threads, and my class right now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.