Lo que todo programador debería saber sobre aritmética de punto flotante
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

more work

+131 -30
+1 -1
.gitignore
··· 1 - output 1 + output 2 2 tmp
+9 -9
README.txt
··· 1 - floating-point.gui.de aims to provide both short and simple 2 - answers to the common recurring questions of novice programmers 3 - about floating point numbers not "adding up" correctly, and 4 - more in-depth information about how IEEE 754 floats work, 5 - when and how to use them correctly, and what to use instead 6 - when they are not appropriate. 7 - 8 - The site is build using the nanoc static site generator: 9 - http://nanoc.stoneship.org/ 1 + floating-point.gui.de aims to provide both short and simple 2 + answers to the common recurring questions of novice programmers 3 + about floating point numbers not "adding up" correctly, and 4 + more in-depth information about how IEEE 754 floats work, 5 + when and how to use them correctly, and what to use instead 6 + when they are not appropriate. 7 + 8 + The site is build using the nanoc static site generator: 9 + http://nanoc.stoneship.org/
+14
Rules
··· 14 14 # don’t filter or layout 15 15 end 16 16 17 + compile '/favicon/' do 18 + # don’t filter or layout 19 + end 20 + compile '/logo/' do 21 + # don’t filter or layout 22 + end 23 + 17 24 compile '*' do 18 25 filter :erb 19 26 filter :kramdown ··· 22 29 23 30 route '/stylesheet/' do 24 31 '/style.css' 32 + end 33 + 34 + route '/favicon/' do 35 + '/favicon.ico' 36 + end 37 + route '/logo/' do 38 + '/logo.png' 25 39 end 26 40 27 41 route '*' do
+5 -3
content/basic.html
··· 1 1 --- 2 2 title: Basic Answers 3 3 --- 4 + <%= @item[:title] %> 5 + ==================== 4 6 5 7 ### 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? 6 8 ··· 14 16 15 17 It's not stupid, just different. Decimal numbers cannot accurately 16 18 represent a number like 1/3, so you have to round to 0.33 - and you 17 - don't expect 0.33 + 0.33 + 0.33 to result in 1, either. 19 + don't expect 0.33 + 0.33 + 0.33 to add up to 1, either - do you? 18 20 19 21 Computers use binary numbers because they're faster at dealing with 20 22 those, and because for most calculations, a tiny error in the 17th ··· 23 25 24 26 ### What can I do to avoid this problem? 25 27 26 - That depends on what kind of calculations you're doing: 28 + That depends on what kind of calculations you're doing. 27 29 28 - * 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. 29 30 * 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. 31 + * 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. 30 32 * 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. 31 33 32 34
content/favicon.ico

This is a binary file and will not be displayed.

