# Doubles suck!

• Continuing the discussion from Bytes suck!:

Could someone with more computer-arithmetic clout than I, explain why, in BLOCKED SCRIPT<br>
<br>var pulse = 0.8;<br>
var pulseComplement =  1 - pulse;<br>
<br>
causes:<br>
pulseComplement == 0.19999999999999995<br>
<br>
<br>
Why does the thing treat 0.8 as 0.80000000000000005?<br>
<br>
PS.<br>
That's in IE. Firefox produces 0.1999999999999999[b]6[/b]<br>
<br>
How hard is it to store 0.8? When did 8e-1 become irrational? What am I missing?<br>
<br>
PSII<br>
It's a tiny, tiny error, and doesn't cause much trouble in the way of pixel-precision, so the question is largely academic.<br>

Meanwhile, nearly 10 years later...

``````C:\>node
> var pulse=0.8;
undefined
> pulse
0.8
> var pulseComplement =1-pulse;
undefined
> pulseComplement
0.19999999999999996
>

``````

IEEE754 is a harsh mistress...

• In an attempt to be a bit more useful, i would recommend having a look at http://floating-point-gui.de/:

##Why don’t my numbers add up?
So you’ve written some absurdly simple code, say for example:

``````	0.1 + 0.2</pre
</code
and got a really unexpected result:
0.30000000000000004</pre
</code
Maybe you asked for help on some forum and got pointed to a long article with lots of formulas that didn’t seem to help with your problem.
Well, this site is here to:  Explain concisely why you get that unexpected result  Tell you how to deal with this problem  If you’re interested, provide in-depth explanations of why floating-point numbers have to work like that and what other problems can ariseYou should look at the Basic Answers first - but don’t stop there!
``````
``` Filed under: It's really hard to include code blocks inside of quote blocks :fa_frown_o: ... ```

• Why don’t we use decimal floating point already?

• In an attempt to be a bit more useful, i would recommend having a look at http://floating-point-gui.de/:

And this guy did a pretty cool series on floating point stuff with catchy names like There are Only Four Billion Floats—So Test Them All!

Also the canonical What Every Computer Scientist Should Know About Floating-Point Arithmetic.

• The Random ASCII series was the one I had in mind when I went looking for floating point information. I'm going to shamelessly copy your link and present it as my own in a onebox...

(not that it actually oneboxes that well, but whatever, seriously, read the series...)

• I clicked the link and I still don't know if `nbsp;` is portable!

• Hmm, I'm not sure I take anything written by JavaScript's chief apologist seriously.

Besides which, didn't IBM already write a library in a bunch of different languages for doing arbitrary precision decimal mathematics?

• Why don’t we use decimal floating point already?

Because binary float allows larger amount of values.

• Because binary float allows larger amount of values.

And neither binary nor decimal floats can represent ⅓ exactly anyway.

• trinary FP then?

• trinary FP then?

Use the old favourite of base 60.

• If you care about exact representation, why do you even use float?

• If you care about exact representation, why do you even use float?

That's a non-trivial question. How much do you really like exact representations?

• I don't. As long as the result is more or less what I expect, I could<blakeybait> care less about correctness.

Filed under: #gamedev

• Use the old favourite of base 60.

That could work, it's not that far off of base 64...

• If you care about exact representation, why do you even use float?

Rationals are the way to go... (if only C had had a `rat` type...)

• Since 99.9% of the time you want fixed-point for storing money amounts, it'd be better if they had an equivalent to SQL Server's "Money" type. (The other 0.01% of the time you're writing a 3D engine for an old CPU without dedicated floating point hardware. Like a 680LC40 or something.)

• 99.9% of the time ... The other 0.01% of the time

We seem to be missing 0.09%. I'd guess that's the time wasted on WTDWTF, but it's much too small.

• We seem to be missing 0.0899999999999999966693309261245303787291049957275390625%. I'd guess that's the time wasted on WTDWTF, but it's much too small.

FPTFY

Also, don’t you mean “percentage points”?

• What's a “percentage points”?

• [citation needed]

• How about http://en.wikipedia.org/wiki/Percentage_point? Will that do as a citation?

• Will that do as a citation?

You mean the "article [that] does not cite any references or sources," not even for its fundamental definition of the article term?

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.