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.

Primer commit en gh-pages, creado sitio web

+1954 -859
-12
README.txt
··· 1 - http://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 built using the nanoc static site generator: 9 - http://nanoc.stoneship.org/ (Requires the kramdown and adsf gems) 10 - 11 - and published under the Creative Commons Attribution License (BY): 12 - http://creativecommons.org/licenses/by/3.0/
-1
Rakefile
··· 1 - require 'nanoc3/tasks'
-31
Rules
··· 1 - #!/usr/bin/env ruby 2 - 3 - # A few helpful tips about the Rules file: 4 - # 5 - # * The order of rules is important: for each item, only the first matching 6 - # rule is applied. 7 - # 8 - # * Item identifiers start and end with a slash (e.g. “/about/” for the file 9 - # “content/about.html”). To select all children, grandchildren, … of an 10 - # item, use the pattern “/about/*/”; “/about/*” will also select the parent, 11 - # because “*” matches zero or more characters. 12 - 13 - compile '*' do 14 - case item[:extension] 15 - when 'html' 16 - filter :erb 17 - filter :kramdown 18 - layout 'default' 19 - end 20 - end 21 - 22 - route '*' do 23 - case item[:extension] 24 - when 'html' 25 - item.identifier + 'index.html' 26 - else 27 - item.identifier.chop + '.' + item[:extension] 28 - end 29 - end 30 - 31 - layout '*', :erb
+106
basic/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Basic Answers</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Concise answers to common basic questions about floating-point math, like "Why don't my numbers add up?""> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Basic Answers</h1> 18 + <h3 id="why-dont-my-numbers-like-01--02-add-up-to-a-nice-round-03-and-instead-i-get-a-weird-result-like-030000000000000004">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?</h3> 19 + 20 + <p>Because internally, computers use a format (<a href="/formats/binary/">binary</a> <a href="/formats/fp/">floating-point</a>) that 21 + cannot accurately represent a number like 0.1, 0.2 or 0.3 <em>at all</em>.</p> 22 + 23 + <p>When the code is compiled or interpreted, your “0.1” is already 24 + rounded to the nearest number in that format, which results 25 + in a small <a href="/errors/rounding/">rounding error</a> even before the calculation happens.</p> 26 + 27 + <h3 id="why-do-computers-use-such-a-stupid-system">Why do computers use such a stupid system?</h3> 28 + 29 + <p>It’s not stupid, just different. Decimal numbers cannot accurately 30 + represent a number like 1/3, so you have to round to something like 31 + 0.33 - and you don’t expect 0.33 + 0.33 + 0.33 to add up to 1, either - do you?</p> 32 + 33 + <p>Computers use <a href="/formats/binary/">binary numbers</a> because they’re faster at dealing with 34 + those, and because for most calculations, a tiny error in the 17th 35 + decimal place doesn’t matter at all since the numbers you work with 36 + aren’t round (or that precise) anyway.</p> 37 + 38 + <h3 id="what-can-i-do-to-avoid-this-problem">What can I do to avoid this problem?</h3> 39 + 40 + <p>That depends on what kind of calculations you’re doing.</p> 41 + 42 + <ul> 43 + <li>If you really need your results to add up exactly, especially when you work with money: use a special <a href="/formats/exact/">decimal datatype</a>.</li> 44 + <li>If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.</li> 45 + <li>If you have no decimal datatype available, an alternative is to work with <a href="/formats/integer/">integers</a>, e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.</li> 46 + </ul> 47 + 48 + <h3 id="why-do-other-calculations-like-01--04-work-correctly">Why do other calculations like 0.1 + 0.4 work correctly?</h3> 49 + 50 + <p>In that case, the result (0.5) <em>can</em> be represented exactly as a floating-point number, 51 + and it’s possible for rounding errors in the input numbers to cancel each other out - 52 + But that can’t necessarily be relied upon (e.g. when those two numbers 53 + were stored in differently sized floating point representations first, the rounding 54 + errors might not offset each other).</p> 55 + 56 + <p>In other cases like 0.1 + 0.3, the result actually isn’t <em>really</em> 0.4, but close enough that 0.4 57 + 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 58 + decimal fraction.</p> 59 + 60 + 61 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 62 + <div id="license"> 63 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 64 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 65 + </div> 66 + </div> 67 + <div id="sidebar"> 68 + <img src="/logo.png"> 69 + <h2>La Guía de la Coma Flotante</h2> 70 + <ul> 71 + <li><a href="/">Inicio</a></li> 72 + <li><a href="/basic/">Respuestas Básicas</a></li> 73 + <li><a href="/references/">Referencias</a></li> 74 + <li><a href="/xkcd/">xkcd</a></li> 75 + </ul> 76 + 77 + <h2>Formatos Numéricos</h2> 78 + <ul> 79 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 80 + <li><a href="/formats/fp/">Coma Flotante</a></li> 81 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 82 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 83 + </ul> 84 + 85 + <h2>Errores</h2> 86 + <ul> 87 + <li><a href="/errors/rounding/">Redondeo</a></li> 88 + <li><a href="/errors/comparison/">Comparación</a></li> 89 + <li><a href="/errors/propagation/">Propagación</a></li> 90 + </ul> 91 + 92 + <h2>Cheat sheets</h2> 93 + <ul> 94 + <li><a href="/languages/csharp/">C#</a></li> 95 + <li><a href="/languages/java/">Java</a></li> 96 + <li><a href="/languages/javascript/">JavaScript</a></li> 97 + <li><a href="/languages/perl/">Perl</a></li> 98 + <li><a href="/languages/php/">PHP</a></li> 99 + <li><a href="/languages/python/">Python</a></li> 100 + <li><a href="/languages/sql/">SQL</a></li> 101 + </ul> 102 + </div> 103 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 104 + 105 + </body> 106 + </html>
-41
config.yaml
··· 1 - # A list of file extensions that nanoc will consider to be textual rather than 2 - # binary. If an item with an extension not in this list is found, the file 3 - # will be considered as binary. 4 - text_extensions: [ 'css', 'erb', 'haml', 'htm', 'html', 'js', 'less', 'markdown', 'md', 'php', 'rb', 'sass', 'txt' ] 5 - 6 - # The path to the directory where all generated files will be written to. This 7 - # can be an absolute path starting with a slash, but it can also be path 8 - # relative to the site directory. 9 - output_dir: ../output 10 - 11 - # A list of index filenames, i.e. names of files that will be served by a web 12 - # server when a directory is requested. Usually, index files are named 13 - # “index.hml”, but depending on the web server, this may be something else, 14 - # such as “default.htm”. This list is used by nanoc to generate pretty URLs. 15 - index_filenames: [ 'index.html' ] 16 - 17 - # Whether or not to generate a diff of the compiled content when compiling a 18 - # site. The diff will contain the differences between the compiled content 19 - # before and after the last site compilation. 20 - enable_output_diff: false 21 - 22 - # The data sources where nanoc loads its data from. This is an array of 23 - # hashes; each array element represents a single data source. By default, 24 - # there is only a single data source that reads data from the “content/” and 25 - # “layout/” directories in the site directory. 26 - data_sources: 27 - - 28 - # The type is the identifier of the data source. By default, this will be 29 - # `filesystem_unified`. 30 - type: filesystem_unified 31 - 32 - # The path where items should be mounted (comparable to mount points in 33 - # Unix-like systems). This is “/” by default, meaning that items will have 34 - # “/” prefixed to their identifiers. If the items root were “/en/” 35 - # instead, an item at content/about.html would have an identifier of 36 - # “/en/about/” instead of just “/about/”. 37 - items_root: / 38 - 39 - # The path where layouts should be mounted. The layouts root behaves the 40 - # same as the items root, but applies to layouts rather than items. 41 - layouts_root: /
-44
content/basic.html
··· 1 - --- 2 - title: Basic Answers 3 - description: Concise answers to common basic questions about floating-point math, like "Why don't my numbers add up?" 4 - --- 5 - 6 - ### 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? 7 - 8 - Because internally, computers use a format ([binary](/formats/binary/) [floating-point](/formats/fp/)) that 9 - cannot accurately represent a number like 0.1, 0.2 or 0.3 *at all*. 10 - 11 - When the code is compiled or interpreted, your "0.1" is already 12 - rounded to the nearest number in that format, which results 13 - in a small [rounding error](/errors/rounding/) even before the calculation happens. 14 - 15 - ### Why do computers use such a stupid system? 16 - 17 - It's not stupid, just different. Decimal numbers cannot accurately 18 - represent a number like 1/3, so you have to round to something like 19 - 0.33 - and you don't expect 0.33 + 0.33 + 0.33 to add up to 1, either - do you? 20 - 21 - Computers use [binary numbers](/formats/binary/) because they're faster at dealing with 22 - those, and because for most calculations, a tiny error in the 17th 23 - decimal place doesn't matter at all since the numbers you work with 24 - aren't round (or that precise) anyway. 25 - 26 - ### What can I do to avoid this problem? 27 - 28 - That depends on what kind of calculations you're doing. 29 - 30 - * If you really need your results to add up exactly, especially when you work with money: use a special [decimal datatype](/formats/exact/). 31 - * If you just don't want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it. 32 - * If you have no decimal datatype available, an alternative is to work with [integers](/formats/integer/), e.g. do money calculations entirely in cents. But this is more work and has some drawbacks. 33 - 34 - ### Why do other calculations like 0.1 + 0.4 work correctly? 35 - 36 - In that case, the result (0.5) *can* be represented exactly as a floating-point number, 37 - and it's possible for rounding errors in the input numbers to cancel each other out - 38 - But that can't necessarily be relied upon (e.g. when those two numbers 39 - were stored in differently sized floating point representations first, the rounding 40 - errors might not offset each other). 41 - 42 - In other cases like 0.1 + 0.3, the result actually isn't *really* 0.4, but close enough that 0.4 43 - 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 44 - decimal fraction.
content/errors/NearlyEqualsTest.java errors/NearlyEqualsTest.java
-70
content/errors/comparison.html
··· 1 - --- 2 - title: Comparison 3 - description: Explanation of the various pitfalls in comparing floating-point numbers. 4 - --- 5 - 6 - Due to rounding errors, most [floating-point](/formats/fp/) numbers end up being slightly imprecise. As long as this 7 - imprecision stays small, it can usually be ignored. However, it also means that numbers expected 8 - to be equal (e.g. when calculating the same result through different correct methods) often differ 9 - slightly, and a simple equality test fails. For example: 10 - 11 - float a = 0.15 + 0.15 12 - float b = 0.1 + 0.2 13 - if(a == b) // can be false! 14 - if(a >= b) // can also be false! 15 - 16 - Don't use absolute error margins 17 - -------------------------------- 18 - The solution is to check not whether the numbers are exactly the same, but whether their difference is 19 - very small. The error margin that the difference is compared to is often called *epsilon*. 20 - The most simple form: 21 - 22 - if( Math.abs(a-b) < 0.00001) // wrong - don't do this 23 - 24 - This is a bad way to do it because a fixed epsilon chosen because it "looks small" could actually be way too 25 - large when the numbers being compared are very small as well. The comparison would return "true" for numbers that are quite different. And when the numbers are very large, the epsilon 26 - could end up being smaller than the smallest rounding error, so that the comparison always returns "false". 27 - Therefore, it is necessary to see whether the *relative error* is smaller than epsilon: 28 - 29 - if( Math.abs((a-b)/b) < 0.00001 ) // still not right! 30 - 31 - Look out for edge cases 32 - ----------------------- 33 - There are some important special cases where this will fail: 34 - 35 - * When both `a` and `b` are zero. `0.0/0.0` is "not a number", which causes an exception on some platforms, or returns false for all comparisons. 36 - * When only `b` is zero, the division yields "infinity", which may also cause an exception, or is greater than epsilon even when `a` is smaller. 37 - * It returns `false` when both `a` and `b` are very small but on opposite sides of zero, even when they're the smallest possible non-zero numbers. 38 - 39 - Also, the result is not commutative (`nearlyEquals(a,b)` is not always the same as `nearlyEquals(b,a)`). To fix these problems, the code has to get a lot more complex, so we really need to put it into a function of its own: 40 - 41 - public static boolean nearlyEqual(float a, float b, float epsilon) 42 - { 43 - final float absA = Math.abs(a); 44 - final float absB = Math.abs(b); 45 - final float diff = Math.abs(a - b); 46 - 47 - if (a == b) { // shortcut, handles infinities 48 - return true; 49 - } else if (a * b == 0) { // a or b or both are zero 50 - // relative error is not meaningful here 51 - return diff < (epsilon * epsilon); 52 - } else { // use relative error 53 - return diff / (absA + absB) < epsilon; 54 - } 55 - } 56 - 57 - This method [passes tests](../NearlyEqualsTest.java) for many important special cases, but as you can see, it 58 - uses some quite non-obvious logic. In particular, it has to use a completely different definition of error margin 59 - when `a` or `b` is zero, because the classical definition of relative error becomes meaningless in those cases. 60 - 61 - There are some cases where the method above still produces unexpected results (in particular, it's much stricter when one value is nearly zero than when it is exactly zero), and some of the tests 62 - it was developed to pass probably specify behaviour that is not appropriate for some applications. Before using it, make sure it's appropriate for your application! 63 - 64 - Comparing floating-point values as integers 65 - ------------------------------------------- 66 - There is an alternative to heaping conceptual complexity onto such an apparently simple task: instead of comparing `a` and `b` as [real numbers](http://en.wikipedia.org/wiki/Real_numbers), we can think about them as discrete steps and define the error margin as the maximum number of possible floating-point values between the two values. 67 - 68 - This is conceptually very clear and easy and has the advantage of implicitly scaling the relative error margin with the magnitude of the values. Technically, it's a bit more complex, but not as much as you might think, because IEEE 754 floats are designed to maintain their order when their bit patterns are interpreted as integers. 69 - 70 - However, this method does require the programming language to support conversion between floating-point values and integer bit patterns. Read the [Comparing floating-point numbers](/references/) paper for more details.
-29
content/errors/propagation.html
··· 1 - --- 2 - title: Error Propagation 3 - description: Explanations about propagation of errors in floating-point math. 4 - --- 5 - 6 - While the errors in single [floating-point numbers](/formats/fp/) are very small, even simple calculations on them 7 - can contain pitfalls that increase the error in the result way beyond just having the individual 8 - errors "add up". 9 - 10 - In general: 11 - 12 - * Multiplication and division are "safe" operations 13 - * Addition and subtraction are dangerous, because when numbers of different magnitudes are involved, 14 - digits of the smaller-magnitude number are lost. 15 - * This loss of digits can be inevitable and benign (when the lost digits also insignificant for 16 - the final result) or catastrophic (when the loss is magnified and distorts the result strongly). 17 - * The more calculations are done (especially when they form an iterative algorithm) the more important 18 - it is to consider this kind of problem. 19 - * A method of calculation can be *stable* (meaning that it tends to reduce rounding errors) 20 - or *unstable* (meaning that rounding errors are magnified). Very often, there are both stable 21 - and unstable solutions for a problem. 22 - 23 - There is an entire sub-field of mathematics (in [numerical analysis](http://en.wikipedia.org/wiki/Numerical_analysis)) devoted to studying the numerical stability 24 - of algorithms. For doing complex calculations involving floating-point numbers, it is absolutely 25 - necessary to have some understanding of this discipline. 26 - 27 - The article [What Every Computer Scientist Should Know About Floating-Point Arithmetic](/references/) gives a detailed introduction, 28 - and served as an inspiration for creating this website, mainly due to being a bit too detailed and 29 - intimidating to programmers without a scientific background.
-58
content/errors/rounding.html
··· 1 - --- 2 - title: Rounding Errors 3 - description: Explanation of the reasons for rounding errors in floating-point math, and of rounding modes. 4 - --- 5 - 6 - Because [floating-point numbers](/formats/fp/) have a limited number of digits, they cannot represent all 7 - [real numbers](http://en.wikipedia.org/wiki/Real_number) accurately: when there 8 - are more digits than the format allows, the leftover ones are omitted - the number is 9 - *rounded*. There are three reasons why this can be necessary: 10 - 11 - * **Large Denominators** 12 - In any base, the larger the denominator of an (irreducible) fraction, the more digits it needs in 13 - positional notation. A sufficiently large denominator will require rounding, no 14 - matter what the base or number of available digits is. For example, 1/1000 15 - cannot be accurately represented in less than 3 decimal digits, nor can any 16 - multiple of it (that does not allow simplifying the fraction). 17 - * **Periodical digits** 18 - Any (irreducible) fraction where the denominator has a prime factor that does not occur in the base 19 - requires an infinite number of digits that repeat periodically after a certain point. 20 - For example, in decimal 1/4, 3/5 and 8/20 are finite, because 2 and 21 - 5 are the prime factors of 10. But 1/3 is not finite, nor is 2/3 or 1/7 or 5/6, because 3 22 - and 7 are not factors of 10. Fractions with a prime factor of 5 in the denominator 23 - can be finite in base 10, but [not in base 2](/formats/binary/) - the biggest source of confusion for most 24 - novice users of floating-point numbers. 25 - * **Non-rational numbers** 26 - Non-rational numbers cannot be represented as a regular fraction at all, and in 27 - positional notation (no matter what base) they require an infinite number of non-recurring digits. 28 - 29 - Rounding modes 30 - -------------- 31 - There are different methods to do rounding, and this can be very important in programming, 32 - because rounding can cause different problems in various contexts that can be addressed by 33 - using a better rounding mode. The most common rounding modes are: 34 - 35 - * **Rounding towards zero** - simply truncate the extra digits. The 36 - simplest method, but it introduces larger errors than necessary as well 37 - as a bias towards zero when dealing with mainly positive or mainly 38 - negative numbers. 39 - * **Rounding half away from zero** - if the truncated fraction is greater than or equal to half the base, 40 - increase the last remaining digit. This is the method generally taught in school and used by most 41 - people. It minimizes errors, but also introduces a bias (away from zero). 42 - * **Rounding half to even** also known as **banker's rounding** - if the truncated fraction is 43 - greater than half the base, 44 - increase the last remaining digit. If it is equal to half the base, increase the digit only 45 - if that produces an even result. This minimizes errors and bias, and is therefore preferred for bookkeeping. 46 - 47 - Examples in base 10: 48 - 49 - | | Towards zero | Half away from zero | Half to even | 50 - |------|--------------|---------------------|--------------| 51 - | 1.4 | 1 | 1 | 1 | 52 - | 1.5 | 1 | 2 | 2 | 53 - | -1.6 | -1 | -2 | -2 | 54 - | 2.6 | 2 | 3 | 3 | 55 - | 2.5 | 2 | 3 | 2 | 56 - | -2.4 | -2 | -2 | -2 | 57 - 58 - More [rounding methods](http://en.wikipedia.org/wiki/Rounding) can be found at Wikipedia.
content/favicon.ico favicon.ico
-86
content/formats/binary.html
··· 1 - --- 2 - title: Binary Fractions 3 - description: In-depth explanation of how binary fractions work, what problems the cause and why they are used anyway 4 - --- 5 - 6 - How they work 7 - ------------- 8 - As a programmer, you should be familiar with the concept of binary integers, i.e. 9 - the representation of integer numbers as a series of bits: 10 - 11 - <table> 12 - <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> 13 - <tr class="base_example"> 14 - <td class="digit">1</td><td>&sdot;</td><td class="num_base">10<sup>1</sup></td><td>+</td> 15 - <td class="digit">3</td><td>&sdot;</td><td class="num_base">10<sup>0</sup></td><td>=</td> 16 - <td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td> 17 - <td class="digit">1101<sub class="num_base">2</sub></td><td>=</td> 18 - <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>3</sup></td><td>+</td> 19 - <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>2</sup></td><td>+</td> 20 - <td class="digit">0</td><td>&sdot;</td><td class="num_base">2<sup>1</sup></td><td>+</td> 21 - <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>0</sup></td> 22 - </tr><tr class="base_example"> 23 - <td class="digit">1</td><td>&sdot;</td><td>10</td><td>+</td> 24 - <td class="digit">3</td><td>&sdot;</td><td>1 </td><td>=</td> 25 - <td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td> 26 - <td class="digit">1101<sub class="num_base">2</sub></td><td>=</td> 27 - <td class="digit">1</td><td>&sdot;</td><td>8</td><td>+</td> 28 - <td class="digit">1</td><td>&sdot;</td><td>4</td><td>+</td> 29 - <td class="digit">0</td><td>&sdot;</td><td>2</td><td>+</td> 30 - <td class="digit">1</td><td>&sdot;</td><td>1</td> 31 - </tr></table> 32 - 33 - This is how computers store integer numbers internally. And for fractional numbers in [positional notation](http://en.wikipedia.org/wiki/Positional_notation), they do the same thing: 34 - 35 - <table> 36 - <tr><th colspan="13">Decimal (<span class="num_base">base 10</span>)</th><th> </th><th colspan="13">Binary (<span class="num_base">base 2</span>)</th></tr> 37 - <tr class="base_example"> 38 - <td class="digit">6</td><td>&sdot;</td><td class="num_base">10<sup>-1</sup></td><td>+</td> 39 - <td class="digit">2</td><td>&sdot;</td><td class="num_base">10<sup>-2</sup></td><td>+</td> 40 - <td class="digit">5</td><td>&sdot;</td><td class="num_base">10<sup>-3</sup></td><td>=</td> 41 - <td class="digit">0.625<sub class="num_base">10</sub></td><td class="separator">=</td> 42 - <td class="digit">0.101<sub class="num_base">2</sub></td><td>=</td> 43 - <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>-1</sup></td><td>+</td> 44 - <td class="digit">0</td><td>&sdot;</td><td class="num_base">2<sup>-2</sup></td><td>+</td> 45 - <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>-3</sup></td> 46 - </tr><tr class="base_example"> 47 - <td class="digit">6</td><td>&sdot;</td><td>1/10</td><td>+</td> 48 - <td class="digit">2</td><td>&sdot;</td><td>1/100</td><td>+</td> 49 - <td class="digit">5</td><td>&sdot;</td><td>1/1000</td><td>=</td> 50 - <td class="digit">0.625<sub class="num_base">10</sub></td><td class="separator">=</td> 51 - <td class="digit">0.101<sub class="num_base">2</sub></td><td>=</td> 52 - <td class="digit">1</td><td>&sdot;</td><td>1/2</td><td>+</td> 53 - <td class="digit">0</td><td>&sdot;</td><td>1/4</td><td>+</td> 54 - <td class="digit">1</td><td>&sdot;</td><td>1/8</td> 55 - </tr></table> 56 - 57 - Problems 58 - -------- 59 - While they work the same in principle, binary fractions are different from decimal fractions in what 60 - numbers they can accurately represent with a given number of digits, and thus also in what numbers result in [rounding errors](/errors/rounding/): 61 - 62 - Specifically, binary can only represent those numbers as a finite fraction where the denominator 63 - is a power of 2. Unfortunately, this does not include most of the numbers that can be 64 - represented as finite fraction in base 10, like 0.1. 65 - 66 - | Fraction | Base | Positional Notation | Rounded to 4 digits| Rounded value as fraction | Rounding error | 67 - |-|-|-|-| 68 - | 1/10 | 10 | 0.1 | 0.1 | 1/10 | 0 | 69 - | 1/3 | 10 | 0.<span class="over">3</span> | 0.3333 | 3333/10000 | 1/30000 | 70 - | 1/2 | 2 | 0.1 | 0.1 | 1/2 | 0 | 71 - | 1/10 | 2 | 0.0<span class="over">0011</span> | 0.0001 | 1/16 | 3/80 | 72 - 73 - And this is how you already get a rounding error when you just *write down* a number like 0.1 and 74 - run it through your interpreter or compiler. It's not as big as 3/80 and may be invisible because 75 - computers cut off after 23 or 52 binary digits rather than 4. But the error is there and *will* cause 76 - problems eventually if you just ignore it. 77 - 78 - 79 - Why use Binary? 80 - --------------- 81 - At the lowest level, computers are based on billions of electrical elements that have only two states, (usually low and high voltage). By interpreting these as 0 and 1, it's very easy to build circuits for storing binary numbers and doing calculations with them. 82 - 83 - While it's possible to simulate the behaviour of decimal numbers with binary circuits as well, it's less efficient. If computers used decimal numbers internally, they'd have less memory and be slower at the same level of technology. 84 - 85 - Since the difference in behaviour between binary and decimal numbers is not important for most applications, the logical choice is to build computers based on binary numbers and live with the fact 86 - that some extra care and effort are necessary for applications that require [decimal-like behaviour](/formats/exact/).
-36
content/formats/exact.html
··· 1 - --- 2 - title: Exact Types 3 - description: Description of various datatypes that can be more exact that floating-point numbers 4 - --- 5 - 6 - While [binary](/formats/binary/) [floating-point](/formats/fp/) numbers are better for computers to work with, and usually good enough for humans, sometimes they are just not appropriate. Sometimes, the numbers really must add up to the last bit, and no technical excuses are acceptable - usually when the calculations involve money. 7 - 8 - Unfortunately, there is no dominating standard like IEEE 754 for this (The 2008 version of the standard added decimal types, which is too recent to have seen widespread adoption). 9 - Each language or platform has its own solution, sometimes multiple different ones. For details, look at the language cheat sheets. 10 - 11 - There are at least three fundamentally different kinds of such types: 12 - 13 - Limited-Precision Decimal 14 - ------------------------- 15 - Basically the same as a IEEE 754 binary floating-point, except that the exponent is interpreted as base 10. As a result, there are no unexpected [rounding errors](/errors/rounding/). Also, this kind of format is relatively compact and fast, but usually slower than binary formats. 16 - 17 - Arbitrary-Precision Decimal 18 - --------------------------- 19 - Sometimes called "bignum", this is similar to a limited-precision type, but has the ability to increase the length of the significand (possibly also the exponent) as required. The downside is that there is some basic overhead (memory and speed) to support this flexibility, and that the longer the significand gets, the more 20 - memory is needed and the slower all calculations become. 21 - 22 - It can be very tempting to say "My calculation is important, so I need as much precision as possible", but in practice the actual importance of precision at the 10,000th decimal digit quickly pales in comparison with the 23 - performance penalty required to support it. 24 - 25 - Symbolic calculations 26 - --------------------- 27 - The "holy grail" of exact calculations. Achieved by writing a program that actually knows all the rules of math and represents data as *symbols* rather than imprecise, rounded numbers. For example: 28 - 29 - * 1/3 is actually a fraction "one divided by three" 30 - * The square root of 2 is really the number that, multiplied by itself, is *exactly* 2 31 - * Even [transcendental numbers](http://en.wikipedia.org/wiki/Transcendental_numbers) like **e** and **&pi;** are known, together with their properties, so that e<sup>i&pi;</sup> is *exactly* equal to -1. 32 - 33 - However, these symbolic math systems are complex, slow and require significant mathematical knowledge to use. 34 - They are invaluable tools for mathematicians, but not appropriate for most everyday programming tasks. And 35 - even many mathematicians work on problems where imprecise, numerical solutions are better because no 36 - symbolic solution is known.
-60
content/formats/fp.html
··· 1 - --- 2 - title: Floating Point Numbers 3 - description: Explanation of how floating-points numbers work and what they are good for 4 - --- 5 - 6 - Why floating-point numbers are needed 7 - ------------------------------------- 8 - 9 - Since computer memory is limited, you cannot store numbers with infinite precision, no matter whether you use [binary fractions](/formats/binary/) or decimal ones: at some point you have to cut off. But how much accuracy is needed? And *where* is it needed? How many integer digits and how many fraction digits? 10 - 11 - * To an engineer building a highway, it does not matter whether it's 10 meters or 10.0001 meters wide - his measurements are probably not that accurate in the first place. 12 - * To someone designing a microchip, 0.0001 meters (a tenth of a millimeter) is a *huge* difference - But he'll never have to deal with a distance larger than 0.1 meters. 13 - * A physicist needs to use the [speed of light](http://en.wikipedia.org/wiki/Speed_of_light) (about 300000000) and [Newton's gravitational constant](http://en.wikipedia.org/wiki/Gravitational_constant) (about 0.0000000000667) together in the same calculation. 14 - 15 - To satisfy the engineer and the chip designer, a number format has to provide accuracy for numbers at very different magnitudes. However, only *relative* accuracy is needed. To satisfy the physicist, it must be possible to do calculations that involve numbers with different magnitudes. 16 - 17 - Basically, having a fixed number of integer and fractional digits is not useful - and the solution is a format with a *floating point*. 18 - 19 - How floating-point numbers work 20 - ------------------------------- 21 - The idea is to compose a number of two main parts: 22 - 23 - * A **significand** that contains the number's digits. Negative significands represent negative numbers. 24 - * An **exponent** that says where the decimal (or binary) point is placed relative to the beginning of the significand. Negative exponents represent numbers that are very small (i.e. close to zero). 25 - 26 - Such a format satisfies all the requirements: 27 - 28 - * It can represent numbers at wildly different magnitudes (limited by the length of the exponent) 29 - * It provides the same relative accuracy at all magnitudes (limited by the length of the significand) 30 - * It allows calculations across magnitudes: multiplying a very large and a very small number preserves the accuracy of both in the result. 31 - 32 - Decimal floating-point numbers usually take the form of [scientific notation](http://en.wikipedia.org/wiki/Scientific_notation) with an 33 - explicit point always between the 1st and 2nd digits. The exponent is 34 - either written explicitly including the base, or an **e** is used to 35 - separate it from the significand. 36 - 37 - | Significand | Exponent | Scientific notation | Fixed-point value | 38 - |-------------|----------|---------------------|-------------------| 39 - | 1.5 | 4 | 1.5 &sdot; 10<sup>4</sup> | 15000 | 40 - | -2.001 | 2 | -2.001 &sdot; 10<sup>2</sup> | -200.1 | 41 - | 5 | -3 | 5 &sdot; 10<sup>-3</sup> | 0,005 | 42 - | 6.667 | -11 | 6.667e-11 | 0.0000000000667 | 43 - 44 - The standard 45 - ------------ 46 - Nearly all hardware and programming languages use floating-point numbers in the same binary formats, which are defined in the [IEEE 754](http://en.wikipedia.org/wiki/IEEE_754-2008) standard. The usual formats are 32 or 64 bits in total length: 47 - 48 - | Format | Total bits | Significand bits | Exponent bits | Smallest number | Largest number | 49 - |--------|------------|------------------|---------------|-----------------|----------------| 50 - | Single precision | 32 | 23 + 1 sign | 8 | ca. 1.2 &sdot; 10<sup>-38</sup> | ca. 3.4 &sdot; 10<sup>38</sup>| 51 - | Double precision | 64 | 52 + 1 sign | 11 | ca. 5.0 &sdot; 10<sup>-324</sup> | ca. 1.8 &sdot; 10<sup>308</sup> | 52 - 53 - Note that there are some peculiarities: 54 - 55 - * The **actual bit sequence** is the sign bit first, followed by the exponent and finally the significand bits. 56 - * The exponent does not have a sign; instead an **exponent bias** is subtracted from it (127 for single and 1023 for double precision). This, and the bit sequence, allows floating-point numbers to be compared and sorted correctly even when interpreting them as integers. 57 - * The significand's most significant bit is assumed to be 1 and omitted, except for special cases. 58 - * There are separate **positive and a negative zero** values, differing in the sign bit, where all other bits are 0. These must be considered equal even though their bit patterns are different. 59 - * There are special **positive and negative infinity** values, where the exponent is all 1-bits and the significand is all 0-bits. These are the results of calculations where the positive range of the exponent is exceeded, or division of a regular number by zero. 60 - * There are special **not a number** (or NaN) values where the exponent is all 1-bits and the significand is *not* all 0-bits. These represent the result of various undefined calculations (like multiplying 0 and infinity, any calculation involving a NaN value, or application-specific cases). Even bit-identical NaN values must *not* be considered equal.
-14
content/formats/integer.html
··· 1 - --- 2 - title: On Using Integers 3 - description: Explanation why using integers to avoid floating-point problems by having them represent e.g. cents is not a good solution. 4 - --- 5 - 6 - While integer types are usually binary and by definition do not support fractions, they are exact (no [rounding errors](/errors/rounding/) when converting from decimal integers) and can be used as a sort of "poor man's [decimal type](/formats/exact/)" by choosing an implicit fixed decimal point so that the smallest unit you work with can be represented as the integer 1. In practical terms, this is often put as: **"To handle money, store and calculate everything in cents and format only the output"**. 7 - 8 - This works, but has a number of **severe drawbacks**: 9 - 10 - * It's more work (and more opportunity for bugs) to get it right, especially in regard to [rounding modes](/errors/rounding/). 11 - * Integers have complete precision, but very limited range, and when they overflow, they usually "wrap around" silently, i.e. the largest integer plus 1 becomes zero (for unsigned ints) or the negative value with the largest magnitude (for signed). This is just about the worst possible behaviour when dealing with money, for obvious reasons. 12 - * The implicit decimal point is hard to change and extremely inflexible: if you store dollars as cents, it's simply impossible to support the [Bahraini dinar](http://en.wikipedia.org/wiki/Bahraini_dinar)(1 dinar = 1,000 Fils) at the same time. You'd have to store the position of the decimal point with the data - the first step in implementing your own (buggy, non-standard) limited-precision decimal [floating-point](/formats/fp/) format. 13 - 14 - Summary: **using integers is not recommended.** Do this only if there really is no [better alternative](/formats/exact/) at all.
-28
content/index.html
··· 1 - --- 2 - title: Lo Que Todo Programador Debería Saber Sobre Aritmética de Coma Flotante 3 - description: Pretende dar respuestas cortas y sencillas a las preguntas recurrentes de programadores principiantes sobre números de coma flotante que «no se suman» correctamente, e información más detallada sobre cómo funcionan los números decimales del IEEE 754, cuándo y cómo usarlos correctamente, y qué usar en su lugar cuando no son apropiados. 4 - --- 5 - 6 - o 7 - - 8 - 9 - ¿Por qué mis números no se suman bien? 10 - ====================================== 11 - 12 - O sea que has escrito algún código absurdamente simple, como por ejemplo: 13 - 14 - 0.1 + 0.2 15 - 16 - y has obtenido un resultado totalmente inesperado: 17 - 18 - 0.30000000000000004 19 - 20 - Tal vez pediste ayuda en algún foro y te mandaron a un [artículo largo con un montón de fórmulas](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) que no parecía ser de ayuda. 21 - 22 - Bueno, este sitio está aquí para: 23 - 24 - * Explicar de manera concisa por qué obtuviste ese resultado inesperado 25 - * Decirte cómo lidiar con este problema 26 - * Si te interesa, dar explicaciones detalladas de por qué los números de coma flotante tienen que funcionar así y qué otros problemas pueden surgir 27 - 28 - Deberías ir a la sección de [Respuestas Básicas](/basic/) primero - ¡pero no termines ahí!
-38
content/languages/csharp.html
··· 1 - --- 2 - title: Floating-point cheat sheet for C# 3 - description: Tips for using floating-point and decimal numbers in C# 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - C# has [IEEE 754](/formats/fp/) single and double precision types supported by keywords: 9 - 10 - float f = 0.1f; // 32 bit float, note f suffix 11 - double d = 0.1d; // 64 bit float, suffix optional 12 - 13 - 14 - Decimal Types 15 - ------------- 16 - C# has a 128 bit [limited-precision](/formats/exact/) decimal type denoted by the keyword 17 - <code>decimal</code>: 18 - 19 - decimal myMoney = 300.1m; // note m suffix on the literal 20 - 21 - 22 - How to Round 23 - ------------ 24 - The <code>Math.Round()</code> method works with the double and decimal types, and allows you to specify a [rounding mode](/errors/rounding/): 25 - 26 - Math.Round(1.25m, 1, MidpointRounding.AwayFromZero); // returns 1.3 27 - 28 - 29 - 30 - Resources 31 - --------- 32 - * [C# Reference](http://msdn.microsoft.com/en-us/library/618ayhy6%28v=VS.80%29.aspx) 33 - * [float type](http://msdn.microsoft.com/en-us/library/b1e65aza%28v=VS.80%29.aspx) 34 - * [double type](http://msdn.microsoft.com/en-us/library/678hzkk9%28v=VS.80%29.aspx) 35 - * [decimal type](http://msdn.microsoft.com/en-us/library/364x0z75%28v=VS.80%29.aspx) 36 - * [Math.Round()](http://msdn.microsoft.com/en-US/library/system.math.round%28v=VS.80%29.aspx) 37 - 38 -
-54
content/languages/java.html
··· 1 - --- 2 - title: Floating-point cheat sheet for Java 3 - description: Tips for using floating-point and decimal numbers in Java 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - Java has [IEEE 754](/formats/fp/) single and double precision types supported by keywords: 9 - 10 - float f = 0.1f; // 32 bit float, note f suffix 11 - double d = 0.1d; // 64 bit float, suffix optional 12 - 13 - The `strictfp` keyword on classes, interfaces and methods forces all intermediate results of floating-point calculations to be IEEE 754 values as well, guaranteeing identical results on all platforms. Without that keyword, implementations can use an extended exponent range where available, resulting in more precise results and faster execution on many common CPUs. 14 - 15 - Decimal Types 16 - ------------- 17 - Java has an [arbitrary-precision](/formats/exact/) decimal type named <code>java.math.BigDecimal</code>, which 18 - also allows to choose the [rounding mode](/errors/rounding/). 19 - 20 - BigDecimal a = new BigDecimal("0.1"); 21 - BigDecimal b = new BigDecimal("0.2"); 22 - BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3 23 - 24 - 25 - How to Round 26 - ------------ 27 - To get a String: 28 - 29 - String.format("%.2f", 1.2399) // returns "1.24" 30 - String.format("%.3f", 1.2399) // returns "1.240" 31 - String.format("%.2f", 1.2) // returns "1.20" 32 - 33 - To print to standard output (or any <code>PrintStream</code>): 34 - 35 - System.out.printf("%.2f", 1.2399) // same syntax as String.format() 36 - 37 - If you don't want trailing zeroes: 38 - 39 - new DecimalFormat("0.00").format(1.2)// returns "1.20" 40 - new DecimalFormat("0.##").format(1.2)// returns "1.2" 41 - 42 - If you need a specific [rounding mode](/errors/rounding/): 43 - 44 - new BigDecimal("1.25").setScale(1, RoundingMode.HALF_EVEN); // returns 1.2 45 - 46 - 47 - Resources 48 - --------- 49 - * [Java Language Specification](http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html) 50 - * [Floating-Point Types, Formats, and Values](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3) 51 - * [Java Standard API](http://java.sun.com/javase/6/docs/api/) 52 - * [BigDecimal](http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html) 53 - * [DecimalFormat](http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html) 54 - * [String.format()](http://download.oracle.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...))
-42
content/languages/javascript.html
··· 1 - --- 2 - title: Floating-point cheat sheet for JavaScript 3 - description: Tips for using floating-point and decimal numbers in JavaScript 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - JavaScript is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are IEEE 64 bit values). To force a variable to floating-point, use the global <code>parseFloat()</code> function. 9 - 10 - var num = parseFloat("3.5"); 11 - 12 - Decimal Types 13 - ------------- 14 - 15 - The best decimal type for JavaScript seems to be a port of [Java's](/languages/java/) <code>BigDecimal</code> class, which also supports [rounding modes](/errors/rounding/): 16 - 17 - var a = new BigDecimal("0.01"); 18 - var b = new BigDecimal("0.02"); 19 - var c = a.add(b); // 0.03 20 - var d = c.setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 21 - 22 - 23 - How to Round 24 - ------------ 25 - 26 - var num = 5.123456; 27 - num.toPrecision(1) //returns 5 as string 28 - num.toPrecision(2) //returns 5.1 as string 29 - num.toPrecision(4) //returns 5.123 as string 30 - 31 - Using a specific rounding mode: 32 - 33 - new BigDecimal("1.25").setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 34 - 35 - 36 - Resources 37 - --------- 38 - * [BigDecimal for JavaScript](https://github.com/dtrebbien/BigDecimal.js) 39 - * [Core JavaScript Reference](https://developer.mozilla.org/en/JavaScript/Reference) 40 - * [parseFloat()](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat) 41 - * [toPrecision()](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toPrecision) 42 -
-60
content/languages/perl.html
··· 1 - --- 2 - title: Floating-point cheat sheet for Perl 3 - description: Tips for using floating-point and decimal numbers in Perl 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - Perl supports platform-native floating-point as scalar values; in practice this usually means [IEEE 754](/formats/fp/) double precision. 9 - 10 - Exact Types 11 - ------------- 12 - Perl can also store decimal numbers as strings, but the builtin arithmetic operators will convert them to integer or floating-point values to perform the operation. 13 - 14 - The <code>Math::BigFloat</code> extension provides an arbitrary-precision [decimal type](/formats/exact/): 15 - 16 - use Math::BigFloat ':constant' 17 - my $f = 0.1 + 0.2; # returns exactly 0.3 18 - 19 - The <code>Number::Fraction</code> extension provides a fraction type that overloads the arithmetic operators with [symbolic](/formats/exact/) fraction arithmetic: 20 - 21 - use Number::Fraction ':constants'; 22 - my $f = '1/2' - '1/3'; # returns 1/6 23 - 24 - The <code>Math::BigRat</code> extension provides similar functionality. Its advantage is compatibility with the 25 - <code>Math::BigInt</code> and <code>Math::BigFloat</code> extensions, but it does not seem to support fraction literals. 26 - 27 - How to Round 28 - ------------ 29 - To get a string: 30 - 31 - $result = sprintf("%.2f", 1.2345); # returns 1.23 32 - 33 - To format output: 34 - 35 - printf("%.2f", 1.2); # prints 1.20 36 - 37 - Note that this implicitly uses [round-to-even](/errors/rounding/). The variable <code>$#</code> contains the default format for printing numbers, but its use is considered deprecated. 38 - 39 - The <code>Math::Round</code> extension provides various functions for rounding floating-point values: 40 - 41 - use Math::Round qw(:all); 42 - $result = nearest(.1, 4.567) # prints 4.6 43 - $result = nearest(.01, 4.567) # prints 4.57 44 - 45 - The <code>Math::BigFloat</code> extension also supports various [rounding modes](/errors/rounding/): 46 - 47 - use Math::BigFloat; 48 - my $n = Math::BigFloat->new(123.455); 49 - my $f1 = $n->round('','-2','common'); # returns 123.46 50 - my $f2 = $n->round('','-2','zero'); # returns 123.45 51 - 52 - Resources 53 - --------- 54 - * [Semantics of numbers and numeric operations in Perl](http://perldoc.perl.org/perlnumber.html) 55 - * [sprintf function](http://perldoc.perl.org/functions/sprintf.html) 56 - * [Math::Round extension](http://search.cpan.org/dist/Math-Round/Round.pm) 57 - * [Number::Fraction extension](http://search.cpan.org/~davecross/Number-Fraction-1.13/lib/Number/Fraction.pm) 58 - * [Math::BigRat extension](http://search.cpan.org/~flora/Math-BigRat-0.26/lib/Math/BigRat.pm) 59 - * [Math::BigFloat extension](http://search.cpan.org/~flora/Math-BigInt-1.95/lib/Math/BigFloat.pm) 60 -
-35
content/languages/php.html
··· 1 - --- 2 - title: Floating-point cheat sheet for PHP 3 - description: Tips for using floating-point and decimal numbers in PHP 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - PHP is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are platform-dependant, but typically IEEE 64 bit values). To force a value to floating-point, evaluate it in a numerical context: 9 - 10 - $foo = 0 + "10.5"; 11 - 12 - 13 - Decimal Types 14 - ------------- 15 - The BC Math extension implements [arbitrary-precision](/formats/exact/) decimal math: 16 - 17 - $a = '0.1'; 18 - $b = '0.2'; 19 - echo bcadd($a, $b); // prints 0.3 20 - 21 - How to Round 22 - ------------ 23 - Rounding can be done with the `number_format()` function: 24 - 25 - $number = 4.123; 26 - echo number_format($number, 2); // prints 4.12 27 - 28 - 29 - Resources 30 - --------- 31 - * [PHP manual](http://www.php.net/manual/en/index.php) 32 - * [Floating point types](http://www.php.net/manual/en/language.types.float.php) 33 - * [BC Math extension](http://de3.php.net/manual/en/ref.bc.php) 34 - * [number_format()](http://de3.php.net/manual/en/function.number-format.php) 35 -
-42
content/languages/python.html
··· 1 - --- 2 - title: Floating-point cheat sheet for Python 3 - description: Tips for using floating-point and decimal numbers in Python 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - Almost all platforms map Python floats to [IEEE 754](/formats/fp/) 9 - double precision. 10 - 11 - f = 0.1 12 - 13 - Decimal Types 14 - ------------- 15 - Python has an [arbitrary-precision](/formats/exact/) decimal type named <code>Decimal</code> in the <code>decimal</code> module, which also allows to choose the [rounding mode](/errors/rounding/). 16 - 17 - a = Decimal('0.1') 18 - b = Decimal('0.2') 19 - c = a + b # returns a Decimal representing exactly 0.3 20 - 21 - How to Round 22 - ------------ 23 - To get a string: 24 - 25 - "%.2f" % 1.2399 # returns "1.24" 26 - "%.3f" % 1.2399 # returns "1.240" 27 - "%.2f" % 1.2 # returns "1.20" 28 - 29 - To print to standard output: 30 - 31 - print "%.2f" % 1.2399 # just use print and string formatting 32 - 33 - Specific [rounding modes](/errors/rounding/) and other parameters can be defined in a Context object: 34 - 35 - getcontext().prec = 7 36 - 37 - Resources 38 - --------- 39 - * [Floating Point Arithmetic: Issues and Limitations](http://docs.python.org/tutorial/floatingpoint.html) 40 - * [The decimal module](http://docs.python.org/library/decimal.html) 41 - * [Context objects](http://docs.python.org/library/decimal.html#context-objects) 42 - * [String formatting in Python](http://docs.python.org/library/stdtypes.html#string-formatting-operations)
-39
content/languages/sql.html
··· 1 - --- 2 - title: Floating-point cheat sheet for SQL 3 - description: Tips for using floating-point and decimal numbers in SQL 4 - --- 5 - 6 - Floating-Point Types 7 - -------- 8 - The SQL standard defines three binary floating-point types: 9 - 10 - * `REAL` has implementation-dependant precision (usually maps to a hardware-supported type like IEEE 754 single or double precision) 11 - * `DOUBLE PRECISION` has implementation-dependant precision which is greater than `REAL` (usually maps to IEEE 754 double precision) 12 - * `FLOAT(N)` has at least `N` binary digits of precision, with an implementation-dependant maximum for `N` 13 - 14 - The exponent range for all three types is implementation-dependant as well. 15 - 16 - Decimal Types 17 - ------------- 18 - The standard defines two fixed-point decimal types: 19 - 20 - * `NUMERIC(M,N)` has exactly `M` total digits, `N` of them after the decimal point 21 - * `DECIMAL(M,N)` is the same as `NUMERIC(M,N)`, except that it is allowed to have more than `M` total digits 22 - 23 - The maximum values of `M` and `M` are implementation-dependant. Vendors often implement the two types identically. 24 - 25 - How to Round 26 - ------------ 27 - 28 - The SQL standard defines no explicit rounding, but most vendors provide a `ROUND()` or `TRUNC()` function. 29 - 30 - However, it usually makes little sense to round within the database, since its job is *storing* data, while rounding is an aspect of *displaying* data, and should therefore be done by the code in the presentation layer. 31 - 32 - 33 - Resources 34 - --------- 35 - * [Official ISO SQL 2008 standard (non-free)](http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=38640) 36 - * [SQL 92 draft (free)](http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt) 37 - * [MySQL numeric types](http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html) 38 - * [PostgreSQL data types](http://www.postgresql.org/docs/8.1/static/datatype.html) 39 - * [MS SQL Server data types](http://msdn.microsoft.com/en-US/library/ms187752%28v=SQL.90%29.aspx)
content/logo.png logo.png
-14
content/references.html
··· 1 - --- 2 - title: References 3 - description: Documents with more in-depth information about floating-point math 4 - --- 5 - 6 - Documents that contain more in-depth information about the topics 7 - covered on this wbesite: 8 - 9 - * [Homepage of the IEEE 754 standard](http://grouper.ieee.org/groups/754/) 10 - * [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) 11 - * [Homepage of William Kahan (architect of the IEEE 754 standard, lots of interesting links)](http://www.cs.berkeley.edu/~wkahan/) 12 - * [Decimal Arithmetic FAQ ](http://speleotrove.com/decimal/decifaq.html) 13 - * [Comparing floating-point numbers](http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm) 14 - * [Tool to convert numbers between bases, including fractions](http://www.easysurf.cc/cnver17.htm)
content/robots.txt robots.txt
content/style.css style.css
-14
content/xkcd.html
··· 1 - --- 2 - title: xkcd 3 - description: How to mess with people who've learned to *expect* rounding errors in floating-point math. 4 - --- 5 - 6 - or 7 - -- 8 - 9 - <%= @item[:description] %> 10 - ==================== 11 - 12 - ![Obligatory xkcd cartoon](http://imgs.xkcd.com/comics/e_to_the_pi_minus_pi.png "Obligatory xkcd cartoon") 13 - 14 - From [xkcd](http://www.xkcd.com/217/)
+133
errors/comparison/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Comparison</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Explanation of the various pitfalls in comparing floating-point numbers."> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Comparison</h1> 18 + <p>Due to rounding errors, most <a href="/formats/fp/">floating-point</a> numbers end up being slightly imprecise. As long as this 19 + imprecision stays small, it can usually be ignored. However, it also means that numbers expected 20 + to be equal (e.g. when calculating the same result through different correct methods) often differ 21 + slightly, and a simple equality test fails. For example:</p> 22 + 23 + <pre><code> float a = 0.15 + 0.15 24 + float b = 0.1 + 0.2 25 + if(a == b) // can be false! 26 + if(a &gt;= b) // can also be false! 27 + </code></pre> 28 + 29 + <h2 id="dont-use-absolute-error-margins">Don’t use absolute error margins</h2> 30 + <p>The solution is to check not whether the numbers are exactly the same, but whether their difference is 31 + very small. The error margin that the difference is compared to is often called <em>epsilon</em>. 32 + The most simple form:</p> 33 + 34 + <pre><code> if( Math.abs(a-b) &lt; 0.00001) // wrong - don't do this 35 + </code></pre> 36 + 37 + <p>This is a bad way to do it because a fixed epsilon chosen because it “looks small” could actually be way too 38 + large when the numbers being compared are very small as well. The comparison would return “true” for numbers that are quite different. And when the numbers are very large, the epsilon 39 + could end up being smaller than the smallest rounding error, so that the comparison always returns “false”. 40 + Therefore, it is necessary to see whether the <em>relative error</em> is smaller than epsilon:</p> 41 + 42 + <pre><code> if( Math.abs((a-b)/b) &lt; 0.00001 ) // still not right! 43 + </code></pre> 44 + 45 + <h2 id="look-out-for-edge-cases">Look out for edge cases</h2> 46 + <p>There are some important special cases where this will fail: </p> 47 + 48 + <ul> 49 + <li>When both <code>a</code> and <code>b</code> are zero. <code>0.0/0.0</code> is “not a number”, which causes an exception on some platforms, or returns false for all comparisons. </li> 50 + <li>When only <code>b</code> is zero, the division yields “infinity”, which may also cause an exception, or is greater than epsilon even when <code>a</code> is smaller. </li> 51 + <li>It returns <code>false</code> when both <code>a</code> and <code>b</code> are very small but on opposite sides of zero, even when they’re the smallest possible non-zero numbers.</li> 52 + </ul> 53 + 54 + <p>Also, the result is not commutative (<code>nearlyEquals(a,b)</code> is not always the same as <code>nearlyEquals(b,a)</code>). To fix these problems, the code has to get a lot more complex, so we really need to put it into a function of its own:</p> 55 + 56 + <pre><code> public static boolean nearlyEqual(float a, float b, float epsilon) 57 + { 58 + final float absA = Math.abs(a); 59 + final float absB = Math.abs(b); 60 + final float diff = Math.abs(a - b); 61 + 62 + if (a == b) { // shortcut, handles infinities 63 + return true; 64 + } else if (a * b == 0) { // a or b or both are zero 65 + // relative error is not meaningful here 66 + return diff &lt; (epsilon * epsilon); 67 + } else { // use relative error 68 + return diff / (absA + absB) &lt; epsilon; 69 + } 70 + } 71 + </code></pre> 72 + 73 + <p>This method <a href="../NearlyEqualsTest.java">passes tests</a> for many important special cases, but as you can see, it 74 + uses some quite non-obvious logic. In particular, it has to use a completely different definition of error margin 75 + when <code>a</code> or <code>b</code> is zero, because the classical definition of relative error becomes meaningless in those cases.</p> 76 + 77 + <p>There are some cases where the method above still produces unexpected results (in particular, it’s much stricter when one value is nearly zero than when it is exactly zero), and some of the tests 78 + it was developed to pass probably specify behaviour that is not appropriate for some applications. Before using it, make sure it’s appropriate for your application!</p> 79 + 80 + <h2 id="comparing-floating-point-values-as-integers">Comparing floating-point values as integers</h2> 81 + <p>There is an alternative to heaping conceptual complexity onto such an apparently simple task: instead of comparing <code>a</code> and <code>b</code> as <a href="http://en.wikipedia.org/wiki/Real_numbers">real numbers</a>, we can think about them as discrete steps and define the error margin as the maximum number of possible floating-point values between the two values. </p> 82 + 83 + <p>This is conceptually very clear and easy and has the advantage of implicitly scaling the relative error margin with the magnitude of the values. Technically, it’s a bit more complex, but not as much as you might think, because IEEE 754 floats are designed to maintain their order when their bit patterns are interpreted as integers.</p> 84 + 85 + <p>However, this method does require the programming language to support conversion between floating-point values and integer bit patterns. Read the <a href="/references/">Comparing floating-point numbers</a> paper for more details.</p> 86 + 87 + 88 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 89 + <div id="license"> 90 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 91 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 92 + </div> 93 + </div> 94 + <div id="sidebar"> 95 + <img src="/logo.png"> 96 + <h2>La Guía de la Coma Flotante</h2> 97 + <ul> 98 + <li><a href="/">Inicio</a></li> 99 + <li><a href="/basic/">Respuestas Básicas</a></li> 100 + <li><a href="/references/">Referencias</a></li> 101 + <li><a href="/xkcd/">xkcd</a></li> 102 + </ul> 103 + 104 + <h2>Formatos Numéricos</h2> 105 + <ul> 106 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 107 + <li><a href="/formats/fp/">Coma Flotante</a></li> 108 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 109 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 110 + </ul> 111 + 112 + <h2>Errores</h2> 113 + <ul> 114 + <li><a href="/errors/rounding/">Redondeo</a></li> 115 + <li><a href="/errors/comparison/">Comparación</a></li> 116 + <li><a href="/errors/propagation/">Propagación</a></li> 117 + </ul> 118 + 119 + <h2>Cheat sheets</h2> 120 + <ul> 121 + <li><a href="/languages/csharp/">C#</a></li> 122 + <li><a href="/languages/java/">Java</a></li> 123 + <li><a href="/languages/javascript/">JavaScript</a></li> 124 + <li><a href="/languages/perl/">Perl</a></li> 125 + <li><a href="/languages/php/">PHP</a></li> 126 + <li><a href="/languages/python/">Python</a></li> 127 + <li><a href="/languages/sql/">SQL</a></li> 128 + </ul> 129 + </div> 130 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 131 + 132 + </body> 133 + </html>
+91
errors/propagation/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Error Propagation</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Explanations about propagation of errors in floating-point math."> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Error Propagation</h1> 18 + <p>While the errors in single <a href="/formats/fp/">floating-point numbers</a> are very small, even simple calculations on them 19 + can contain pitfalls that increase the error in the result way beyond just having the individual 20 + errors “add up”.</p> 21 + 22 + <p>In general:</p> 23 + 24 + <ul> 25 + <li>Multiplication and division are “safe” operations</li> 26 + <li>Addition and subtraction are dangerous, because when numbers of different magnitudes are involved, 27 + digits of the smaller-magnitude number are lost.</li> 28 + <li>This loss of digits can be inevitable and benign (when the lost digits also insignificant for 29 + the final result) or catastrophic (when the loss is magnified and distorts the result strongly).</li> 30 + <li>The more calculations are done (especially when they form an iterative algorithm) the more important 31 + it is to consider this kind of problem.</li> 32 + <li>A method of calculation can be <em>stable</em> (meaning that it tends to reduce rounding errors) 33 + or <em>unstable</em> (meaning that rounding errors are magnified). Very often, there are both stable 34 + and unstable solutions for a problem.</li> 35 + </ul> 36 + 37 + <p>There is an entire sub-field of mathematics (in <a href="http://en.wikipedia.org/wiki/Numerical_analysis">numerical analysis</a>) devoted to studying the numerical stability 38 + of algorithms. For doing complex calculations involving floating-point numbers, it is absolutely 39 + necessary to have some understanding of this discipline.</p> 40 + 41 + <p>The article <a href="/references/">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a> gives a detailed introduction, 42 + and served as an inspiration for creating this website, mainly due to being a bit too detailed and 43 + intimidating to programmers without a scientific background.</p> 44 + 45 + 46 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 47 + <div id="license"> 48 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 49 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 50 + </div> 51 + </div> 52 + <div id="sidebar"> 53 + <img src="/logo.png"> 54 + <h2>La Guía de la Coma Flotante</h2> 55 + <ul> 56 + <li><a href="/">Inicio</a></li> 57 + <li><a href="/basic/">Respuestas Básicas</a></li> 58 + <li><a href="/references/">Referencias</a></li> 59 + <li><a href="/xkcd/">xkcd</a></li> 60 + </ul> 61 + 62 + <h2>Formatos Numéricos</h2> 63 + <ul> 64 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 65 + <li><a href="/formats/fp/">Coma Flotante</a></li> 66 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 67 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 68 + </ul> 69 + 70 + <h2>Errores</h2> 71 + <ul> 72 + <li><a href="/errors/rounding/">Redondeo</a></li> 73 + <li><a href="/errors/comparison/">Comparación</a></li> 74 + <li><a href="/errors/propagation/">Propagación</a></li> 75 + </ul> 76 + 77 + <h2>Cheat sheets</h2> 78 + <ul> 79 + <li><a href="/languages/csharp/">C#</a></li> 80 + <li><a href="/languages/java/">Java</a></li> 81 + <li><a href="/languages/javascript/">JavaScript</a></li> 82 + <li><a href="/languages/perl/">Perl</a></li> 83 + <li><a href="/languages/php/">PHP</a></li> 84 + <li><a href="/languages/python/">Python</a></li> 85 + <li><a href="/languages/sql/">SQL</a></li> 86 + </ul> 87 + </div> 88 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 89 + 90 + </body> 91 + </html>
+161
errors/rounding/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Rounding Errors</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Explanation of the reasons for rounding errors in floating-point math, and of rounding modes."> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Rounding Errors</h1> 18 + <p>Because <a href="/formats/fp/">floating-point numbers</a> have a limited number of digits, they cannot represent all 19 + <a href="http://en.wikipedia.org/wiki/Real_number">real numbers</a> accurately: when there 20 + are more digits than the format allows, the leftover ones are omitted - the number is 21 + <em>rounded</em>. There are three reasons why this can be necessary:</p> 22 + 23 + <ul> 24 + <li><strong>Large Denominators</strong> 25 + In any base, the larger the denominator of an (irreducible) fraction, the more digits it needs in 26 + positional notation. A sufficiently large denominator will require rounding, no 27 + matter what the base or number of available digits is. For example, 1/1000 28 + cannot be accurately represented in less than 3 decimal digits, nor can any 29 + multiple of it (that does not allow simplifying the fraction).</li> 30 + <li><strong>Periodical digits</strong> 31 + Any (irreducible) fraction where the denominator has a prime factor that does not occur in the base 32 + requires an infinite number of digits that repeat periodically after a certain point. 33 + For example, in decimal 1/4, 3/5 and 8/20 are finite, because 2 and 34 + 5 are the prime factors of 10. But 1/3 is not finite, nor is 2/3 or 1/7 or 5/6, because 3 35 + and 7 are not factors of 10. Fractions with a prime factor of 5 in the denominator 36 + can be finite in base 10, but <a href="/formats/binary/">not in base 2</a> - the biggest source of confusion for most 37 + novice users of floating-point numbers.</li> 38 + <li><strong>Non-rational numbers</strong> 39 + Non-rational numbers cannot be represented as a regular fraction at all, and in 40 + positional notation (no matter what base) they require an infinite number of non-recurring digits.</li> 41 + </ul> 42 + 43 + <h2 id="rounding-modes">Rounding modes</h2> 44 + <p>There are different methods to do rounding, and this can be very important in programming, 45 + because rounding can cause different problems in various contexts that can be addressed by 46 + using a better rounding mode. The most common rounding modes are:</p> 47 + 48 + <ul> 49 + <li><strong>Rounding towards zero</strong> - simply truncate the extra digits. The 50 + simplest method, but it introduces larger errors than necessary as well 51 + as a bias towards zero when dealing with mainly positive or mainly 52 + negative numbers.</li> 53 + <li><strong>Rounding half away from zero</strong> - if the truncated fraction is greater than or equal to half the base, 54 + increase the last remaining digit. This is the method generally taught in school and used by most 55 + people. It minimizes errors, but also introduces a bias (away from zero).</li> 56 + <li><strong>Rounding half to even</strong> also known as <strong>banker’s rounding</strong> - if the truncated fraction is 57 + greater than half the base, 58 + increase the last remaining digit. If it is equal to half the base, increase the digit only 59 + if that produces an even result. This minimizes errors and bias, and is therefore preferred for bookkeeping.</li> 60 + </ul> 61 + 62 + <p>Examples in base 10:</p> 63 + 64 + <table> 65 + <thead> 66 + <tr> 67 + <th> </th> 68 + <th>Towards zero</th> 69 + <th>Half away from zero</th> 70 + <th>Half to even</th> 71 + </tr> 72 + </thead> 73 + <tbody> 74 + <tr> 75 + <td>1.4</td> 76 + <td>1</td> 77 + <td>1</td> 78 + <td>1</td> 79 + </tr> 80 + <tr> 81 + <td>1.5</td> 82 + <td>1</td> 83 + <td>2</td> 84 + <td>2</td> 85 + </tr> 86 + <tr> 87 + <td>-1.6</td> 88 + <td>-1</td> 89 + <td>-2</td> 90 + <td>-2</td> 91 + </tr> 92 + <tr> 93 + <td>2.6</td> 94 + <td>2</td> 95 + <td>3</td> 96 + <td>3</td> 97 + </tr> 98 + <tr> 99 + <td>2.5</td> 100 + <td>2</td> 101 + <td>3</td> 102 + <td>2</td> 103 + </tr> 104 + <tr> 105 + <td>-2.4</td> 106 + <td>-2</td> 107 + <td>-2</td> 108 + <td>-2</td> 109 + </tr> 110 + </tbody> 111 + </table> 112 + 113 + <p>More <a href="http://en.wikipedia.org/wiki/Rounding">rounding methods</a> can be found at Wikipedia.</p> 114 + 115 + 116 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 117 + <div id="license"> 118 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 119 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 120 + </div> 121 + </div> 122 + <div id="sidebar"> 123 + <img src="/logo.png"> 124 + <h2>La Guía de la Coma Flotante</h2> 125 + <ul> 126 + <li><a href="/">Inicio</a></li> 127 + <li><a href="/basic/">Respuestas Básicas</a></li> 128 + <li><a href="/references/">Referencias</a></li> 129 + <li><a href="/xkcd/">xkcd</a></li> 130 + </ul> 131 + 132 + <h2>Formatos Numéricos</h2> 133 + <ul> 134 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 135 + <li><a href="/formats/fp/">Coma Flotante</a></li> 136 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 137 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 138 + </ul> 139 + 140 + <h2>Errores</h2> 141 + <ul> 142 + <li><a href="/errors/rounding/">Redondeo</a></li> 143 + <li><a href="/errors/comparison/">Comparación</a></li> 144 + <li><a href="/errors/propagation/">Propagación</a></li> 145 + </ul> 146 + 147 + <h2>Cheat sheets</h2> 148 + <ul> 149 + <li><a href="/languages/csharp/">C#</a></li> 150 + <li><a href="/languages/java/">Java</a></li> 151 + <li><a href="/languages/javascript/">JavaScript</a></li> 152 + <li><a href="/languages/perl/">Perl</a></li> 153 + <li><a href="/languages/php/">PHP</a></li> 154 + <li><a href="/languages/python/">Python</a></li> 155 + <li><a href="/languages/sql/">SQL</a></li> 156 + </ul> 157 + </div> 158 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 159 + 160 + </body> 161 + </html>
+182
formats/binary/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Binary Fractions</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="In-depth explanation of how binary fractions work, what problems the cause and why they are used anyway"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Binary Fractions</h1> 18 + <h2 id="how-they-work">How they work</h2> 19 + <p>As a programmer, you should be familiar with the concept of binary integers, i.e. 20 + the representation of integer numbers as a series of bits:</p> 21 + 22 + <table> 23 + <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> 24 + <tr class="base_example"> 25 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">10<sup>1</sup></td><td>+</td> 26 + <td class="digit">3</td><td>&sdot;</td><td class="num_base">10<sup>0</sup></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 class="num_base">2<sup>3</sup></td><td>+</td> 30 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>2</sup></td><td>+</td> 31 + <td class="digit">0</td><td>&sdot;</td><td class="num_base">2<sup>1</sup></td><td>+</td> 32 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>0</sup></td> 33 + </tr><tr class="base_example"> 34 + <td class="digit">1</td><td>&sdot;</td><td>10</td><td>+</td> 35 + <td class="digit">3</td><td>&sdot;</td><td>1 </td><td>=</td> 36 + <td class="digit">13<sub class="num_base">10</sub></td><td class="separator">=</td> 37 + <td class="digit">1101<sub class="num_base">2</sub></td><td>=</td> 38 + <td class="digit">1</td><td>&sdot;</td><td>8</td><td>+</td> 39 + <td class="digit">1</td><td>&sdot;</td><td>4</td><td>+</td> 40 + <td class="digit">0</td><td>&sdot;</td><td>2</td><td>+</td> 41 + <td class="digit">1</td><td>&sdot;</td><td>1</td> 42 + </tr></table> 43 + 44 + <p>This is how computers store integer numbers internally. And for fractional numbers in <a href="http://en.wikipedia.org/wiki/Positional_notation">positional notation</a>, they do the same thing:</p> 45 + 46 + <table> 47 + <tr><th colspan="13">Decimal (<span class="num_base">base 10</span>)</th><th> </th><th colspan="13">Binary (<span class="num_base">base 2</span>)</th></tr> 48 + <tr class="base_example"> 49 + <td class="digit">6</td><td>&sdot;</td><td class="num_base">10<sup>-1</sup></td><td>+</td> 50 + <td class="digit">2</td><td>&sdot;</td><td class="num_base">10<sup>-2</sup></td><td>+</td> 51 + <td class="digit">5</td><td>&sdot;</td><td class="num_base">10<sup>-3</sup></td><td>=</td> 52 + <td class="digit">0.625<sub class="num_base">10</sub></td><td class="separator">=</td> 53 + <td class="digit">0.101<sub class="num_base">2</sub></td><td>=</td> 54 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>-1</sup></td><td>+</td> 55 + <td class="digit">0</td><td>&sdot;</td><td class="num_base">2<sup>-2</sup></td><td>+</td> 56 + <td class="digit">1</td><td>&sdot;</td><td class="num_base">2<sup>-3</sup></td> 57 + </tr><tr class="base_example"> 58 + <td class="digit">6</td><td>&sdot;</td><td>1/10</td><td>+</td> 59 + <td class="digit">2</td><td>&sdot;</td><td>1/100</td><td>+</td> 60 + <td class="digit">5</td><td>&sdot;</td><td>1/1000</td><td>=</td> 61 + <td class="digit">0.625<sub class="num_base">10</sub></td><td class="separator">=</td> 62 + <td class="digit">0.101<sub class="num_base">2</sub></td><td>=</td> 63 + <td class="digit">1</td><td>&sdot;</td><td>1/2</td><td>+</td> 64 + <td class="digit">0</td><td>&sdot;</td><td>1/4</td><td>+</td> 65 + <td class="digit">1</td><td>&sdot;</td><td>1/8</td> 66 + </tr></table> 67 + 68 + <h2 id="problems">Problems</h2> 69 + <p>While they work the same in principle, binary fractions are different from decimal fractions in what 70 + numbers they can accurately represent with a given number of digits, and thus also in what numbers result in <a href="/errors/rounding/">rounding errors</a>:</p> 71 + 72 + <p>Specifically, binary can only represent those numbers as a finite fraction where the denominator 73 + is a power of 2. Unfortunately, this does not include most of the numbers that can be 74 + represented as finite fraction in base 10, like 0.1.</p> 75 + 76 + <table> 77 + <thead> 78 + <tr> 79 + <th>Fraction</th> 80 + <th>Base</th> 81 + <th>Positional Notation</th> 82 + <th>Rounded to 4 digits</th> 83 + <th>Rounded value as fraction</th> 84 + <th>Rounding error</th> 85 + </tr> 86 + </thead> 87 + <tbody> 88 + <tr> 89 + <td>1/10</td> 90 + <td>10</td> 91 + <td>0.1</td> 92 + <td>0.1</td> 93 + <td>1/10</td> 94 + <td>0</td> 95 + </tr> 96 + <tr> 97 + <td>1/3</td> 98 + <td>10</td> 99 + <td>0.<span class="over">3</span></td> 100 + <td>0.3333</td> 101 + <td>3333/10000</td> 102 + <td>1/30000</td> 103 + </tr> 104 + <tr> 105 + <td>1/2</td> 106 + <td>2</td> 107 + <td>0.1</td> 108 + <td>0.1</td> 109 + <td>1/2</td> 110 + <td>0</td> 111 + </tr> 112 + <tr> 113 + <td>1/10</td> 114 + <td>2</td> 115 + <td>0.0<span class="over">0011</span></td> 116 + <td>0.0001</td> 117 + <td>1/16</td> 118 + <td>3/80</td> 119 + </tr> 120 + </tbody> 121 + </table> 122 + 123 + <p>And this is how you already get a rounding error when you just <em>write down</em> a number like 0.1 and 124 + run it through your interpreter or compiler. It’s not as big as 3/80 and may be invisible because 125 + computers cut off after 23 or 52 binary digits rather than 4. But the error is there and <em>will</em> cause 126 + problems eventually if you just ignore it.</p> 127 + 128 + <h2 id="why-use-binary">Why use Binary?</h2> 129 + <p>At the lowest level, computers are based on billions of electrical elements that have only two states, (usually low and high voltage). By interpreting these as 0 and 1, it’s very easy to build circuits for storing binary numbers and doing calculations with them.</p> 130 + 131 + <p>While it’s possible to simulate the behaviour of decimal numbers with binary circuits as well, it’s less efficient. If computers used decimal numbers internally, they’d have less memory and be slower at the same level of technology. </p> 132 + 133 + <p>Since the difference in behaviour between binary and decimal numbers is not important for most applications, the logical choice is to build computers based on binary numbers and live with the fact 134 + that some extra care and effort are necessary for applications that require <a href="/formats/exact/">decimal-like behaviour</a>.</p> 135 + 136 + 137 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 138 + <div id="license"> 139 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 140 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 141 + </div> 142 + </div> 143 + <div id="sidebar"> 144 + <img src="/logo.png"> 145 + <h2>La Guía de la Coma Flotante</h2> 146 + <ul> 147 + <li><a href="/">Inicio</a></li> 148 + <li><a href="/basic/">Respuestas Básicas</a></li> 149 + <li><a href="/references/">Referencias</a></li> 150 + <li><a href="/xkcd/">xkcd</a></li> 151 + </ul> 152 + 153 + <h2>Formatos Numéricos</h2> 154 + <ul> 155 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 156 + <li><a href="/formats/fp/">Coma Flotante</a></li> 157 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 158 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 159 + </ul> 160 + 161 + <h2>Errores</h2> 162 + <ul> 163 + <li><a href="/errors/rounding/">Redondeo</a></li> 164 + <li><a href="/errors/comparison/">Comparación</a></li> 165 + <li><a href="/errors/propagation/">Propagación</a></li> 166 + </ul> 167 + 168 + <h2>Cheat sheets</h2> 169 + <ul> 170 + <li><a href="/languages/csharp/">C#</a></li> 171 + <li><a href="/languages/java/">Java</a></li> 172 + <li><a href="/languages/javascript/">JavaScript</a></li> 173 + <li><a href="/languages/perl/">Perl</a></li> 174 + <li><a href="/languages/php/">PHP</a></li> 175 + <li><a href="/languages/python/">Python</a></li> 176 + <li><a href="/languages/sql/">SQL</a></li> 177 + </ul> 178 + </div> 179 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 180 + 181 + </body> 182 + </html>
+95
formats/exact/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Exact Types</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Description of various datatypes that can be more exact that floating-point numbers"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Exact Types</h1> 18 + <p>While <a href="/formats/binary/">binary</a> <a href="/formats/fp/">floating-point</a> numbers are better for computers to work with, and usually good enough for humans, sometimes they are just not appropriate. Sometimes, the numbers really must add up to the last bit, and no technical excuses are acceptable - usually when the calculations involve money. </p> 19 + 20 + <p>Unfortunately, there is no dominating standard like IEEE 754 for this (The 2008 version of the standard added decimal types, which is too recent to have seen widespread adoption). 21 + Each language or platform has its own solution, sometimes multiple different ones. For details, look at the language cheat sheets.</p> 22 + 23 + <p>There are at least three fundamentally different kinds of such types:</p> 24 + 25 + <h2 id="limited-precision-decimal">Limited-Precision Decimal</h2> 26 + <p>Basically the same as a IEEE 754 binary floating-point, except that the exponent is interpreted as base 10. As a result, there are no unexpected <a href="/errors/rounding/">rounding errors</a>. Also, this kind of format is relatively compact and fast, but usually slower than binary formats.</p> 27 + 28 + <h2 id="arbitrary-precision-decimal">Arbitrary-Precision Decimal</h2> 29 + <p>Sometimes called “bignum”, this is similar to a limited-precision type, but has the ability to increase the length of the significand (possibly also the exponent) as required. The downside is that there is some basic overhead (memory and speed) to support this flexibility, and that the longer the significand gets, the more 30 + memory is needed and the slower all calculations become.</p> 31 + 32 + <p>It can be very tempting to say “My calculation is important, so I need as much precision as possible”, but in practice the actual importance of precision at the 10,000th decimal digit quickly pales in comparison with the 33 + performance penalty required to support it.</p> 34 + 35 + <h2 id="symbolic-calculations">Symbolic calculations</h2> 36 + <p>The “holy grail” of exact calculations. Achieved by writing a program that actually knows all the rules of math and represents data as <em>symbols</em> rather than imprecise, rounded numbers. For example:</p> 37 + 38 + <ul> 39 + <li>1/3 is actually a fraction “one divided by three”</li> 40 + <li>The square root of 2 is really the number that, multiplied by itself, is <em>exactly</em> 2</li> 41 + <li>Even <a href="http://en.wikipedia.org/wiki/Transcendental_numbers">transcendental numbers</a> like <strong>e</strong> and <strong>π</strong> are known, together with their properties, so that e<sup>iπ</sup> is <em>exactly</em> equal to -1.</li> 42 + </ul> 43 + 44 + <p>However, these symbolic math systems are complex, slow and require significant mathematical knowledge to use. 45 + They are invaluable tools for mathematicians, but not appropriate for most everyday programming tasks. And 46 + even many mathematicians work on problems where imprecise, numerical solutions are better because no 47 + symbolic solution is known.</p> 48 + 49 + 50 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 51 + <div id="license"> 52 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 53 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 54 + </div> 55 + </div> 56 + <div id="sidebar"> 57 + <img src="/logo.png"> 58 + <h2>La Guía de la Coma Flotante</h2> 59 + <ul> 60 + <li><a href="/">Inicio</a></li> 61 + <li><a href="/basic/">Respuestas Básicas</a></li> 62 + <li><a href="/references/">Referencias</a></li> 63 + <li><a href="/xkcd/">xkcd</a></li> 64 + </ul> 65 + 66 + <h2>Formatos Numéricos</h2> 67 + <ul> 68 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 69 + <li><a href="/formats/fp/">Coma Flotante</a></li> 70 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 71 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 72 + </ul> 73 + 74 + <h2>Errores</h2> 75 + <ul> 76 + <li><a href="/errors/rounding/">Redondeo</a></li> 77 + <li><a href="/errors/comparison/">Comparación</a></li> 78 + <li><a href="/errors/propagation/">Propagación</a></li> 79 + </ul> 80 + 81 + <h2>Cheat sheets</h2> 82 + <ul> 83 + <li><a href="/languages/csharp/">C#</a></li> 84 + <li><a href="/languages/java/">Java</a></li> 85 + <li><a href="/languages/javascript/">JavaScript</a></li> 86 + <li><a href="/languages/perl/">Perl</a></li> 87 + <li><a href="/languages/php/">PHP</a></li> 88 + <li><a href="/languages/python/">Python</a></li> 89 + <li><a href="/languages/sql/">SQL</a></li> 90 + </ul> 91 + </div> 92 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 93 + 94 + </body> 95 + </html>
+181
formats/fp/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating Point Numbers</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Explanation of how floating-points numbers work and what they are good for"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating Point Numbers</h1> 18 + <h2 id="why-floating-point-numbers-are-needed">Why floating-point numbers are needed</h2> 19 + 20 + <p>Since computer memory is limited, you cannot store numbers with infinite precision, no matter whether you use <a href="/formats/binary/">binary fractions</a> or decimal ones: at some point you have to cut off. But how much accuracy is needed? And <em>where</em> is it needed? How many integer digits and how many fraction digits? </p> 21 + 22 + <ul> 23 + <li>To an engineer building a highway, it does not matter whether it’s 10 meters or 10.0001 meters wide - his measurements are probably not that accurate in the first place.</li> 24 + <li>To someone designing a microchip, 0.0001 meters (a tenth of a millimeter) is a <em>huge</em> difference - But he’ll never have to deal with a distance larger than 0.1 meters.</li> 25 + <li>A physicist needs to use the <a href="http://en.wikipedia.org/wiki/Speed_of_light">speed of light</a> (about 300000000) and <a href="http://en.wikipedia.org/wiki/Gravitational_constant">Newton’s gravitational constant</a> (about 0.0000000000667) together in the same calculation.</li> 26 + </ul> 27 + 28 + <p>To satisfy the engineer and the chip designer, a number format has to provide accuracy for numbers at very different magnitudes. However, only <em>relative</em> accuracy is needed. To satisfy the physicist, it must be possible to do calculations that involve numbers with different magnitudes.</p> 29 + 30 + <p>Basically, having a fixed number of integer and fractional digits is not useful - and the solution is a format with a <em>floating point</em>.</p> 31 + 32 + <h2 id="how-floating-point-numbers-work">How floating-point numbers work</h2> 33 + <p>The idea is to compose a number of two main parts:</p> 34 + 35 + <ul> 36 + <li>A <strong>significand</strong> that contains the number’s digits. Negative significands represent negative numbers.</li> 37 + <li>An <strong>exponent</strong> that says where the decimal (or binary) point is placed relative to the beginning of the significand. Negative exponents represent numbers that are very small (i.e. close to zero).</li> 38 + </ul> 39 + 40 + <p>Such a format satisfies all the requirements:</p> 41 + 42 + <ul> 43 + <li>It can represent numbers at wildly different magnitudes (limited by the length of the exponent)</li> 44 + <li>It provides the same relative accuracy at all magnitudes (limited by the length of the significand)</li> 45 + <li>It allows calculations across magnitudes: multiplying a very large and a very small number preserves the accuracy of both in the result.</li> 46 + </ul> 47 + 48 + <p>Decimal floating-point numbers usually take the form of <a href="http://en.wikipedia.org/wiki/Scientific_notation">scientific notation</a> with an 49 + explicit point always between the 1st and 2nd digits. The exponent is 50 + either written explicitly including the base, or an <strong>e</strong> is used to 51 + separate it from the significand.</p> 52 + 53 + <table> 54 + <thead> 55 + <tr> 56 + <th>Significand</th> 57 + <th>Exponent</th> 58 + <th>Scientific notation</th> 59 + <th>Fixed-point value</th> 60 + </tr> 61 + </thead> 62 + <tbody> 63 + <tr> 64 + <td>1.5</td> 65 + <td>4</td> 66 + <td>1.5 ⋅ 10<sup>4</sup></td> 67 + <td>15000</td> 68 + </tr> 69 + <tr> 70 + <td>-2.001</td> 71 + <td>2</td> 72 + <td>-2.001 ⋅ 10<sup>2</sup></td> 73 + <td>-200.1</td> 74 + </tr> 75 + <tr> 76 + <td>5</td> 77 + <td>-3</td> 78 + <td>5 ⋅ 10<sup>-3</sup></td> 79 + <td>0,005</td> 80 + </tr> 81 + <tr> 82 + <td>6.667</td> 83 + <td>-11</td> 84 + <td>6.667e-11</td> 85 + <td>0.0000000000667</td> 86 + </tr> 87 + </tbody> 88 + </table> 89 + 90 + <h2 id="the-standard">The standard</h2> 91 + <p>Nearly all hardware and programming languages use floating-point numbers in the same binary formats, which are defined in the <a href="http://en.wikipedia.org/wiki/IEEE_754-2008">IEEE 754</a> standard. The usual formats are 32 or 64 bits in total length:</p> 92 + 93 + <table> 94 + <thead> 95 + <tr> 96 + <th>Format</th> 97 + <th>Total bits</th> 98 + <th>Significand bits</th> 99 + <th>Exponent bits</th> 100 + <th>Smallest number</th> 101 + <th>Largest number</th> 102 + </tr> 103 + </thead> 104 + <tbody> 105 + <tr> 106 + <td>Single precision</td> 107 + <td>32</td> 108 + <td>23 + 1 sign</td> 109 + <td>8</td> 110 + <td>ca. 1.2 ⋅ 10<sup>-38</sup></td> 111 + <td>ca. 3.4 ⋅ 10<sup>38</sup></td> 112 + </tr> 113 + <tr> 114 + <td>Double precision</td> 115 + <td>64</td> 116 + <td>52 + 1 sign</td> 117 + <td>11</td> 118 + <td>ca. 5.0 ⋅ 10<sup>-324</sup></td> 119 + <td>ca. 1.8 ⋅ 10<sup>308</sup></td> 120 + </tr> 121 + </tbody> 122 + </table> 123 + 124 + <p>Note that there are some peculiarities:</p> 125 + 126 + <ul> 127 + <li>The <strong>actual bit sequence</strong> is the sign bit first, followed by the exponent and finally the significand bits.</li> 128 + <li>The exponent does not have a sign; instead an <strong>exponent bias</strong> is subtracted from it (127 for single and 1023 for double precision). This, and the bit sequence, allows floating-point numbers to be compared and sorted correctly even when interpreting them as integers.</li> 129 + <li>The significand’s most significant bit is assumed to be 1 and omitted, except for special cases.</li> 130 + <li>There are separate <strong>positive and a negative zero</strong> values, differing in the sign bit, where all other bits are 0. These must be considered equal even though their bit patterns are different.</li> 131 + <li>There are special <strong>positive and negative infinity</strong> values, where the exponent is all 1-bits and the significand is all 0-bits. These are the results of calculations where the positive range of the exponent is exceeded, or division of a regular number by zero.</li> 132 + <li>There are special <strong>not a number</strong> (or NaN) values where the exponent is all 1-bits and the significand is <em>not</em> all 0-bits. These represent the result of various undefined calculations (like multiplying 0 and infinity, any calculation involving a NaN value, or application-specific cases). Even bit-identical NaN values must <em>not</em> be considered equal.</li> 133 + </ul> 134 + 135 + 136 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 137 + <div id="license"> 138 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 139 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 140 + </div> 141 + </div> 142 + <div id="sidebar"> 143 + <img src="/logo.png"> 144 + <h2>La Guía de la Coma Flotante</h2> 145 + <ul> 146 + <li><a href="/">Inicio</a></li> 147 + <li><a href="/basic/">Respuestas Básicas</a></li> 148 + <li><a href="/references/">Referencias</a></li> 149 + <li><a href="/xkcd/">xkcd</a></li> 150 + </ul> 151 + 152 + <h2>Formatos Numéricos</h2> 153 + <ul> 154 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 155 + <li><a href="/formats/fp/">Coma Flotante</a></li> 156 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 157 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 158 + </ul> 159 + 160 + <h2>Errores</h2> 161 + <ul> 162 + <li><a href="/errors/rounding/">Redondeo</a></li> 163 + <li><a href="/errors/comparison/">Comparación</a></li> 164 + <li><a href="/errors/propagation/">Propagación</a></li> 165 + </ul> 166 + 167 + <h2>Cheat sheets</h2> 168 + <ul> 169 + <li><a href="/languages/csharp/">C#</a></li> 170 + <li><a href="/languages/java/">Java</a></li> 171 + <li><a href="/languages/javascript/">JavaScript</a></li> 172 + <li><a href="/languages/perl/">Perl</a></li> 173 + <li><a href="/languages/php/">PHP</a></li> 174 + <li><a href="/languages/python/">Python</a></li> 175 + <li><a href="/languages/sql/">SQL</a></li> 176 + </ul> 177 + </div> 178 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 179 + 180 + </body> 181 + </html>
+76
formats/integer/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - On Using Integers</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Explanation why using integers to avoid floating-point problems by having them represent e.g. cents is not a good solution."> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>On Using Integers</h1> 18 + <p>While integer types are usually binary and by definition do not support fractions, they are exact (no <a href="/errors/rounding/">rounding errors</a> when converting from decimal integers) and can be used as a sort of “poor man’s <a href="/formats/exact/">decimal type</a>” by choosing an implicit fixed decimal point so that the smallest unit you work with can be represented as the integer 1. In practical terms, this is often put as: <strong>“To handle money, store and calculate everything in cents and format only the output”</strong>.</p> 19 + 20 + <p>This works, but has a number of <strong>severe drawbacks</strong>:</p> 21 + 22 + <ul> 23 + <li>It’s more work (and more opportunity for bugs) to get it right, especially in regard to <a href="/errors/rounding/">rounding modes</a>.</li> 24 + <li>Integers have complete precision, but very limited range, and when they overflow, they usually “wrap around” silently, i.e. the largest integer plus 1 becomes zero (for unsigned ints) or the negative value with the largest magnitude (for signed). This is just about the worst possible behaviour when dealing with money, for obvious reasons.</li> 25 + <li>The implicit decimal point is hard to change and extremely inflexible: if you store dollars as cents, it’s simply impossible to support the <a href="http://en.wikipedia.org/wiki/Bahraini_dinar">Bahraini dinar</a>(1 dinar = 1,000 Fils) at the same time. You’d have to store the position of the decimal point with the data - the first step in implementing your own (buggy, non-standard) limited-precision decimal <a href="/formats/fp/">floating-point</a> format.</li> 26 + </ul> 27 + 28 + <p>Summary: <strong>using integers is not recommended.</strong> Do this only if there really is no <a href="/formats/exact/">better alternative</a> at all.</p> 29 + 30 + 31 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 32 + <div id="license"> 33 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 34 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 35 + </div> 36 + </div> 37 + <div id="sidebar"> 38 + <img src="/logo.png"> 39 + <h2>La Guía de la Coma Flotante</h2> 40 + <ul> 41 + <li><a href="/">Inicio</a></li> 42 + <li><a href="/basic/">Respuestas Básicas</a></li> 43 + <li><a href="/references/">Referencias</a></li> 44 + <li><a href="/xkcd/">xkcd</a></li> 45 + </ul> 46 + 47 + <h2>Formatos Numéricos</h2> 48 + <ul> 49 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 50 + <li><a href="/formats/fp/">Coma Flotante</a></li> 51 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 52 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 53 + </ul> 54 + 55 + <h2>Errores</h2> 56 + <ul> 57 + <li><a href="/errors/rounding/">Redondeo</a></li> 58 + <li><a href="/errors/comparison/">Comparación</a></li> 59 + <li><a href="/errors/propagation/">Propagación</a></li> 60 + </ul> 61 + 62 + <h2>Cheat sheets</h2> 63 + <ul> 64 + <li><a href="/languages/csharp/">C#</a></li> 65 + <li><a href="/languages/java/">Java</a></li> 66 + <li><a href="/languages/javascript/">JavaScript</a></li> 67 + <li><a href="/languages/perl/">Perl</a></li> 68 + <li><a href="/languages/php/">PHP</a></li> 69 + <li><a href="/languages/python/">Python</a></li> 70 + <li><a href="/languages/sql/">SQL</a></li> 71 + </ul> 72 + </div> 73 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 74 + 75 + </body> 76 + </html>
+90
index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Lo Que Todo Programador Debería Saber Sobre Aritmética de Coma Flotante</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Pretende dar respuestas cortas y sencillas a las preguntas recurrentes de programadores principiantes sobre números de coma flotante que «no se suman» correctamente, e información más detallada sobre cómo funcionan los números decimales del IEEE 754, cuándo y cómo usarlos correctamente, y qué usar en su lugar cuando no son apropiados."> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Lo Que Todo Programador Debería Saber Sobre Aritmética de Coma Flotante</h1> 18 + <h2 id="o">o</h2> 19 + 20 + <h1 id="por-qu-mis-nmeros-no-se-suman-bien">¿Por qué mis números no se suman bien?</h1> 21 + 22 + <p>O sea que has escrito algún código absurdamente simple, como por ejemplo:</p> 23 + 24 + <pre><code> 0.1 + 0.2 25 + </code></pre> 26 + 27 + <p>y has obtenido un resultado totalmente inesperado:</p> 28 + 29 + <pre><code> 0.30000000000000004 30 + </code></pre> 31 + 32 + <p>Tal vez pediste ayuda en algún foro y te mandaron a un <a href="http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html">artículo largo con un montón de fórmulas</a> que no parecía ser de ayuda.</p> 33 + 34 + <p>Bueno, este sitio está aquí para:</p> 35 + 36 + <ul> 37 + <li>Explicar de manera concisa por qué obtuviste ese resultado inesperado</li> 38 + <li>Decirte cómo lidiar con este problema</li> 39 + <li>Si te interesa, dar explicaciones detalladas de por qué los números de coma flotante tienen que funcionar así y qué otros problemas pueden surgir</li> 40 + </ul> 41 + 42 + <p>Deberías ir a la sección de <a href="/basic/">Respuestas Básicas</a> primero - ¡pero no termines ahí!</p> 43 + 44 + 45 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 46 + <div id="license"> 47 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 48 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 49 + </div> 50 + </div> 51 + <div id="sidebar"> 52 + <img src="/logo.png"> 53 + <h2>La Guía de la Coma Flotante</h2> 54 + <ul> 55 + <li><a href="/">Inicio</a></li> 56 + <li><a href="/basic/">Respuestas Básicas</a></li> 57 + <li><a href="/references/">Referencias</a></li> 58 + <li><a href="/xkcd/">xkcd</a></li> 59 + </ul> 60 + 61 + <h2>Formatos Numéricos</h2> 62 + <ul> 63 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 64 + <li><a href="/formats/fp/">Coma Flotante</a></li> 65 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 66 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 67 + </ul> 68 + 69 + <h2>Errores</h2> 70 + <ul> 71 + <li><a href="/errors/rounding/">Redondeo</a></li> 72 + <li><a href="/errors/comparison/">Comparación</a></li> 73 + <li><a href="/errors/propagation/">Propagación</a></li> 74 + </ul> 75 + 76 + <h2>Cheat sheets</h2> 77 + <ul> 78 + <li><a href="/languages/csharp/">C#</a></li> 79 + <li><a href="/languages/java/">Java</a></li> 80 + <li><a href="/languages/javascript/">JavaScript</a></li> 81 + <li><a href="/languages/perl/">Perl</a></li> 82 + <li><a href="/languages/php/">PHP</a></li> 83 + <li><a href="/languages/python/">Python</a></li> 84 + <li><a href="/languages/sql/">SQL</a></li> 85 + </ul> 86 + </div> 87 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 88 + 89 + </body> 90 + </html>
+96
languages/csharp/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for C#</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in C#"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for C#</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>C# has <a href="/formats/fp/">IEEE 754</a> single and double precision types supported by keywords:</p> 20 + 21 + <pre><code> float f = 0.1f; // 32 bit float, note f suffix 22 + double d = 0.1d; // 64 bit float, suffix optional 23 + </code></pre> 24 + 25 + <h2 id="decimal-types">Decimal Types</h2> 26 + <p>C# has a 128 bit <a href="/formats/exact/">limited-precision</a> decimal type denoted by the keyword 27 + <code>decimal</code>:</p> 28 + 29 + <pre><code> decimal myMoney = 300.1m; // note m suffix on the literal 30 + </code></pre> 31 + 32 + <h2 id="how-to-round">How to Round</h2> 33 + <p>The <code>Math.Round()</code> method works with the double and decimal types, and allows you to specify a <a href="/errors/rounding/">rounding mode</a>:</p> 34 + 35 + <pre><code> Math.Round(1.25m, 1, MidpointRounding.AwayFromZero); // returns 1.3 36 + </code></pre> 37 + 38 + <h2 id="resources">Resources</h2> 39 + <ul> 40 + <li><a href="http://msdn.microsoft.com/en-us/library/618ayhy6%28v=VS.80%29.aspx">C# Reference</a> 41 + <ul> 42 + <li><a href="http://msdn.microsoft.com/en-us/library/b1e65aza%28v=VS.80%29.aspx">float type</a> </li> 43 + <li><a href="http://msdn.microsoft.com/en-us/library/678hzkk9%28v=VS.80%29.aspx">double type</a> </li> 44 + <li><a href="http://msdn.microsoft.com/en-us/library/364x0z75%28v=VS.80%29.aspx">decimal type</a> </li> 45 + <li><a href="http://msdn.microsoft.com/en-US/library/system.math.round%28v=VS.80%29.aspx">Math.Round()</a></li> 46 + </ul> 47 + </li> 48 + </ul> 49 + 50 + 51 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 52 + <div id="license"> 53 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 54 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 55 + </div> 56 + </div> 57 + <div id="sidebar"> 58 + <img src="/logo.png"> 59 + <h2>La Guía de la Coma Flotante</h2> 60 + <ul> 61 + <li><a href="/">Inicio</a></li> 62 + <li><a href="/basic/">Respuestas Básicas</a></li> 63 + <li><a href="/references/">Referencias</a></li> 64 + <li><a href="/xkcd/">xkcd</a></li> 65 + </ul> 66 + 67 + <h2>Formatos Numéricos</h2> 68 + <ul> 69 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 70 + <li><a href="/formats/fp/">Coma Flotante</a></li> 71 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 72 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 73 + </ul> 74 + 75 + <h2>Errores</h2> 76 + <ul> 77 + <li><a href="/errors/rounding/">Redondeo</a></li> 78 + <li><a href="/errors/comparison/">Comparación</a></li> 79 + <li><a href="/errors/propagation/">Propagación</a></li> 80 + </ul> 81 + 82 + <h2>Cheat sheets</h2> 83 + <ul> 84 + <li><a href="/languages/csharp/">C#</a></li> 85 + <li><a href="/languages/java/">Java</a></li> 86 + <li><a href="/languages/javascript/">JavaScript</a></li> 87 + <li><a href="/languages/perl/">Perl</a></li> 88 + <li><a href="/languages/php/">PHP</a></li> 89 + <li><a href="/languages/python/">Python</a></li> 90 + <li><a href="/languages/sql/">SQL</a></li> 91 + </ul> 92 + </div> 93 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 94 + 95 + </body> 96 + </html>
+122
languages/java/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for Java</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in Java"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for Java</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>Java has <a href="/formats/fp/">IEEE 754</a> single and double precision types supported by keywords:</p> 20 + 21 + <pre><code> float f = 0.1f; // 32 bit float, note f suffix 22 + double d = 0.1d; // 64 bit float, suffix optional 23 + </code></pre> 24 + 25 + <p>The <code>strictfp</code> keyword on classes, interfaces and methods forces all intermediate results of floating-point calculations to be IEEE 754 values as well, guaranteeing identical results on all platforms. Without that keyword, implementations can use an extended exponent range where available, resulting in more precise results and faster execution on many common CPUs.</p> 26 + 27 + <h2 id="decimal-types">Decimal Types</h2> 28 + <p>Java has an <a href="/formats/exact/">arbitrary-precision</a> decimal type named <code>java.math.BigDecimal</code>, which 29 + also allows to choose the <a href="/errors/rounding/">rounding mode</a>.</p> 30 + 31 + <pre><code> BigDecimal a = new BigDecimal("0.1"); 32 + BigDecimal b = new BigDecimal("0.2"); 33 + BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3 34 + </code></pre> 35 + 36 + <h2 id="how-to-round">How to Round</h2> 37 + <p>To get a String:</p> 38 + 39 + <pre><code> String.format("%.2f", 1.2399) // returns "1.24" 40 + String.format("%.3f", 1.2399) // returns "1.240" 41 + String.format("%.2f", 1.2) // returns "1.20" 42 + </code></pre> 43 + 44 + <p>To print to standard output (or any <code>PrintStream</code>):</p> 45 + 46 + <pre><code> System.out.printf("%.2f", 1.2399) // same syntax as String.format() 47 + </code></pre> 48 + 49 + <p>If you don’t want trailing zeroes:</p> 50 + 51 + <pre><code> new DecimalFormat("0.00").format(1.2)// returns "1.20" 52 + new DecimalFormat("0.##").format(1.2)// returns "1.2" 53 + </code></pre> 54 + 55 + <p>If you need a specific <a href="/errors/rounding/">rounding mode</a>:</p> 56 + 57 + <pre><code> new BigDecimal("1.25").setScale(1, RoundingMode.HALF_EVEN); // returns 1.2 58 + </code></pre> 59 + 60 + <h2 id="resources">Resources</h2> 61 + <ul> 62 + <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html">Java Language Specification</a><br /> 63 + <ul> 64 + <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3">Floating-Point Types, Formats, and Values</a></li> 65 + </ul> 66 + </li> 67 + <li><a href="http://java.sun.com/javase/6/docs/api/">Java Standard API</a> 68 + <ul> 69 + <li><a href="http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html">BigDecimal</a></li> 70 + <li><a href="http://download.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html">DecimalFormat</a></li> 71 + <li><a href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)">String.format()</a></li> 72 + </ul> 73 + </li> 74 + </ul> 75 + 76 + 77 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 78 + <div id="license"> 79 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 80 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 81 + </div> 82 + </div> 83 + <div id="sidebar"> 84 + <img src="/logo.png"> 85 + <h2>La Guía de la Coma Flotante</h2> 86 + <ul> 87 + <li><a href="/">Inicio</a></li> 88 + <li><a href="/basic/">Respuestas Básicas</a></li> 89 + <li><a href="/references/">Referencias</a></li> 90 + <li><a href="/xkcd/">xkcd</a></li> 91 + </ul> 92 + 93 + <h2>Formatos Numéricos</h2> 94 + <ul> 95 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 96 + <li><a href="/formats/fp/">Coma Flotante</a></li> 97 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 98 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 99 + </ul> 100 + 101 + <h2>Errores</h2> 102 + <ul> 103 + <li><a href="/errors/rounding/">Redondeo</a></li> 104 + <li><a href="/errors/comparison/">Comparación</a></li> 105 + <li><a href="/errors/propagation/">Propagación</a></li> 106 + </ul> 107 + 108 + <h2>Cheat sheets</h2> 109 + <ul> 110 + <li><a href="/languages/csharp/">C#</a></li> 111 + <li><a href="/languages/java/">Java</a></li> 112 + <li><a href="/languages/javascript/">JavaScript</a></li> 113 + <li><a href="/languages/perl/">Perl</a></li> 114 + <li><a href="/languages/php/">PHP</a></li> 115 + <li><a href="/languages/python/">Python</a></li> 116 + <li><a href="/languages/sql/">SQL</a></li> 117 + </ul> 118 + </div> 119 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 120 + 121 + </body> 122 + </html>
+104
languages/javascript/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for JavaScript</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in JavaScript"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for JavaScript</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>JavaScript is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are IEEE 64 bit values). To force a variable to floating-point, use the global <code>parseFloat()</code> function.</p> 20 + 21 + <pre><code> var num = parseFloat("3.5"); 22 + </code></pre> 23 + 24 + <h2 id="decimal-types">Decimal Types</h2> 25 + 26 + <p>The best decimal type for JavaScript seems to be a port of <a href="/languages/java/">Java’s</a> <code>BigDecimal</code> class, which also supports <a href="/errors/rounding/">rounding modes</a>:</p> 27 + 28 + <pre><code> var a = new BigDecimal("0.01"); 29 + var b = new BigDecimal("0.02"); 30 + var c = a.add(b); // 0.03 31 + var d = c.setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 32 + </code></pre> 33 + 34 + <h2 id="how-to-round">How to Round</h2> 35 + 36 + <pre><code> var num = 5.123456; 37 + num.toPrecision(1) //returns 5 as string 38 + num.toPrecision(2) //returns 5.1 as string 39 + num.toPrecision(4) //returns 5.123 as string 40 + </code></pre> 41 + 42 + <p>Using a specific rounding mode:</p> 43 + 44 + <pre><code> new BigDecimal("1.25").setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 45 + </code></pre> 46 + 47 + <h2 id="resources">Resources</h2> 48 + <ul> 49 + <li><a href="https://github.com/dtrebbien/BigDecimal.js">BigDecimal for JavaScript</a></li> 50 + <li><a href="https://developer.mozilla.org/en/JavaScript/Reference">Core JavaScript Reference</a><br /> 51 + <ul> 52 + <li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat">parseFloat()</a></li> 53 + <li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toPrecision">toPrecision()</a></li> 54 + </ul> 55 + </li> 56 + </ul> 57 + 58 + 59 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 60 + <div id="license"> 61 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 62 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 63 + </div> 64 + </div> 65 + <div id="sidebar"> 66 + <img src="/logo.png"> 67 + <h2>La Guía de la Coma Flotante</h2> 68 + <ul> 69 + <li><a href="/">Inicio</a></li> 70 + <li><a href="/basic/">Respuestas Básicas</a></li> 71 + <li><a href="/references/">Referencias</a></li> 72 + <li><a href="/xkcd/">xkcd</a></li> 73 + </ul> 74 + 75 + <h2>Formatos Numéricos</h2> 76 + <ul> 77 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 78 + <li><a href="/formats/fp/">Coma Flotante</a></li> 79 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 80 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 81 + </ul> 82 + 83 + <h2>Errores</h2> 84 + <ul> 85 + <li><a href="/errors/rounding/">Redondeo</a></li> 86 + <li><a href="/errors/comparison/">Comparación</a></li> 87 + <li><a href="/errors/propagation/">Propagación</a></li> 88 + </ul> 89 + 90 + <h2>Cheat sheets</h2> 91 + <ul> 92 + <li><a href="/languages/csharp/">C#</a></li> 93 + <li><a href="/languages/java/">Java</a></li> 94 + <li><a href="/languages/javascript/">JavaScript</a></li> 95 + <li><a href="/languages/perl/">Perl</a></li> 96 + <li><a href="/languages/php/">PHP</a></li> 97 + <li><a href="/languages/python/">Python</a></li> 98 + <li><a href="/languages/sql/">SQL</a></li> 99 + </ul> 100 + </div> 101 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 102 + 103 + </body> 104 + </html>
+123
languages/perl/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for Perl</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in Perl"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for Perl</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>Perl supports platform-native floating-point as scalar values; in practice this usually means <a href="/formats/fp/">IEEE 754</a> double precision.</p> 20 + 21 + <h2 id="exact-types">Exact Types</h2> 22 + <p>Perl can also store decimal numbers as strings, but the builtin arithmetic operators will convert them to integer or floating-point values to perform the operation.</p> 23 + 24 + <p>The <code>Math::BigFloat</code> extension provides an arbitrary-precision <a href="/formats/exact/">decimal type</a>:</p> 25 + 26 + <pre><code> use Math::BigFloat ':constant' 27 + my $f = 0.1 + 0.2; # returns exactly 0.3 28 + </code></pre> 29 + 30 + <p>The <code>Number::Fraction</code> extension provides a fraction type that overloads the arithmetic operators with <a href="/formats/exact/">symbolic</a> fraction arithmetic:</p> 31 + 32 + <pre><code> use Number::Fraction ':constants'; 33 + my $f = '1/2' - '1/3'; # returns 1/6 34 + </code></pre> 35 + 36 + <p>The <code>Math::BigRat</code> extension provides similar functionality. Its advantage is compatibility with the 37 + <code>Math::BigInt</code> and <code>Math::BigFloat</code> extensions, but it does not seem to support fraction literals.</p> 38 + 39 + <h2 id="how-to-round">How to Round</h2> 40 + <p>To get a string:</p> 41 + 42 + <pre><code> $result = sprintf("%.2f", 1.2345); # returns 1.23 43 + </code></pre> 44 + 45 + <p>To format output:</p> 46 + 47 + <pre><code> printf("%.2f", 1.2); # prints 1.20 48 + </code></pre> 49 + 50 + <p>Note that this implicitly uses <a href="/errors/rounding/">round-to-even</a>. The variable <code>$#</code> contains the default format for printing numbers, but its use is considered deprecated.</p> 51 + 52 + <p>The <code>Math::Round</code> extension provides various functions for rounding floating-point values:</p> 53 + 54 + <pre><code> use Math::Round qw(:all); 55 + $result = nearest(.1, 4.567) # prints 4.6 56 + $result = nearest(.01, 4.567) # prints 4.57 57 + </code></pre> 58 + 59 + <p>The <code>Math::BigFloat</code> extension also supports various <a href="/errors/rounding/">rounding modes</a>:</p> 60 + 61 + <pre><code> use Math::BigFloat; 62 + my $n = Math::BigFloat-&gt;new(123.455); 63 + my $f1 = $n-&gt;round('','-2','common'); # returns 123.46 64 + my $f2 = $n-&gt;round('','-2','zero'); # returns 123.45 65 + </code></pre> 66 + 67 + <h2 id="resources">Resources</h2> 68 + <ul> 69 + <li><a href="http://perldoc.perl.org/perlnumber.html">Semantics of numbers and numeric operations in Perl</a></li> 70 + <li><a href="http://perldoc.perl.org/functions/sprintf.html">sprintf function</a></li> 71 + <li><a href="http://search.cpan.org/dist/Math-Round/Round.pm">Math::Round extension</a></li> 72 + <li><a href="http://search.cpan.org/~davecross/Number-Fraction-1.13/lib/Number/Fraction.pm">Number::Fraction extension</a></li> 73 + <li><a href="http://search.cpan.org/~flora/Math-BigRat-0.26/lib/Math/BigRat.pm">Math::BigRat extension</a></li> 74 + <li><a href="http://search.cpan.org/~flora/Math-BigInt-1.95/lib/Math/BigFloat.pm">Math::BigFloat extension</a></li> 75 + </ul> 76 + 77 + 78 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 79 + <div id="license"> 80 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 81 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 82 + </div> 83 + </div> 84 + <div id="sidebar"> 85 + <img src="/logo.png"> 86 + <h2>La Guía de la Coma Flotante</h2> 87 + <ul> 88 + <li><a href="/">Inicio</a></li> 89 + <li><a href="/basic/">Respuestas Básicas</a></li> 90 + <li><a href="/references/">Referencias</a></li> 91 + <li><a href="/xkcd/">xkcd</a></li> 92 + </ul> 93 + 94 + <h2>Formatos Numéricos</h2> 95 + <ul> 96 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 97 + <li><a href="/formats/fp/">Coma Flotante</a></li> 98 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 99 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 100 + </ul> 101 + 102 + <h2>Errores</h2> 103 + <ul> 104 + <li><a href="/errors/rounding/">Redondeo</a></li> 105 + <li><a href="/errors/comparison/">Comparación</a></li> 106 + <li><a href="/errors/propagation/">Propagación</a></li> 107 + </ul> 108 + 109 + <h2>Cheat sheets</h2> 110 + <ul> 111 + <li><a href="/languages/csharp/">C#</a></li> 112 + <li><a href="/languages/java/">Java</a></li> 113 + <li><a href="/languages/javascript/">JavaScript</a></li> 114 + <li><a href="/languages/perl/">Perl</a></li> 115 + <li><a href="/languages/php/">PHP</a></li> 116 + <li><a href="/languages/python/">Python</a></li> 117 + <li><a href="/languages/sql/">SQL</a></li> 118 + </ul> 119 + </div> 120 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 121 + 122 + </body> 123 + </html>
+96
languages/php/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for PHP</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in PHP"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for PHP</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>PHP is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are platform-dependant, but typically IEEE 64 bit values). To force a value to floating-point, evaluate it in a numerical context:</p> 20 + 21 + <pre><code> $foo = 0 + "10.5"; 22 + </code></pre> 23 + 24 + <h2 id="decimal-types">Decimal Types</h2> 25 + <p>The BC Math extension implements <a href="/formats/exact/">arbitrary-precision</a> decimal math:</p> 26 + 27 + <pre><code> $a = '0.1'; 28 + $b = '0.2'; 29 + echo bcadd($a, $b); // prints 0.3 30 + </code></pre> 31 + 32 + <h2 id="how-to-round">How to Round</h2> 33 + <p>Rounding can be done with the <code>number_format()</code> function:</p> 34 + 35 + <pre><code> $number = 4.123; 36 + echo number_format($number, 2); // prints 4.12 37 + </code></pre> 38 + 39 + <h2 id="resources">Resources</h2> 40 + <ul> 41 + <li><a href="http://www.php.net/manual/en/index.php">PHP manual</a> 42 + <ul> 43 + <li><a href="http://www.php.net/manual/en/language.types.float.php">Floating point types</a></li> 44 + <li><a href="http://de3.php.net/manual/en/ref.bc.php">BC Math extension</a></li> 45 + <li><a href="http://de3.php.net/manual/en/function.number-format.php">number_format()</a></li> 46 + </ul> 47 + </li> 48 + </ul> 49 + 50 + 51 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 52 + <div id="license"> 53 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 54 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 55 + </div> 56 + </div> 57 + <div id="sidebar"> 58 + <img src="/logo.png"> 59 + <h2>La Guía de la Coma Flotante</h2> 60 + <ul> 61 + <li><a href="/">Inicio</a></li> 62 + <li><a href="/basic/">Respuestas Básicas</a></li> 63 + <li><a href="/references/">Referencias</a></li> 64 + <li><a href="/xkcd/">xkcd</a></li> 65 + </ul> 66 + 67 + <h2>Formatos Numéricos</h2> 68 + <ul> 69 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 70 + <li><a href="/formats/fp/">Coma Flotante</a></li> 71 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 72 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 73 + </ul> 74 + 75 + <h2>Errores</h2> 76 + <ul> 77 + <li><a href="/errors/rounding/">Redondeo</a></li> 78 + <li><a href="/errors/comparison/">Comparación</a></li> 79 + <li><a href="/errors/propagation/">Propagación</a></li> 80 + </ul> 81 + 82 + <h2>Cheat sheets</h2> 83 + <ul> 84 + <li><a href="/languages/csharp/">C#</a></li> 85 + <li><a href="/languages/java/">Java</a></li> 86 + <li><a href="/languages/javascript/">JavaScript</a></li> 87 + <li><a href="/languages/perl/">Perl</a></li> 88 + <li><a href="/languages/php/">PHP</a></li> 89 + <li><a href="/languages/python/">Python</a></li> 90 + <li><a href="/languages/sql/">SQL</a></li> 91 + </ul> 92 + </div> 93 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 94 + 95 + </body> 96 + </html>
+108
languages/python/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for Python</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in Python"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for Python</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>Almost all platforms map Python floats to <a href="/formats/fp/">IEEE 754</a> 20 + double precision.</p> 21 + 22 + <pre><code> f = 0.1 23 + </code></pre> 24 + 25 + <h2 id="decimal-types">Decimal Types</h2> 26 + <p>Python has an <a href="/formats/exact/">arbitrary-precision</a> decimal type named <code>Decimal</code> in the <code>decimal</code> module, which also allows to choose the <a href="/errors/rounding/">rounding mode</a>.</p> 27 + 28 + <pre><code> a = Decimal('0.1') 29 + b = Decimal('0.2') 30 + c = a + b # returns a Decimal representing exactly 0.3 31 + </code></pre> 32 + 33 + <h2 id="how-to-round">How to Round</h2> 34 + <p>To get a string:</p> 35 + 36 + <pre><code> "%.2f" % 1.2399 # returns "1.24" 37 + "%.3f" % 1.2399 # returns "1.240" 38 + "%.2f" % 1.2 # returns "1.20" 39 + </code></pre> 40 + 41 + <p>To print to standard output:</p> 42 + 43 + <pre><code> print "%.2f" % 1.2399 # just use print and string formatting 44 + </code></pre> 45 + 46 + <p>Specific <a href="/errors/rounding/">rounding modes</a> and other parameters can be defined in a Context object:</p> 47 + 48 + <pre><code> getcontext().prec = 7 49 + </code></pre> 50 + 51 + <h2 id="resources">Resources</h2> 52 + <ul> 53 + <li><a href="http://docs.python.org/tutorial/floatingpoint.html">Floating Point Arithmetic: Issues and Limitations</a></li> 54 + <li><a href="http://docs.python.org/library/decimal.html">The decimal module</a> 55 + <ul> 56 + <li><a href="http://docs.python.org/library/decimal.html#context-objects">Context objects</a></li> 57 + </ul> 58 + </li> 59 + <li><a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations">String formatting in Python</a></li> 60 + </ul> 61 + 62 + 63 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 64 + <div id="license"> 65 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 66 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 67 + </div> 68 + </div> 69 + <div id="sidebar"> 70 + <img src="/logo.png"> 71 + <h2>La Guía de la Coma Flotante</h2> 72 + <ul> 73 + <li><a href="/">Inicio</a></li> 74 + <li><a href="/basic/">Respuestas Básicas</a></li> 75 + <li><a href="/references/">Referencias</a></li> 76 + <li><a href="/xkcd/">xkcd</a></li> 77 + </ul> 78 + 79 + <h2>Formatos Numéricos</h2> 80 + <ul> 81 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 82 + <li><a href="/formats/fp/">Coma Flotante</a></li> 83 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 84 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 85 + </ul> 86 + 87 + <h2>Errores</h2> 88 + <ul> 89 + <li><a href="/errors/rounding/">Redondeo</a></li> 90 + <li><a href="/errors/comparison/">Comparación</a></li> 91 + <li><a href="/errors/propagation/">Propagación</a></li> 92 + </ul> 93 + 94 + <h2>Cheat sheets</h2> 95 + <ul> 96 + <li><a href="/languages/csharp/">C#</a></li> 97 + <li><a href="/languages/java/">Java</a></li> 98 + <li><a href="/languages/javascript/">JavaScript</a></li> 99 + <li><a href="/languages/perl/">Perl</a></li> 100 + <li><a href="/languages/php/">PHP</a></li> 101 + <li><a href="/languages/python/">Python</a></li> 102 + <li><a href="/languages/sql/">SQL</a></li> 103 + </ul> 104 + </div> 105 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 106 + 107 + </body> 108 + </html>
+100
languages/sql/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - Floating-point cheat sheet for SQL</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Tips for using floating-point and decimal numbers in SQL"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>Floating-point cheat sheet for SQL</h1> 18 + <h2 id="floating-point-types">Floating-Point Types</h2> 19 + <p>The SQL standard defines three binary floating-point types:</p> 20 + 21 + <ul> 22 + <li><code>REAL</code> has implementation-dependant precision (usually maps to a hardware-supported type like IEEE 754 single or double precision)</li> 23 + <li><code>DOUBLE PRECISION</code> has implementation-dependant precision which is greater than <code>REAL</code> (usually maps to IEEE 754 double precision)</li> 24 + <li><code>FLOAT(N)</code> has at least <code>N</code> binary digits of precision, with an implementation-dependant maximum for <code>N</code></li> 25 + </ul> 26 + 27 + <p>The exponent range for all three types is implementation-dependant as well.</p> 28 + 29 + <h2 id="decimal-types">Decimal Types</h2> 30 + <p>The standard defines two fixed-point decimal types:</p> 31 + 32 + <ul> 33 + <li><code>NUMERIC(M,N)</code> has exactly <code>M</code> total digits, <code>N</code> of them after the decimal point</li> 34 + <li><code>DECIMAL(M,N)</code> is the same as <code>NUMERIC(M,N)</code>, except that it is allowed to have more than <code>M</code> total digits</li> 35 + </ul> 36 + 37 + <p>The maximum values of <code>M</code> and <code>M</code> are implementation-dependant. Vendors often implement the two types identically.</p> 38 + 39 + <h2 id="how-to-round">How to Round</h2> 40 + 41 + <p>The SQL standard defines no explicit rounding, but most vendors provide a <code>ROUND()</code> or <code>TRUNC()</code> function.</p> 42 + 43 + <p>However, it usually makes little sense to round within the database, since its job is <em>storing</em> data, while rounding is an aspect of <em>displaying</em> data, and should therefore be done by the code in the presentation layer.</p> 44 + 45 + <h2 id="resources">Resources</h2> 46 + <ul> 47 + <li><a href="http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=38640">Official ISO SQL 2008 standard (non-free)</a> </li> 48 + <li><a href="http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt">SQL 92 draft (free)</a></li> 49 + <li><a href="http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html">MySQL numeric types</a></li> 50 + <li><a href="http://www.postgresql.org/docs/8.1/static/datatype.html">PostgreSQL data types</a></li> 51 + <li><a href="http://msdn.microsoft.com/en-US/library/ms187752%28v=SQL.90%29.aspx">MS SQL Server data types</a></li> 52 + </ul> 53 + 54 + 55 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 56 + <div id="license"> 57 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 58 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 59 + </div> 60 + </div> 61 + <div id="sidebar"> 62 + <img src="/logo.png"> 63 + <h2>La Guía de la Coma Flotante</h2> 64 + <ul> 65 + <li><a href="/">Inicio</a></li> 66 + <li><a href="/basic/">Respuestas Básicas</a></li> 67 + <li><a href="/references/">Referencias</a></li> 68 + <li><a href="/xkcd/">xkcd</a></li> 69 + </ul> 70 + 71 + <h2>Formatos Numéricos</h2> 72 + <ul> 73 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 74 + <li><a href="/formats/fp/">Coma Flotante</a></li> 75 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 76 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 77 + </ul> 78 + 79 + <h2>Errores</h2> 80 + <ul> 81 + <li><a href="/errors/rounding/">Redondeo</a></li> 82 + <li><a href="/errors/comparison/">Comparación</a></li> 83 + <li><a href="/errors/propagation/">Propagación</a></li> 84 + </ul> 85 + 86 + <h2>Cheat sheets</h2> 87 + <ul> 88 + <li><a href="/languages/csharp/">C#</a></li> 89 + <li><a href="/languages/java/">Java</a></li> 90 + <li><a href="/languages/javascript/">JavaScript</a></li> 91 + <li><a href="/languages/perl/">Perl</a></li> 92 + <li><a href="/languages/php/">PHP</a></li> 93 + <li><a href="/languages/python/">Python</a></li> 94 + <li><a href="/languages/sql/">SQL</a></li> 95 + </ul> 96 + </div> 97 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 98 + 99 + </body> 100 + </html>
+14 -9
layouts/default.html xkcd/index.html
··· 2 2 <html lang="es"> 3 3 <head> 4 4 <meta charset="utf-8"> 5 - <title>La Guía de la Coma Flotante - <%= @item[:title] %></title> 6 - <% if @item[:verification] %> 7 - <meta name="google-site-verification" content="SoYmbsJEmSz2s1LmZk_cku4pKwhRsU6m0ZOTgGdnTL0" /> 8 - <% end %> 5 + <title>La Guía de la Coma Flotante - xkcd</title> 6 + 9 7 <meta name="generator" content="nanoc 3.1.2"> 10 - <% if @item[:description] %> 11 - <meta name="Description" content="<%= @item[:description] %>"> 12 - <% end %> 8 + 9 + <meta name="Description" content="How to mess with people who've learned to *expect* rounding errors in floating-point math."> 10 + 13 11 <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 14 12 <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 15 13 <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 16 14 </head> 17 15 <body> 18 16 <div id="main"> 19 - <h1><%= @item[:title] %></h1> 20 - <%= yield %> 17 + <h1>xkcd</h1> 18 + <h2 id="or">or</h2> 19 + 20 + <h1 id="how-to-mess-with-people-whove-learned-to-expect-rounding-errors-in-floating-point-math">How to mess with people who’ve learned to <em>expect</em> rounding errors in floating-point math.</h1> 21 + 22 + <p><img src="http://imgs.xkcd.com/comics/e_to_the_pi_minus_pi.png" alt="Obligatory xkcd cartoon" title="Obligatory xkcd cartoon" /></p> 23 + 24 + <p>From <a href="http://www.xkcd.com/217/">xkcd</a></p> 25 + 21 26 22 27 <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 23 28 <div id="license">
-2
lib/default.rb
··· 1 - # All files in the 'lib' directory will be loaded 2 - # before nanoc starts compiling.
+76
references/index.html
··· 1 + <!DOCTYPE HTML> 2 + <html lang="es"> 3 + <head> 4 + <meta charset="utf-8"> 5 + <title>La Guía de la Coma Flotante - References</title> 6 + 7 + <meta name="generator" content="nanoc 3.1.2"> 8 + 9 + <meta name="Description" content="Documents with more in-depth information about floating-point math"> 10 + 11 + <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 + <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> 13 + <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> 14 + </head> 15 + <body> 16 + <div id="main"> 17 + <h1>References</h1> 18 + <p>Documents that contain more in-depth information about the topics 19 + covered on this wbesite:</p> 20 + 21 + <ul> 22 + <li><a href="http://grouper.ieee.org/groups/754/">Homepage of the IEEE 754 standard</a></li> 23 + <li><a href="http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a></li> 24 + <li><a href="http://www.cs.berkeley.edu/~wkahan/">Homepage of William Kahan (architect of the IEEE 754 standard, lots of interesting links)</a></li> 25 + <li><a href="http://speleotrove.com/decimal/decifaq.html">Decimal Arithmetic FAQ </a></li> 26 + <li><a href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">Comparing floating-point numbers</a></li> 27 + <li><a href="http://www.easysurf.cc/cnver17.htm">Tool to convert numbers between bases, including fractions</a></li> 28 + </ul> 29 + 30 + 31 + <g:plusone href="http://juanlu001.github.com/comaflotante/"></g:plusone> 32 + <div id="license"> 33 + <p>&copy; Publicado en <a href="http://juanlu001.github.com/comaflotante/">http://juanlu001.github.com/comaflotante/</a> bajo la 34 + <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution License (BY)</a></p> 35 + </div> 36 + </div> 37 + <div id="sidebar"> 38 + <img src="/logo.png"> 39 + <h2>La Guía de la Coma Flotante</h2> 40 + <ul> 41 + <li><a href="/">Inicio</a></li> 42 + <li><a href="/basic/">Respuestas Básicas</a></li> 43 + <li><a href="/references/">Referencias</a></li> 44 + <li><a href="/xkcd/">xkcd</a></li> 45 + </ul> 46 + 47 + <h2>Formatos Numéricos</h2> 48 + <ul> 49 + <li><a href="/formats/binary/">Fracciones Binarias</a></li> 50 + <li><a href="/formats/fp/">Coma Flotante</a></li> 51 + <li><a href="/formats/exact/">Tipos Exactos</a></li> 52 + <li><a href="/formats/integer/">Sobre Usar Enteros</a></li> 53 + </ul> 54 + 55 + <h2>Errores</h2> 56 + <ul> 57 + <li><a href="/errors/rounding/">Redondeo</a></li> 58 + <li><a href="/errors/comparison/">Comparación</a></li> 59 + <li><a href="/errors/propagation/">Propagación</a></li> 60 + </ul> 61 + 62 + <h2>Cheat sheets</h2> 63 + <ul> 64 + <li><a href="/languages/csharp/">C#</a></li> 65 + <li><a href="/languages/java/">Java</a></li> 66 + <li><a href="/languages/javascript/">JavaScript</a></li> 67 + <li><a href="/languages/perl/">Perl</a></li> 68 + <li><a href="/languages/php/">PHP</a></li> 69 + <li><a href="/languages/python/">Python</a></li> 70 + <li><a href="/languages/sql/">SQL</a></li> 71 + </ul> 72 + </div> 73 + <a href="http://github.com/Juanlu001/floating-point-gui.de-ES"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a> 74 + 75 + </body> 76 + </html>