Permalink
Browse files

initial checkin

  • Loading branch information...
0 parents commit c9a3781829055d5ebc3a8fb4c755c9d0f0977f71 @brazzy committed Apr 9, 2010
@@ -0,0 +1,2 @@
+output
+tmp
@@ -0,0 +1,9 @@
+floating-point.gui.de aims to provide both short and simple
+answers to the common recurring questions of novice programmers
+about floating point numbers not "adding up" correctly, and
+more in-depth information about how IEEE 754 floats work,
+when and how to use them correctly, and what to use instead
+when they are not appropriate.
+
+The site is build using the nanoc static site generator:
+http://nanoc.stoneship.org/
@@ -0,0 +1 @@
+require 'nanoc3/tasks'
@@ -0,0 +1,31 @@
+#!/usr/bin/env ruby
+
+# A few helpful tips about the Rules file:
+#
+# * The order of rules is important: for each item, only the first matching
+# rule is applied.
+#
+# * Item identifiers start and end with a slash (e.g. “/about/” for the file
+# “content/about.html”). To select all children, grandchildren, … of an
+# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
+# because “*” matches zero or more characters.
+
+compile '/stylesheet/' do
+ # don’t filter or layout
+end
+
+compile '*' do
+ filter :erb
+ filter :kramdown
+ layout 'default'
+end
+
+route '/stylesheet/' do
+ '/style.css'
+end
+
+route '*' do
+ item.identifier + 'index.html'
+end
+
+layout '*', :erb
@@ -0,0 +1,41 @@
+# A list of file extensions that nanoc will consider to be textual rather than
+# binary. If an item with an extension not in this list is found, the file
+# will be considered as binary.
+text_extensions: [ 'css', 'erb', 'haml', 'htm', 'html', 'js', 'less', 'markdown', 'md', 'php', 'rb', 'sass', 'txt' ]
+
+# The path to the directory where all generated files will be written to. This
+# can be an absolute path starting with a slash, but it can also be path
+# relative to the site directory.
+output_dir: output
+
+# A list of index filenames, i.e. names of files that will be served by a web
+# server when a directory is requested. Usually, index files are named
+# “index.hml”, but depending on the web server, this may be something else,
+# such as “default.htm”. This list is used by nanoc to generate pretty URLs.
+index_filenames: [ 'index.html' ]
+
+# Whether or not to generate a diff of the compiled content when compiling a
+# site. The diff will contain the differences between the compiled content
+# before and after the last site compilation.
+enable_output_diff: false
+
+# The data sources where nanoc loads its data from. This is an array of
+# hashes; each array element represents a single data source. By default,
+# there is only a single data source that reads data from the “content/” and
+# “layout/” directories in the site directory.
+data_sources:
+ -
+ # The type is the identifier of the data source. By default, this will be
+ # `filesystem_unified`.
+ type: filesystem_unified
+
+ # The path where items should be mounted (comparable to mount points in
+ # Unix-like systems). This is “/” by default, meaning that items will have
+ # “/” prefixed to their identifiers. If the items root were “/en/”
+ # instead, an item at content/about.html would have an identifier of
+ # “/en/about/” instead of just “/about/”.
+ items_root: /
+
+ # The path where layouts should be mounted. The layouts root behaves the
+ # same as the items root, but applies to layouts rather than items.
+ layouts_root: /
@@ -0,0 +1,40 @@
+---
+title: Basic Answers
+---
+
+### 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?
+
+Because internally, computers use a format ([binary floating point](/fp)) that
+cannot accurately represent a number like 0.1, 0.2 or 0.3 *at all*.
+When the code is compiled or interpreted, your "0.1" is already
+rounded to the nearest number in that format, which results
+in a slight error even before the calculation happens.
+
+### Why do computers use such a stupid system?
+
+It's not stupid, just different. Decimal numbers cannot accurately
+represent a number like 1/3, so you have to round to 0.33 - and you
+don't expect 0.33 + 0.33 + 0.33 to result in 1, either.
+
+Computers use binary numbers because they're faster at dealing with
+those, and because for most calculations, a tiny error in the 17th
+decimal place doesn't matter at all since the numbers you work with
+aren't round (or that precise) anyway.
+
+### What can I do to avoid this problem?
+
+That depends on what kind of calculations you're doing:
+
+* 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.
+* 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.
+* 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.
+
+
+### Why do other calculations like 0.1 + 0.4 work correctly?
+
+In that case, the result (0.5) *can* be represented exactly as a floating point number,
+and the errors in the input numbers cancel each other out.
+
+In other cases like 0.1 + 0.3, the result actually isn't *really* 0.4, but close enough that 0.4
+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
+decimal fraction.
@@ -0,0 +1,5 @@
+---
+title: A New Item
+---
+
+Hi, I'm a new item!
@@ -0,0 +1,6 @@
+---
+title: A New Item
+---
+
+![Obligatory xkcd cartoon](http://imgs.xkcd.com/comics/e_to_the_pi_minus_pi.png "Obligatory xkcd cartoon")
+How to mess with people who've come to *expect* rounding errors in floating point math.
@@ -0,0 +1,32 @@
+---
+title: Home
+---
+
+What Every Programmer Should Know About Floating-Point Arithmetic
+=================================================================
+
+or
+--
+
+Why don't my numbers add up?
+============================
+
+So you've written some innocent code, say for example:
+
+<pre>
+ <a href="javascript:alert(0.1 + 0.2)">0.1 + 0.2</a>
+</pre>
+
+and got a really unexpected result:
+
+<pre>
+ 0.30000000000000004
+</pre>
+
+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.
+
+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 arise
@@ -0,0 +1,5 @@
+---
+title: A New Item
+---
+
+Hi, I'm a new item!
@@ -0,0 +1,5 @@
+---
+title: A New Item
+---
+
+Hi, I'm a new item!
@@ -0,0 +1,5 @@
+---
+title: A New Item
+---
+
+Hi, I'm a new item!
@@ -0,0 +1,5 @@
+---
+title: A New Item
+---
+
+Hi, I'm a new item!
@@ -0,0 +1,5 @@
+---
+title: A New Item
+---
+
+Hi, I'm a new item!
@@ -0,0 +1,86 @@
+* {
+ margin: 0;
+ padding: 0;
+
+ font-family: Georgia, Palatino, Times, 'Times New Roman', sans-serif;
+}
+
+body {
+ background: #fff;
+}
+
+#main {
+ position: absolute;
+
+ top: 40px;
+ left: 280px;
+
+ width: 700px;
+}
+
+#main h1 {
+ font-size: 30px;
+ font-weight: normal;
+
+ line-height: 30px;
+
+ letter-spacing: -1px;
+}
+
+#main p {
+ margin: 20px 0;
+
+ font-size: 15px;
+
+ line-height: 20px;
+}
+
+#main ul {
+ margin: 20px;
+}
+
+#main li {
+ list-style-type: square;
+
+ font-size: 15px;
+
+ line-height: 20px;
+}
+
+#sidebar {
+ position: absolute;
+
+ top: 40px;
+ left: 20px;
+ width: 200px;
+
+ padding: 20px 20px 0 0;
+
+ border-right: 1px solid #ccc;
+
+ text-align: right;
+}
+
+#sidebar h2 {
+ text-transform: uppercase;
+
+ font-size: 13px;
+
+ color: #333;
+
+ letter-spacing: 1px;
+
+ line-height: 20px;
+}
+
+#sidebar ul {
+ list-style-type: none;
+
+ margin: 20px 0;
+}
+
+#sidebar li {
+ font-size: 14px;
+
+ line-height: 20px;
+}
@@ -0,0 +1,31 @@
+<!DOCTYPE HTML>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>The Floating-Point Guide - <%= @item[:title] %></title>
+ <link rel="stylesheet" type="text/css" href="/style.css" media="screen">
+ <meta name="generator" content="nanoc 3.1.2">
+ </head>
+ <body>
+ <div id="main">
+ <%= yield %>
+ </div>
+ <div id="sidebar">
+ <h2>The Floating-Point Guide</h2>
+ <ul>
+ <li><a href="/">Home</a></li>
+ <li><a href="/basic">Basic Answers</a></li>
+ <li><a href="/fp">Floating Point</a></li>
+ <li><a href="/decimal">Decimal types</a></li>
+ <li><a href="/rounding">Rounding</a></li>
+ </ul>
+ <h2>Languages</h2>
+ <ul>
+ <li><a href="/languages/csharp">C#</a></li>
+ <li><a href="/languages/java">Java</a></li>
+ <li><a href="/languages/javascript">JavaScript</a></li>
+ <li><a href="/languages/php">PHP</a></li>
+ </ul>
+ </div>
+ </body>
+</html>
@@ -0,0 +1,2 @@
+# All files in the 'lib' directory will be loaded
+# before nanoc starts compiling.

0 comments on commit c9a3781

Please sign in to comment.