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 7932779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:07:14+00:00 2026-06-03T21:07:14+00:00

Possible Duplicate: Is JavaScript's Math broken? Suppose, var x = .6 – .5; var

  • 0

Possible Duplicate:
Is JavaScript's Math broken?

Suppose,

var x = .6 - .5;

var y = 10.2 – 10.1;

var z = .2  -  .1;

Comparison result

x == y;     // false

x == .1;    // false

y == .1;    // false

but

z == .1;    // true

Why Javascript show such behavior?

  • 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-03T21:07:16+00:00Added an answer on June 3, 2026 at 9:07 pm

    Because floating point is not perfectly precise. You can end up with slight differences.

    (Side note: I think you meant var x = .6 - .5; Otherwise, you’re comparing -0.1 with 0.1.)

    JavaScript uses IEEE-754 double-precision 64-bit floating point (ref). This is an extremely good approximation of floating point numbers, but there is no perfect way to represent all floating point numbers in binary.

    Some discrepancies are easier to see than others. For instance:

    console.log(0.1 + 0.2); // "0.30000000000000004"
    

    There are some JavaScript libraries out there that do the “decimal” thing a’la C#’s decimal type or Java’s BigDecimal. That’s where the number is actually stored as a series of decimal digits. But they’re not a panacea, they just have a different class of problems (try to represent 1 / 3 accurately with it, for instance). “Decimal” types/libraries are fantastic for financial applications, because we’re used to dealing with the style of rounding required in financial stuff, but there is the cost that they tend to be slower than IEEE floating point.

    Let’s output your x and y values:

    var x = .6 - .5;
    console.log(x); // "0.09999999999999998"
    
    var y = 10.2 - 10.1;
    console.log(y); // "0.09999999999999964"
    

    No great surprise that 0.09999999999999998 is != to 0.09999999999999964. 🙂

    You can rationalize those a bit to make the comparison work:

    function roundTwoPlaces(num) {
      return Math.round(num * 100) / 100;
    }
    
    var x = roundTwoPlaces(0.6 - 0.5);
    
    var y = roundTwoPlaces(10.2 - 10.1);
    
    console.log(x);       // "0.1"
    console.log(y);       // "0.1"
    console.log(x === y); // "true"
    

    Or a more generalized solution:

    function round(num, places) {
        var mult = Math.pow(10, places);
        return Math.round(num * mult) / mult;
    }
    

    Live example | source

    Note that it’s still possible for accuracy crud to be in the resulting number, but at least two numbers that are very, very, very close to each other, if run through round with the same number of places, should end up being the same number (even if that number isn’t perfectly accurate).

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

Sidebar

Related Questions

Possible Duplicate: Javascript regex returning true.. then false.. then true.. etc var r =
Possible Duplicate: Is JavaScript’s math broken? This seems really stupid, but when running this,
Possible Duplicate: Is JavaScript’s math broken? If the main difference between Javascript's strict comparison
Possible Duplicate: Is JavaScript's Math broken? Strange result with floating point addition Some simple
Possible Duplicate: Is JavaScript's Math broken? <html> <body> <script type=text/javascript> var w = 0;
Possible Duplicate: Is JavaScript's Math broken? when i subtract 6.4-1.6 i am getting the
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} Suppose we
Possible Duplicate: Is JavaScript's Math broken? I have just started playing around with Javascript
Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
Possible Duplicate: Is JavaScript's Math broken? I have fount weird problem in arithmetic precision

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.