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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:19:09+00:00 2026-06-06T23:19:09+00:00

Possible Duplicate: Javascript Math Error: Inexact Floats I have the following code to make

  • 0

Possible Duplicate:
Javascript Math Error: Inexact Floats

I have the following code to make an animation (doIt does the animation but not related).

function recur(max, i){
    console.log("i: " + i);
    if ( i <= 1){
        setTimeout(function(){
            // doIt(max,i);
            recur(max, i + 0.1);                
        },100);
    } else {
        // OK
    }
}
recur(16,0);

However, the i values are not consistent. For the following code the output is (Google Chrome 20):

i: 0 
i: 0.1 
i: 0.2 
i: 0.30000000000000004
i: 0.4 
i: 0.5 
i: 0.6 
i: 0.7 
i: 0.7999999999999999 
i: 0.8999999999999999 
i: 0.9999999999999999 
i: 1.0999999999999999 

Why is this happening? I want 0.3 not a so close number. Unfortunately this happens in every iteration.

  • 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-06T23:19:10+00:00Added an answer on June 6, 2026 at 11:19 pm

    To output 0.3, you can use toFixed and floating point numbers have slight mis-calculations as @Oleksi pointed out.

    console.log((0.1 + 0.2).toFixed(1)); // 0.3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
Possible Duplicate: Graph visualization code in javascript? I have to integrate a graph with
Possible Duplicate: Is JavaScript's Math broken? I have just started playing around with Javascript
Possible Duplicate: Is JavaScript’s math broken? This seems really stupid, but when running this,
Possible Duplicate: JavaScript: Know when an image is fully loaded I have this code:
Possible Duplicate: Is JavaScript's Math broken? I have fount weird problem in arithmetic precision
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: JavaScript post request like a form submit I have a value calculated
Possible Duplicate: JavaScript BlockStatement confusion Why does {10 + '1'} + 10 equal 10?

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.