···11-floating-point.gui.de aims to provide both short and simple
22-answers to the common recurring questions of novice programmers
33-about floating point numbers not "adding up" correctly, and
44-more in-depth information about how IEEE 754 floats work,
55-when and how to use them correctly, and what to use instead
66-when they are not appropriate.
77-88-The site is build using the nanoc static site generator:
99-http://nanoc.stoneship.org/
11+floating-point.gui.de aims to provide both short and simple
22+answers to the common recurring questions of novice programmers
33+about floating point numbers not "adding up" correctly, and
44+more in-depth information about how IEEE 754 floats work,
55+when and how to use them correctly, and what to use instead
66+when they are not appropriate.
77+88+The site is build using the nanoc static site generator:
99+http://nanoc.stoneship.org/
+14
Rules
···1414 # don’t filter or layout
1515end
16161717+compile '/favicon/' do
1818+ # don’t filter or layout
1919+end
2020+compile '/logo/' do
2121+ # don’t filter or layout
2222+end
2323+1724compile '*' do
1825 filter :erb
1926 filter :kramdown
···22292330route '/stylesheet/' do
2431 '/style.css'
3232+end
3333+3434+route '/favicon/' do
3535+ '/favicon.ico'
3636+end
3737+route '/logo/' do
3838+ '/logo.png'
2539end
26402741route '*' do
+5-3
content/basic.html
···11---
22title: Basic Answers
33---
44+<%= @item[:title] %>
55+====================
4657### Why don't my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?
68···14161517It's not stupid, just different. Decimal numbers cannot accurately
1618represent a number like 1/3, so you have to round to 0.33 - and you
1717-don't expect 0.33 + 0.33 + 0.33 to result in 1, either.
1919+don't expect 0.33 + 0.33 + 0.33 to add up to 1, either - do you?
18201921Computers use binary numbers because they're faster at dealing with
2022those, and because for most calculations, a tiny error in the 17th
···23252426### What can I do to avoid this problem?
25272626-That depends on what kind of calculations you're doing:
2828+That depends on what kind of calculations you're doing.
27292828-* If you are doing technical calculations with short numbers and just don't want to end up with all those extra decimal places: simply [round your result](/rounding) to a fixed number of decimal places.
2930* If you really need your results to add up exactly, especially when you work with money: use a special [decimal datatype](/decimal). Look at the "languages" section for details.
3131+* If you just don't want to end up with all those extra decimal places: simply [round your result](/rounding) to a fixed number of decimal places.
3032* If you have no decimal datatype available, an alternative is to work with integers, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.
31333234
content/favicon.ico
This is a binary file and will not be displayed.
+35-3
content/fp.html
···11---
22-title: A New Item
22+title: Floating Point Numbers
33---
4455-
66-How to mess with people who've come to *expect* rounding errors in floating point math.
55+Floating Point Numbers
66+======================
77+88+Binary Fractions
99+----------------
1010+As a programmer, you are familiar with the concept of binary integers, i.e.
1111+the representation of integer numbers as a series of bits:
1212+1313+<table>
1414+<tr><th colspan="9">Decimal (<span class="num_base">base 10</span>)</th><th></th><th colspan="17">Binary (<span class="num_base">base 2</span>)</th></tr>
1515+<tr class="base_example">
1616+<td class="digit">1</td><td>⋅</td><td class="num_base">10<sup>1</sup></td><td>+</td>
1717+<td class="digit">3</td><td>⋅</td><td class="num_base">10<sup>0</sup></td><td>=</td>
1818+<td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td>
1919+<td class="digit">1101<sub class="num_base">2</sub></td><td>=</td>
2020+<td class="digit">1</td><td>⋅</td><td class="num_base">2<sup>3</sup></td><td>+</td>
2121+<td class="digit">1</td><td>⋅</td><td class="num_base">2<sup>2</sup></td><td>+</td>
2222+<td class="digit">0</td><td>⋅</td><td class="num_base">2<sup>1</sup></td><td>+</td>
2323+<td class="digit">1</td><td>⋅</td><td class="num_base">2<sup>0</sup></td>
2424+</tr><tr class="base_example">
2525+<td class="digit">1</td><td>⋅</td><td>10</td><td>+</td>
2626+<td class="digit">3</td><td>⋅</td><td>1 </td><td>=</td>
2727+<td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td>
2828+<td class="digit">1101<sub class="num_base">2</sub></td><td>=</td>
2929+<td class="digit">1</td><td>⋅</td><td>8</td><td>+</td>
3030+<td class="digit">1</td><td>⋅</td><td>4</td><td>+</td>
3131+<td class="digit">0</td><td>⋅</td><td>2</td><td>+</td>
3232+<td class="digit">1</td><td>⋅</td><td>0</td>
3333+</tr></table>
3434+3535+This is how computers store integer numbers internally. And for fractional numbers, they do the same thing:
3636+3737+3838+
+7-2
content/languages/java.html
···12121313Decimal Types
1414-------------
1515-Java has an arbitrary-precision decimal type named <code>BigDecimal</code>, which
1616-also allows to choose the rounding mode.
1515+Java has an arbitrary-precision decimal type named <code>java.math.BigDecimal</code>, which
1616+also allows to choose the rounding mode:
1717+ new BigDecimal("0.1").add(new BigDecimal("0.2")) // returns a BigDecimal representing exactly 0.3
171818191920How to Round
2021------------
2222+To get a String:
2123 String.format("%.2f", 1.2399) // returns "1.24"
2224 String.format("%.3f", 1.2399) // returns "1.240"
2325 String.format("%.2f", 1.2) // returns "1.20"
2626+To print to standard output (or any <code>PrintStream</code>):
2727+ System.out.printf("%.2f", 1.2399) // same syntax as String.format()
2828+If you don't want trailing zeroes:
2429 new DecimalFormat("0.00").format(1.2)// returns "1.20"
2530 new DecimalFormat("0.##").format(1.2)// returns "1.2"
2631
···11+---
22+title: xkcd
33+---
44+55+e<sup>π</sup> - π
66+====================
77+88+
99+1010+[xkcd](http://www.xkcd.com/217/): How to mess with people who've come to *expect* rounding errors in floating point math.