Giter Site home page Giter Site logo

naipotato / 0.30000000000000004 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from erikwiffin/0.30000000000000004

0.0 0.0 0.0 195 KB

Floating Point Math Examples

Home Page: https://0.30000000000000004.com/

License: GNU General Public License v3.0

CSS 33.50% HTML 26.52% Ruby 39.98%

0.30000000000000004's Introduction

Floating Point Math

Your language isn't broken, it's doing floating point math. Computers can only natively store integers, so they need some way of representing decimal numbers. This representation has a degree of inaccuracy which is why, more often than not, 0.1 + 0.2 != 0.3.

Why does this happen?

It’s actually rather interesting. When you have a base-10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1 / 2, 1 / 4, 1 / 5, 1 / 8, and 1 / 10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1 / 3, 1 / 6, and 1 / 7 are all repeating decimals because their denominators use a prime factor of 3 or 7.

In binary (or base-2), the only prime factor is 2, so you can only express fractions cleanly which only contain 2 as a prime factor. In binary, 1 / 2, 1 / 4, 1 / 8 would all be expressed cleanly as decimals, while 1 / 5 or 1 / 10 would be repeating decimals. So 0.1 and 0.2 (1 / 10 and 1 / 5), while clean decimals in a base-10 system, are repeating decimals in the base-2 system the computer uses. When you do math on these repeating decimals, you end up with leftovers which carry over when you convert the computer's base-2 (binary) number into a more human-readable base-10 representation.

Below are some examples of sending .1 + .2 to standard output in a variety of languages.

0.30000000000000004's People

Contributors

erikwiffin avatar blieque avatar ash avatar brackendev avatar cowens avatar stefanedwards avatar vendethiel avatar anirbanmu avatar autarch avatar itaisteinherz avatar dependabot[bot] avatar amirtcho avatar braincompiler avatar dkordic avatar ihid avatar still-learning avatar sre42 avatar linuxbckp avatar lazymofo avatar 0racle avatar abrudz avatar aconchillo avatar lengau avatar envek avatar ircmaxell avatar brehberg avatar bjsemrad avatar polymeris avatar sixlettervariables avatar cncolder 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.