GithubHelp home page GithubHelp logo

montana / is-floating-point-math-broken Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 9 KB

This demonstrates JavaScripts possible floating point math bug by showing which two-decimal-place numbers between 0.00 and 1.00 inclusive have fractional parts after being multiplied by one hundred

JavaScript 100.00%

is-floating-point-math-broken's Introduction

Is Floating Point Math Broken?

This was inspired by: https://stackoverflow.com/questions/588004/is-floating-point-math-broken 12 years ago.

Demonstrated is JavaScript's floating point math bugs by showing which two-decimal-place numbers between 0.00 and 1.00 inclusive have fractional parts after being multiplied by one hundred.

The original version of this program used the floating-point value, i, as the loop counter of the for-loop. Apparently, using a floating-point number, along with a loop final-expression that converted the value to a string and back to a float again in an attempt to carefully increment by 0.01, caused Bad Things to happen. E.g., the loop would run for thousands of iterations rather than exactly one hundred, causing the browser to freeze.

The original code:

for (i = 0.00; i <= 1.00; i = +((i + 0.01).toPrecision(2))) {
    // Note the final-expression in the for-loop limits decimal places,
    // overcoming float errors from adding 0.01
    j = i * 100;
    if (Math.round(j) != j) {
        console.log('Multiplcation error:', i, j);
    }
}

Changed in the new version:

  • Added an integer loop counter, n
  • Check for floating point addition errors, and if found
    • Display a message
    • Convert the more-precise number from a string back to a float

The last point may be significant, because it avoids converting the float to a string and back to a float again in every iteration. This conversion is done only when necessary.

Another version of this is, where the variable i is actually a string to avoid bugs in floating point addition, but this has an opportunity cost of plausibly causing more type conversions:

for (n = 0; n <= 100; ++n) {
  with ('00' + n) {
    // i = (n / 100), two decimal places
    i = substr(0, (length-2)) + '.' + substr(-2);
  }
  j = i * 100;
  if (Math.round(j) != j) {
    console.log(n, 'Multiplication error:', i, j);
  }
}

is-floating-point-math-broken's People

Contributors

montana avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.