# Why Do Computers Suck at Math?

**URL:** <https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178>\
**Category:** blog\
**Created:** [May 13, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178 "2009-05-13T00:00:00Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![Kim](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/k/9f8e36/32.png) [@Kim](https://discourse.codinghorror.com/u/Kim)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/101 "2009-05-14T00:00:00Z")

</div>

Since nobody mentioned Haskell so far: Haskell is another language that handles large numbers and fractions very well. It even has a rational number datatype that represents numbers as the quotient of two integers of arbitrary size.

---

<div class="post-metadata">

**Author:** ![Atario](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/a/97f17d/32.png) [@Atario](https://discourse.codinghorror.com/u/Atario)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/102 "2009-05-14T00:00:00Z")

</div>

@the old [o]rang:

A simple way (very simplified) is to say that 0.99999… is so close to 1.0, that I wish not to have to print all them darned nines, and for my purposes I will round it off to 1.0, so I don’t have to spend all day writing out senseless nines. The precision of avoiding the incredibly small differences is not worth the effort, since each space is 1/10 the size of the previous.

At the risk of enabling a troll, let me correct you.

0.999… is not _pretty close to_ one. It is one. Not by convention, not by habit, not by agreement, not by tradition, not by laziness. The problem here, I think, is that you are ignoring what that … means. It doesn’t mean a whole bunch more, nor an arbitrarily large number of. It means an _infinite_ number of. It means they go on forever. Not till you get tired of writing them, or holding down the 9 key. _For_. _Ever_.

What’s more, your rambling about positive zero and negative zero, about how computers can only add, the rest is just tricks, and computers can’t do anything but binary make me think you’re a representative from the Time Cube organization. Look up bignums and BCD and get back to us. (Or don’t.)

---

<div class="post-metadata">

**Author:** ![MarkT](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/m/c89c15/32.png) [@MarkT](https://discourse.codinghorror.com/u/MarkT)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/103 "2009-05-14T00:00:00Z")

</div>

In general, to the commenters who are saying that Jeff is speaking to the wrong crowd…

It’s good that you had an excellent computer science education. But you seem to be failing to account for the common nature of these issues. Maybe Jeff is saying things that have already been said, but as long as these mistakes keep happening these things bear repeating. At the very top of the article, it’s observed that Google has made this common, easily-corrected error (I say easily-corrected because with Guido van Rossum in their organization somewhere, you’d think they could make the search engine’s math output at least as good as the default handler in Python). If Google’s screwing up something this simple, maybe it’s not as obvious to the average programmer as we think it is?

So perhaps the people who write common and widely-used software should improve their computer science education. And perhaps the people who comment on these articles should ([http://drupal.org/node/29405)](http://drupal.org/node/29405)) help by contributing to successful projects like Drupal.

Maybe computer science prowess and popular, successful programs are semi-independent variables.

And maybe in a world where they are semi-independent, the people who know what they are doing should get up off the backs of the people who try to educate the rest of us.

---

<div class="post-metadata">

**Author:** ![osp70](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/o/c89c15/32.png) [@osp70](https://discourse.codinghorror.com/u/osp70)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/104 "2009-05-14T00:00:00Z")

</div>

The only problem I have with the .99999… = 1 is that give me N number of 9’s after 0.9 and I can give you an infinite number of number between that number and 1. Not necessarily on a computer but in theory.

---

<div class="post-metadata">

**Author:** ![someone](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/b77776/32.png) [@someone](https://discourse.codinghorror.com/u/someone)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/105 "2009-05-14T00:00:00Z")

</div>

You know the old saying … garbage in, garbage out

Don’t blame the poor unknowing computer. It’s just doing what it’s told, and following the rules its given to produce a result.

---

<div class="post-metadata">

**Author:** ![aurelix](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/a/8c91f0/32.png) [@aurelix](https://discourse.codinghorror.com/u/aurelix)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/106 "2009-05-14T00:00:00Z")

</div>

Trickiest computer math gotcha I stumbled upon in reality: modulo repetition. Given a float x, think there’s no difference between x%1 and x%1%1 (% being modulo operator)? Think again:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)  
[GCC 4.3.3] on linux2  
Type help, copyright, credits or license for more information.  
x=-1e-20  
x%1  
1.0  
x%1%1  
0.0

---

<div class="post-metadata">

**Author:** ![DennisD](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/d/e274bd/32.png) [@DennisD](https://discourse.codinghorror.com/u/DennisD)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/107 "2009-05-14T00:00:00Z")

</div>

Jolly good. If you see somebody using floating point for currency representation, they surely don’t know what they’re doing. Beware bad text-books.

Decimal numbers are coming back to computers. There is a modern IEEE-standard for them, and hardware support is said to be coming for this standard. Even the software versions are actually quite fast, but most importantly, they’re correct. There are implementations for C and C++ as well.

Mike Cowlishaw made the programming language REXX which actually uses decimal numbers, and he’s done some important work on decimal numbers and the IEEE-standard. He’s also the man behind the JSR-13 for BigDecimal in Java. Densely Packed Decimal numbers are a variance of Chen-Ho encoded decimal numbers.

Some links on decimal arithmetic on computers for those who are interested:

[http://domino.research.ibm.com/comm/research\_people.nsf/pages/cowlishaw.index.html](http://domino.research.ibm.com/comm/research_people.nsf/pages/cowlishaw.index.html)  
[http://en.wikipedia.org/wiki/Mike\_Cowlishaw](http://en.wikipedia.org/wiki/Mike_Cowlishaw)  
[http://www.intel.com/technology/itj/2007/v11i1/s2-decimal/1-sidebar.htm](http://www.intel.com/technology/itj/2007/v11i1/s2-decimal/1-sidebar.htm)  
[http://en.wikipedia.org/wiki/Densely\_Packed\_Decimal](http://en.wikipedia.org/wiki/Densely_Packed_Decimal)  
[http://speleotrove.com/decimal/](http://speleotrove.com/decimal/)  
[http://speleotrove.com/decimal/DPDecimal.html](http://speleotrove.com/decimal/DPDecimal.html)

Simply put: There is not much excuse for not doing math correctly on computers. I can understand why floating point arithmetic is wanted by Fortran/HPC/Science programmers, who at least for some systems need as much speed as they can get their hands on, but for anything else, decimal arithmetic is the way to go.

Anyway, it’s a bliss to use a language which just does it correctly.

---

<div class="post-metadata">

**Author:** ![jasonmray](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/j/85f322/32.png) [@jasonmray](https://discourse.codinghorror.com/u/jasonmray)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/108 "2009-05-14T00:00:00Z")

</div>

@zokier

So if Java is like a house, then C++ is like a house without a kitchen sink?

Sounds about right to me.. ;)

---

<div class="post-metadata">

**Author:** ![Practicality](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/p/e9bcb4/32.png) [@Practicality](https://discourse.codinghorror.com/u/Practicality)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/109 "2009-05-14T00:00:00Z")

</div>

So…you’ve found a problem. What’s the solution?

---

<div class="post-metadata">

**Author:** ![EuroM](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/e/fbc32d/32.png) [@EuroM](https://discourse.codinghorror.com/u/EuroM)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/110 "2009-05-14T00:00:00Z")

</div>

In mathematics, the repeating decimal 0.999Ö denotes a real number equal to one. In other words: the notations 0.999Ö and 1 actually represent the same real number.  
This equality has long been accepted by professional mathematicians and taught in textbooks.

Really? Glad to know it gets taught; nobody taught that to _ME_. I swear, just a couple of months ago I was randomly thinking about periodic fractions and non-decimal bases (nothing better to think about… maybe that’s why it took me so long to get married :-) ), and I stumbled upon this fact in total bewilderment. It was a clear, unmistakable and inescapable consequence of a few basic mathematical facts. How fun!

To me, the most interesting consequence is that our conventional numeric notation system (even with the use of … or the vinculum sign) is not a biyective representation of the set of Reals, even though for the longest time I had assumed it was.

---

<div class="post-metadata">

**Author:** ![Steve\_W](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/ba9def/32.png) [@Steve\_W](https://discourse.codinghorror.com/u/Steve_W)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/111 "2009-05-14T00:00:00Z")

</div>

@Craig Fritzpatrick

ìSo when we divide 9 by 2 for example, we as people might write: 4 1/2. Simple, a string of 5 characters including the space. No precision problems.î

Good luck when you want to calculate sqrt(2). Seriously, there are many rational classes, but I doubt any store them as strings.

@Jim

ìIt doesn’t help that the real numbers are uncountably infinite, not merely countably infinite like the integers.î … ì Any two different real numbers have an infinite number of other reals between them, so it’s impossible to represent any nonempty segment of the real number line exactly.î

Thatís not just a problem with unaccountably infinite numbers; rationales are countable, but any two rationale numbers have an infinite number of rationales between them. But this isnít really the problem with representing numbers on computers ñ integers have just as much of a problem if they are sufficiently large.

---

<div class="post-metadata">

**Author:** ![Scottie\_T](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/ce73a5/32.png) [@Scottie\_T](https://discourse.codinghorror.com/u/Scottie_T)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/112 "2009-05-14T00:00:00Z")

</div>

Not related to floating point numbers: Stack Overflow has spoiled me. Everything on the internet needs to be able to be up-voted or down-voted. I read this article in Google Reader and for a split second was looking for the up-arrow to click on. Sharing or staring an article just doesn’t feel the same as up-voting.

---

<div class="post-metadata">

**Author:** ![EuroM](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/e/fbc32d/32.png) [@EuroM](https://discourse.codinghorror.com/u/EuroM)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/113 "2009-05-14T00:00:00Z")

</div>

@Daren:  
The correct mathematical explanation is that 0.99999… (zero point nine recurring) approaches 1.

No, actually it has nothing to do with Limits. I know it looks like it, but it’s not.

Here’s a simplified version of what I stumbled upon:

```
1/3 = 0.33333...
2/3 = 0.66666...

```

Nothing weird there. Those two are clearly exactly equivalent. Now:

```
1/3 + 2/3 = 1

```

No possible doubt there. There are no limits or rounding involved. Therefore,

```
0.33333... + 0.66666... = 0.99999...

```

Which means that 0.99999… MUST therefore be an alternative (non-normalized) representation of 1.

---

<div class="post-metadata">

**Author:** ![SergeiK](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/s/c67d28/32.png) [@SergeiK](https://discourse.codinghorror.com/u/SergeiK)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/114 "2009-05-14T00:00:00Z")

</div>

Ruby and Python rules too :) Just like the python:

[ruby]  
~$ irb  
irb(main):001:0 399999999999999-399999999999998  
= 1  
irb(main):002:0 exit

[php]  
~$ php -r ‘echo 399999999999999-399999999999998 . \n;’  
1  
~$

---

<div class="post-metadata">

**Author:** ![Norbert](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/n/f6c823/32.png) [@Norbert](https://discourse.codinghorror.com/u/Norbert)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/115 "2009-05-14T00:00:00Z")

</div>

I got the correct answer from excel 2007 when I tried =850\*77.1

---

<div class="post-metadata">

**Author:** ![Andrew](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/a/f19dbf/32.png) [@Andrew](https://discourse.codinghorror.com/u/Andrew)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/116 "2009-05-14T00:00:00Z")

</div>

The identity, 1 = 0.999…, simply indicates that we can represent the number that we call ‘one’ as two different power series,

0 + 9/10 + 9/100 + 9/1000 + …

and

1 + 0/10 + 0/100 + 0/1000 + …

(This is simply the definition of our decimal expansion notation; if the trailing coefficients of the series are repeating zeros, we omit writing them for convenience.) Put another way, the equation, 1 = 0.999…, just says that two different sequences (the partial sums of the two power series above) converge to one.

---

<div class="post-metadata">

**Author:** ![jul](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/j/41988e/32.png) [@jul](https://discourse.codinghorror.com/u/jul)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/117 "2009-05-14T00:00:00Z")

</div>

Python rulez ?

38.1 \* .198  
7.5438000000000009  
.1  
0.10000000000000001

[http://docs.python.org/tutorial/floatingpoint.html](http://docs.python.org/tutorial/floatingpoint.html)

I had the same problem on an e-commerce website in c#, and in perl/PHP/Tcl/C, and as everybody knows computers are not faulty, only developpers :)

Normaly to avoid problem it is quite better not to work in BCD (binary coded decimal) float (unless your CPU compute natively numbers in decimal :) ), but rather to use fixed point trick.

In real life it means manipulating only intergers (you don’t store price as float, but integer representing tenth of cents for instance) and the point is just a matter of presentation.

It hardly works all the time, but it is better than nothing.

---

<div class="post-metadata">

**Author:** ![darkbagel](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/d/b19c9b/32.png) [@darkbagel](https://discourse.codinghorror.com/u/darkbagel)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/118 "2009-05-14T00:00:00Z")

</div>

Computers don’t suck at math. People simply use floating point variables for purposes that floating point wasn’t designed for.

The technology to do true decimal arithmetic and return precise numbers has existed for more than 50 years, On business platforms, such as the IBM i platform, it’s the default numeric data type. It’s the right choice when you are working with money, weights, quantities and the other precise numbers used in business.

Floating point was designed more for scientific applications or graphical applications. It wasn’t designed for business.

Strangely, most languages for the PC platform are lacking true decimal arithmetic, and the developers aren’t clamoring for it. I’ve never understood that. How can floating point be good enough for your business?

---

<div class="post-metadata">

**Author:** ![ChrisHDog](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/c/34f0e0/32.png) [@ChrisHDog](https://discourse.codinghorror.com/u/ChrisHDog)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/119 "2009-05-14T00:00:00Z")

</div>

The excel bug is very confusing … if you take that 850 \* 77.1 and format the cell as a date you get the same value as if you have 65535 formatted as a date … if you format it pretty much any other way you get the 100000 and 65535

---

<div class="post-metadata">

**Author:** ![Joe](https://discourse.codinghorror.com/letter_avatar_proxy/v2/letter/j/b9e5f3/32.png) [@Joe](https://discourse.codinghorror.com/u/Joe)\
**Post date:** [May 14, 2009, 12:00am UTC](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178/120 "2009-05-14T00:00:00Z")

</div>

But subtraction can be anywhere from exact to completely inaccurate. If two numbers agree to n figures, you can lose up to n figures of precision in their subtraction.

This makes no sense to me, and neither this article nor the linked article attempts to explain it. Why should subtraction be harder than addition, multiplication, or division? I’ve tried thinking about it from various angles, and don’t see why subtraction should introduce this kind of difficulty, and especially why the agreement of the two operands should have an effect on the precision of the results. Can you elaborate?

[Previous page](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178.md?page=5)

[Next page](https://discourse.codinghorror.com/t/why-do-computers-suck-at-math/178.md?page=7)
