···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+#!/usr/bin/env ruby
22+33+# A few helpful tips about the Rules file:
44+#
55+# * The order of rules is important: for each item, only the first matching
66+# rule is applied.
77+#
88+# * Item identifiers start and end with a slash (e.g. “/about/” for the file
99+# “content/about.html”). To select all children, grandchildren, … of an
1010+# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
1111+# because “*” matches zero or more characters.
1212+1313+compile '/stylesheet/' do
1414+ # don’t filter or layout
1515+end
1616+1717+compile '*' do
1818+ filter :erb
1919+ filter :kramdown
2020+ layout 'default'
2121+end
2222+2323+route '/stylesheet/' do
2424+ '/style.css'
2525+end
2626+2727+route '*' do
2828+ item.identifier + 'index.html'
2929+end
3030+3131+layout '*', :erb
+41
config.yaml
···11+# A list of file extensions that nanoc will consider to be textual rather than
22+# binary. If an item with an extension not in this list is found, the file
33+# will be considered as binary.
44+text_extensions: [ 'css', 'erb', 'haml', 'htm', 'html', 'js', 'less', 'markdown', 'md', 'php', 'rb', 'sass', 'txt' ]
55+66+# The path to the directory where all generated files will be written to. This
77+# can be an absolute path starting with a slash, but it can also be path
88+# relative to the site directory.
99+output_dir: output
1010+1111+# A list of index filenames, i.e. names of files that will be served by a web
1212+# server when a directory is requested. Usually, index files are named
1313+# “index.hml”, but depending on the web server, this may be something else,
1414+# such as “default.htm”. This list is used by nanoc to generate pretty URLs.
1515+index_filenames: [ 'index.html' ]
1616+1717+# Whether or not to generate a diff of the compiled content when compiling a
1818+# site. The diff will contain the differences between the compiled content
1919+# before and after the last site compilation.
2020+enable_output_diff: false
2121+2222+# The data sources where nanoc loads its data from. This is an array of
2323+# hashes; each array element represents a single data source. By default,
2424+# there is only a single data source that reads data from the “content/” and
2525+# “layout/” directories in the site directory.
2626+data_sources:
2727+ -
2828+ # The type is the identifier of the data source. By default, this will be
2929+ # `filesystem_unified`.
3030+ type: filesystem_unified
3131+3232+ # The path where items should be mounted (comparable to mount points in
3333+ # Unix-like systems). This is “/” by default, meaning that items will have
3434+ # “/” prefixed to their identifiers. If the items root were “/en/”
3535+ # instead, an item at content/about.html would have an identifier of
3636+ # “/en/about/” instead of just “/about/”.
3737+ items_root: /
3838+3939+ # The path where layouts should be mounted. The layouts root behaves the
4040+ # same as the items root, but applies to layouts rather than items.
4141+ layouts_root: /
+40
content/basic.html
···11+---
22+title: Basic Answers
33+---
44+55+### 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?
66+77+Because internally, computers use a format ([binary floating point](/fp)) that
88+cannot accurately represent a number like 0.1, 0.2 or 0.3 *at all*.
99+When the code is compiled or interpreted, your "0.1" is already
1010+rounded to the nearest number in that format, which results
1111+in a slight error even before the calculation happens.
1212+1313+### Why do computers use such a stupid system?
1414+1515+It's not stupid, just different. Decimal numbers cannot accurately
1616+represent 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.
1818+1919+Computers use binary numbers because they're faster at dealing with
2020+those, and because for most calculations, a tiny error in the 17th
2121+decimal place doesn't matter at all since the numbers you work with
2222+aren't round (or that precise) anyway.
2323+2424+### What can I do to avoid this problem?
2525+2626+That depends on what kind of calculations you're doing:
2727+2828+* 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.
2929+* 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.
3030+* 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.
3131+3232+3333+### Why do other calculations like 0.1 + 0.4 work correctly?
3434+3535+In that case, the result (0.5) *can* be represented exactly as a floating point number,
3636+and the errors in the input numbers cancel each other out.
3737+3838+In other cases like 0.1 + 0.3, the result actually isn't *really* 0.4, but close enough that 0.4
3939+is the shortest number that is closer to the result than to any other floating point number. Many languages then display that number instead of converting the actual result back to the closest
4040+decimal fraction.
+5
content/decimal.html
···11+---
22+title: A New Item
33+---
44+55+Hi, I'm a new item!
+6
content/fp.html
···11+---
22+title: A New Item
33+---
44+55+
66+How to mess with people who've come to *expect* rounding errors in floating point math.
+32
content/index.html
···11+---
22+title: Home
33+---
44+55+What Every Programmer Should Know About Floating-Point Arithmetic
66+=================================================================
77+88+or
99+--
1010+1111+Why don't my numbers add up?
1212+============================
1313+1414+So you've written some innocent code, say for example:
1515+1616+<pre>
1717+ <a href="javascript:alert(0.1 + 0.2)">0.1 + 0.2</a>
1818+</pre>
1919+2020+and got a really unexpected result:
2121+2222+<pre>
2323+ 0.30000000000000004
2424+</pre>
2525+2626+You asked for help on some forum and got pointed to a [long article with lots of formulas](http://docs.sun.com/source/806-3568/ncg_goldberg.html) that didn't seem to help with your problem.
2727+2828+Well, this site is here to:
2929+3030+* Explain concisely why you get that unexpected result
3131+* Tell you how to deal with this problem
3232+* If you're interested, provide in-depth explanations of why floating point numbers have to work like that and what other problems can arise
+5
content/languages/csharp.html
···11+---
22+title: A New Item
33+---
44+55+Hi, I'm a new item!
+5
content/languages/java.html
···11+---
22+title: A New Item
33+---
44+55+Hi, I'm a new item!
+5
content/languages/javascript.html
···11+---
22+title: A New Item
33+---
44+55+Hi, I'm a new item!
+5
content/languages/php.html
···11+---
22+title: A New Item
33+---
44+55+Hi, I'm a new item!
+5
content/rounding.html
···11+---
22+title: A New Item
33+---
44+55+Hi, I'm a new item!