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
24Comment deleted
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
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
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
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
Dev heaven: exact decimals by default, sparing us epsilon dances in every prod-facing aggregation query
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
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
But... Comment deleted
in reality it 0.3000000006? Comment deleted
== tho Comment deleted
True Comment deleted
i dont understand this joke Comment deleted
Using floating point numbers, addition like this generally leads to a very small error, so it would equal 0.3000000006 instead of 0.3 Comment deleted
Ok, thanks Comment deleted
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 Comment deleted
Its can be problem with a super accurate calculations Comment deleted
and not-so-accurate, as this simple one shows Comment deleted
but why it is in that way? Comment deleted
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 Comment deleted
JavaScript devs left the chat Comment deleted
OOF Comment deleted
Use decimal Comment deleted
Exactly. Never use floating-point primitives for anything but estimation Comment deleted
https://0.30000000000000004.com Comment deleted