Skip to content
DevMeme
2482 of 7435
The world if floating point math just worked
CS Fundamentals Post #2756, on Feb 15, 2021 in TG

The world if floating point math just worked

Description

A classic 'The World If' meme format, which depicts a gleaming, futuristic, utopian city with sleek, towering skyscrapers and advanced infrastructure. Superimposed over this idyllic scene is white, bold text. The top text reads 'THE WORLD IF', and the bottom text presents a simple mathematical equation: '0.1 + 0.2 = 0.3'. The joke is a deep-cut reference for programmers to the inherent inaccuracies of floating-point arithmetic in most programming languages due to the way decimal numbers are represented in binary (as defined by the IEEE 754 standard). In reality, 0.1 + 0.2 evaluates to a number like 0.30000000000000004, a common source of bugs, especially in financial and scientific applications. The meme humorously suggests that this fundamental, persistent quirk of computing is the primary obstacle preventing humanity from achieving a perfect, utopian society

Comments

24
Anonymous ★ Top Pick The correct way to check if 0.1 + 0.2 equals 0.3 is to see if the absolute difference is less than epsilon. Or, as we do in the real world, multiply by 100 and work with integers
  1. Anonymous ★ Top Pick

    The correct way to check if 0.1 + 0.2 equals 0.3 is to see if the absolute difference is less than epsilon. Or, as we do in the real world, multiply by 100 and work with integers

  2. Anonymous

    In the universe where 0.1 + 0.2 == 0.3, we shipped the fintech stack in one sprint - no BigDecimal, no ε, only the PM’s timelines still overflow

  3. Anonymous

    After 20 years in the industry, I've accepted that we'll achieve cold fusion before JavaScript properly handles decimal arithmetic - at least BigDecimal is finally in Stage 2, only took us three decades to admit that financial calculations shouldn't rely on binary approximations of base-10 numbers

  4. Anonymous

    Ah yes, the IEEE 754 fantasy - where we've finally achieved a civilization advanced enough to represent 0.1 in binary without an infinite series. In this utopia, financial systems don't need BigDecimal, JavaScript developers sleep soundly at night, and 'use an epsilon for comparison' is just an ancient proverb from the before-times. Truly, we'd have colonized Mars by now if only those three bits of mantissa had cooperated

  5. Anonymous

    Dev heaven: exact decimals by default, sparing us epsilon dances in every prod-facing aggregation query

  6. Anonymous

    In a universe where 0.1 + 0.2 === 0.3, I’d finally delete epsilonCompare(), half the flaky tests, and three ADRs debating floats-for-money - Finance would still argue about rounding

  7. Anonymous

    If 0.1 + 0.2 ever equaled 0.3, we’d delete closeEnough(), retire BigDecimal, and stop explaining to Finance why their budget is 0.30000000000000004

  8. @Sokolovskiy01 5y

    But...

  9. @average_meni_na_drugu_enjoyer 5y

    in reality it 0.3000000006?

  10. @RKRohk 5y

    == tho

    1. @JoseAngelSanchez 5y

      True

  11. @mashkachan 5y

    i dont understand this joke

    1. @deerspangle 5y

      Using floating point numbers, addition like this generally leads to a very small error, so it would equal 0.3000000006 instead of 0.3

      1. @mashkachan 5y

        Ok, thanks

    2. @Supuhstar 5y

      If you try this in a programming language which uses normal IEEE floating-point numbers (basically all programming languages except Mathemetica and the like), then: 0.1 + 0.2 = 0.30000000000000004

      1. @mashkachan 5y

        Its can be problem with a super accurate calculations

        1. @Supuhstar 5y

          and not-so-accurate, as this simple one shows

      2. @devotus08 5y

        but why it is in that way?

        1. @Supuhstar 5y

          Because you're thinking in decimal, and the computer is thinking in binary. If you wanted to use only binary fractions, it'd work out better, but not always accurate. This is all due to the format that IEEE floating point numbers are stored in. They're all similar, but let's focus on 32-bit floats. They have one bit for whether it's negative ("sign"), 8 for how big it is (exponent), and 23 for how precise it is (significant, or mantissa, or fraction). In order to calculate what it means and how to make one, you have to use the formula in this picture. If you go through by hand, you'll see that it's impossible to represent 3/10

  12. @GTRst 5y

    JavaScript devs left the chat

  13. @Supuhstar 5y

    OOF

  14. @nuntikov 5y

    Use decimal

    1. @Supuhstar 5y

      Exactly. Never use floating-point primitives for anything but estimation

  15. @energizer91 5y

    https://0.30000000000000004.com

Use J and K for navigation