+35 -3
content/fp.html
··· 1 1 --- 2 - title: A New Item 2 + title: Floating Point Numbers 3 3 --- 4 4 5 - ![Obligatory xkcd cartoon](http://imgs.xkcd.com/comics/e_to_the_pi_minus_pi.png "Obligatory xkcd cartoon") 6 - How to mess with people who've come to *expect* rounding errors in floating point math. 5 + Floating Point Numbers 6 + ====================== 7 + 8 + Binary Fractions 9 + ---------------- 10 + As a programmer, you are familiar with the concept of binary integers, i.e. 11 + the representation of integer numbers as a series of bits: 12 + 13 + <table> 14 + <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> 15 + <tr class="base_example"> 16 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">10<sup>1</sup></td><td>+</td> 17 + <td class="digit">3</td><td>&sdot;</td><td class="num_base">10<sup>0</sup></td><td>=</td> 18 + <td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td> 19 + <td class="digit">1101<sub class="num_base">2</sub></td><td>=</td> 20 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>3</sup></td><td>+</td> 21 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>2</sup></td><td>+</td> 22 + <td class="digit">0</td><td>&sdot;</td><td class="num_base">2<sup>1</sup></td><td>+</td> 23 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>0</sup></td> 24 + </tr><tr class="base_example"> 25 + <td class="digit">1</td><td>&sdot;</td><td>10</td><td>+</td> 26 + <td class="digit">3</td><td>&sdot;</td><td>1 </td><td>=</td> 27 + <td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td> 28 + <td class="digit">1101<sub class="num_base">2</sub></td><td>=</td> 29 + <td class="digit">1</td><td>&sdot;</td><td>8</td><td>+</td> 30 + <td class="digit">1</td><td>&sdot;</td><td>4</td><td>+</td> 31 + <td class="digit">0</td><td>&sdot;</td><td>2</td><td>+</td> 32 + <td class="digit">1</td><td>&sdot;</td><td>0</td> 33 + </tr></table> 34 + 35 + This is how computers store integer numbers internally. And for fractional numbers, they do the same thing: 36 + 37 + 38 +
+7 -2
content/languages/java.html
··· 12 12 13 13 Decimal Types 14 14 ------------- 15 - Java has an arbitrary-precision decimal type named <code>BigDecimal</code>, which 16 - also allows to choose the rounding mode. 15 + Java has an arbitrary-precision decimal type named <code>java.math.BigDecimal</code>, which 16 + also allows to choose the rounding mode: 17 + new BigDecimal("0.1").add(new BigDecimal("0.2")) // returns a BigDecimal representing exactly 0.3 17 18 18 19 19 20 How to Round 20 21 ------------ 22 + To get a String: 21 23 String.format("%.2f", 1.2399) // returns "1.24" 22 24 String.format("%.3f", 1.2399) // returns "1.240" 23 25 String.format("%.2f", 1.2) // returns "1.20" 26 + To print to standard output (or any <code>PrintStream</code>): 27 + System.out.printf("%.2f", 1.2399) // same syntax as String.format() 28 + If you don't want trailing zeroes: 24 29 new DecimalFormat("0.00").format(1.2)// returns "1.20" 25 30 new DecimalFormat("0.##").format(1.2)// returns "1.2" 26 31
content/logo.png

This is a binary file and will not be displayed.

+39 -4
content/stylesheet.css
··· 10 10 background: #ffffee; 11 11 } 12 12 13 + table { 14 + border: 1px solid black; 15 + border-collapse: collapse; 16 + } 17 + td{ 18 + border: 1px solid black; 19 + padding: 5px; 20 + } 21 + tr.base_example td{ 22 + border-left:none; 23 + border-right:none; 24 + border-top: 1px solid black; 25 + border-bottom: 1px solid black; 26 + padding: 2px; 27 + vertical-align:bottom; 28 + } 29 + tr.base_example td.separator{ 30 + border: 1px solid black; 31 + padding: 5px; 32 + } 33 + tr.base_example td.digit{ 34 + font-weight:bold; 35 + color: #ff0000 36 + } 37 + th { 38 + border: 1px solid black; 39 + padding: 5px; 40 + background: #fff0f0; 41 + text-align: left; 42 + } 43 + 44 + .num_base{ 45 + color: #0000ff 46 + } 47 + 48 + .num_base sup{ 49 + color: #000000 50 + } 51 + 13 52 #main { 14 53 position: absolute; 15 54 ··· 19 58 20 59 #main h1 { 21 60 font-size: 30px; 22 - font-weight: normal; 23 - 24 61 line-height: 30px; 25 - 26 - letter-spacing: -1px; 27 62 } 28 63 29 64 #main p {
+10
content/xkcd.html
··· 1 + --- 2 + title: xkcd 3 + --- 4 + 5 + e<sup>&pi;</sup> - &pi; 6 + ==================== 7 + 8 + ![Obligatory xkcd cartoon](http://imgs.xkcd.com/comics/e_to_the_pi_minus_pi.png "Obligatory xkcd cartoon") 9 + 10 + [xkcd](http://www.xkcd.com/217/): How to mess with people who've come to *expect* rounding errors in floating point math.
+11 -8
layouts/default.html
··· 4 4 <meta charset="utf-8"> 5 5 <title>The Floating-Point Guide - <%= @item[:title] %></title> 6 6 <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 7 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 7 8 <meta name="generator" content="nanoc 3.1.2"> 8 9 </head> 9 10 <body> ··· 11 12 <%= yield %> 12 13 </div> 13 14 <div id="sidebar"> 15 + <img src="/logo.png"> 14 16 <h2>The Floating-Point Guide</h2> 15 17 <ul> 16 18 <li><a href="/">Home</a></li> 17 - <li><a href="/basic">Basic Answers</a></li> 18 - <li><a href="/fp">Floating Point</a></li> 19 - <li><a href="/decimal">Decimal types</a></li> 20 - <li><a href="/rounding">Rounding</a></li> 19 + <li><a href="/basic/">Basic Answers</a></li> 20 + <li><a href="/fp/">Floating Point</a></li> 21 + <li><a href="/decimal/">Decimal types</a></li> 22 + <li><a href="/rounding/">Rounding</a></li> 23 + <li><a href="/xkcd/">xkcd</a></li> 21 24 </ul> 22 25 <h2>Languages</h2> 23 26 <ul> 24 - <li><a href="/languages/csharp">C#</a></li> 25 - <li><a href="/languages/java">Java</a></li> 26 - <li><a href="/languages/javascript">JavaScript</a></li> 27 - <li><a href="/languages/php">PHP</a></li> 27 + <li><a href="/languages/csharp/">C#</a></li> 28 + <li><a href="/languages/java/">Java</a></li> 29 + <li><a href="/languages/javascript/">JavaScript</a></li> 30 + <li><a href="/languages/php/">PHP</a></li> 28 31 </ul> 29 32 </div> 30 33 </body>