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.

Traducidas secciones básicas

+53 -64
+35 -32
basic/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 - Basic Answers</title> 5 + <title>La Guía de la Coma Flotante - Respuestas Básicas</title> 6 6 7 7 <meta name="generator" content="nanoc 3.1.2"> 8 8 9 - <meta name="Description" content="Concise answers to common basic questions about floating-point math, like "Why don't my numbers add up?""> 9 + <meta name="Description" content="Respuestas concisas a preguntas básicas sobre aritmética de coma flotante, como «¿Por qué mis números no se suman bien?»"> 10 10 11 11 <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 12 <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> ··· 14 14 </head> 15 15 <body> 16 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> 17 + <h1>Respuestas Básicas</h1> 18 + <h3 id="por-qu-al-sumar-mis-nmeros-como-01--02-en-vez-de-dar-03-dan-un-resultado-extrao-como-030000000000000004">¿Por qué al sumar mis números, como 0.1 + 0.2, en vez de dar 0.3 dan un resultado extraño como 0.30000000000000004?</h3> 19 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> 20 + <p>Porque internamente, los ordenadores usan un formato (<a href="/formats/fp">coma flotante</a> <a href="/formats/binary">binario</a>) 21 + que no puede representar de forma precisa números como 0.1, 0.2 o 0.3 <em>de ninguna manera</em>.</p> 22 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> 23 + <p>Cuando el código es compilado o interpretado, tu “0.1” se redondea 24 + al número más cercano en ese formato, lo que resulta en un pequeño 25 + <a href="/errors/rounding/">error de redondeo</a> incluso antes de que se haga 26 + la operación.</p> 26 27 27 - <h3 id="why-do-computers-use-such-a-stupid-system">Why do computers use such a stupid system?</h3> 28 + <h3 id="por-qu-los-ordenadores-usan-un-sistema-tan-estpido">¿Por qué los ordenadores usan un sistema tan estúpido?</h3> 28 29 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> 30 + <p>No es estúpido, solo diferente. Los números decimales no pueden representar 31 + con precisión un número como ⅓, así que lo tienes que redondear a algo como 32 + 0.33 - y no esperas que 0.33 + 0.33 + 0.33 sea igual a 1 tampoco, ¿no?</p> 32 33 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> 34 + <p>Los ordenadores usan <a href="/formats/binary/">números binarios</a> porque son más 35 + rápidos de manejar, y porque para la mayoría de operaciones un error en la 36 + 17ª cifra decimal no importa en absoluto ya que los valores con los que 37 + trabajas no eran así de precisos de todas formas.</p> 37 38 38 - <h3 id="what-can-i-do-to-avoid-this-problem">What can I do to avoid this problem?</h3> 39 + <h3 id="qu-puedo-hacer-para-evitar-este-problema">¿Qué puedo hacer para evitar este problema?</h3> 39 40 40 - <p>That depends on what kind of calculations you’re doing.</p> 41 + <p>Eso depende del tipo de cálculos que estés haciendo.</p> 41 42 42 43 <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> 44 + <li>Si de verdad necesitas que tus resultados se sumen con exactitud, especialmente cuando trabajas con dinero: utiliza un <a href="/formats/exact/">tipo de datos decimal</a> especial.</li> 45 + <li>Si es solo que no quieres ver todos esos decimales extra: simplemente da formato a tu resultado redondeando a un número fijo de cifras decimales cuando lo presentes.</li> 46 + <li>Si no tienes un tipo de datos decimal, una alternativa es trabajar con <a href="/formats/integer/">enteros</a>, e.g. hacer todos los cálculos con dinero en céntimos. Pero esto requiere más trabajo y tiene algunas desventajas.</li> 46 47 </ul> 47 48 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 + <h3 id="por-qu-otros-clculos-como-01--04-s-funcionan-bien">¿Por qué otros cálculos como 0.1 + 0.4 sí funcionan bien?</h3> 50 + 51 + <p>En este caso, el resultado (0.5) <em>sí</em> puede ser representado de manera exacta como un 52 + número de coma flotante, y es posible que los errores de redondeo de los datos de partida 53 + se cancelen entre sí - aunque no se debería confiar excesivamente en esto (e.g. cuando 54 + esos dos números fueron almacenados en representaciones de coma flotante de diferente 55 + tamaño, los errores de redondeo pueden no cancelarse entre ellos).</p> 49 56 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> 57 + <p>En otros casos como 0.1 + 0.3, el resultado no es <em>realmente</em> 0.4, pero está lo suficientemente 58 + cerca como para que 0.4 sea el número más corto que está más cerca del resultado que cualquier 59 + otro número de coma flotante. La mayoría de lenguajes presentan ese número en vez de convertir 60 + el resultado real a una fracción decimal.</p> 55 61 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> 62 + <p>Si quieres más información, puedes acudir a las <a href="/references/">referencias</a>.</p> 59 63 60 - 61 64 <g:plusone href="http://comaflotante.org/"></g:plusone> 62 65 <div id="license"> 63 66 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
errors/comparison/index.html
··· 84 84 85 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 86 87 - 88 87 <g:plusone href="http://comaflotante.org/"></g:plusone> 89 88 <div id="license"> 90 89 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
errors/propagation/index.html
··· 42 42 and served as an inspiration for creating this website, mainly due to being a bit too detailed and 43 43 intimidating to programmers without a scientific background.</p> 44 44 45 - 46 45 <g:plusone href="http://comaflotante.org/"></g:plusone> 47 46 <div id="license"> 48 47 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
errors/rounding/index.html
··· 112 112 113 113 <p>More <a href="http://en.wikipedia.org/wiki/Rounding">rounding methods</a> can be found at Wikipedia.</p> 114 114 115 - 116 115 <g:plusone href="http://comaflotante.org/"></g:plusone> 117 116 <div id="license"> 118 117 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
formats/binary/index.html
··· 133 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 134 that some extra care and effort are necessary for applications that require <a href="/formats/exact/">decimal-like behaviour</a>.</p> 135 135 136 - 137 136 <g:plusone href="http://comaflotante.org/"></g:plusone> 138 137 <div id="license"> 139 138 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
formats/exact/index.html
··· 46 46 even many mathematicians work on problems where imprecise, numerical solutions are better because no 47 47 symbolic solution is known.</p> 48 48 49 - 50 49 <g:plusone href="http://comaflotante.org/"></g:plusone> 51 50 <div id="license"> 52 51 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
formats/fp/index.html
··· 132 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 133 </ul> 134 134 135 - 136 135 <g:plusone href="http://comaflotante.org/"></g:plusone> 137 136 <div id="license"> 138 137 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
formats/integer/index.html
··· 27 27 28 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 29 30 - 31 30 <g:plusone href="http://comaflotante.org/"></g:plusone> 32 31 <div id="license"> 33 32 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
+5 -2
index.html
··· 21 21 22 22 <h1 id="por-qu-mis-nmeros-no-se-suman-bien">¿Por qué mis números no se suman bien?</h1> 23 23 24 - <p>O sea que has escrito algún código absurdamente simple, como por ejemplo:</p> 24 + <p>O sea que has escrito algún código absurdamente simple, como por ejemplo<sup>1</sup>:</p> 25 25 26 26 <pre><code> 0.1 + 0.2 27 27 </code></pre> ··· 43 43 44 44 <p>Deberías ir a la sección de <a href="/basic/">Respuestas Básicas</a> primero - ¡pero no termines ahí!</p> 45 45 46 - 46 + <p><sup>1</sup> <small>Las Academias de la Lengua <a href="http://www.tex-tipografia.com/marca_decimal.html">recomiendan el punto como separador para las cifras 47 + decimales</a>, convergiendo así con 48 + la notación anglosajona que es la que se utiliza en la mayoría de lenguajes de programación.</small></p> 49 + 47 50 <g:plusone href="http://comaflotante.org/"></g:plusone> 48 51 <div id="license"> 49 52 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/csharp/index.html
··· 47 47 </li> 48 48 </ul> 49 49 50 - 51 50 <g:plusone href="http://comaflotante.org/"></g:plusone> 52 51 <div id="license"> 53 52 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/java/index.html
··· 73 73 </li> 74 74 </ul> 75 75 76 - 77 76 <g:plusone href="http://comaflotante.org/"></g:plusone> 78 77 <div id="license"> 79 78 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/javascript/index.html
··· 55 55 </li> 56 56 </ul> 57 57 58 - 59 58 <g:plusone href="http://comaflotante.org/"></g:plusone> 60 59 <div id="license"> 61 60 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/perl/index.html
··· 74 74 <li><a href="http://search.cpan.org/~flora/Math-BigInt-1.95/lib/Math/BigFloat.pm">Math::BigFloat extension</a></li> 75 75 </ul> 76 76 77 - 78 77 <g:plusone href="http://comaflotante.org/"></g:plusone> 79 78 <div id="license"> 80 79 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/php/index.html
··· 47 47 </li> 48 48 </ul> 49 49 50 - 51 50 <g:plusone href="http://comaflotante.org/"></g:plusone> 52 51 <div id="license"> 53 52 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/python/index.html
··· 59 59 <li><a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations">String formatting in Python</a></li> 60 60 </ul> 61 61 62 - 63 62 <g:plusone href="http://comaflotante.org/"></g:plusone> 64 63 <div id="license"> 65 64 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
-1
languages/sql/index.html
··· 51 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 52 </ul> 53 53 54 - 55 54 <g:plusone href="http://comaflotante.org/"></g:plusone> 56 55 <div id="license"> 57 56 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
+8 -10
references/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 - References</title> 5 + <title>La Guía de la Coma Flotante - Referencias</title> 6 6 7 7 <meta name="generator" content="nanoc 3.1.2"> 8 8 9 - <meta name="Description" content="Documents with more in-depth information about floating-point math"> 9 + <meta name="Description" content="Documentos con más información detallada sobre aritmética de coma flotante"> 10 10 11 11 <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 12 <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> ··· 14 14 </head> 15 15 <body> 16 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> 17 + <h1>Referencias</h1> 18 + <p>Documentos que contienen más información detallada sobre los temas tratados en este sitio:</p> 20 19 21 20 <ul> 22 - <li><a href="http://grouper.ieee.org/groups/754/">Homepage of the IEEE 754 standard</a></li> 21 + <li><a href="http://grouper.ieee.org/groups/754/">Página del estándar IEEE 754</a></li> 23 22 <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> 23 + <li><a href="http://www.cs.berkeley.edu/~wkahan/">Página de William Kahan</a> (arquitecto del estándar IEEE 754, muchos enlaces interesantes)</li> 25 24 <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> 25 + <li><a href="http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/">Comparing floating point numbers, 2012 edition</a></li> 26 + <li><a href="http://www.easysurf.cc/cnver17.htm">Herramienta para convertir números entre bases, incluyendo fracciones</a></li> 28 27 </ul> 29 28 30 - 31 29 <g:plusone href="http://comaflotante.org/"></g:plusone> 32 30 <div id="license"> 33 31 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia
+5 -6
xkcd/index.html
··· 6 6 7 7 <meta name="generator" content="nanoc 3.1.2"> 8 8 9 - <meta name="Description" content="How to mess with people who've learned to *expect* rounding errors in floating-point math."> 9 + <meta name="Description" content="Cómo fastidiar a la gente que ha aprendido a *esperar* errores de redondeo en aritmética de coma flotante."> 10 10 11 11 <link rel="stylesheet" type="text/css" href="/style.css" media="screen"> 12 12 <link rel="shortcut icon" href="/favicon.ico" type="image/vnd.microsoft.icon"> ··· 15 15 <body> 16 16 <div id="main"> 17 17 <h1>xkcd</h1> 18 - <h2 id="or">or</h2> 18 + <h2 id="o">o</h2> 19 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> 20 + <h1 id="cmo-fastidiar-a-la-gente-que-ha-aprendido-a-esperar-errores-de-redondeo-en-aritmtica-de-coma-flotante">Cómo fastidiar a la gente que ha aprendido a <em>esperar</em> errores de redondeo en aritmética de coma flotante.</h1> 21 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> 22 + <p><img src="http://imgs.xkcd.com/comics/e_to_the_pi_minus_pi.png" alt="Viñeta obligatoria de xkcd" title="Viñeta obligatoria de xkcd" /></p> 23 23 24 - <p>From <a href="http://www.xkcd.com/217/">xkcd</a></p> 24 + <p>De <a href="http://www.xkcd.com/217/">xkcd</a></p> 25 25 26 - 27 26 <g:plusone href="http://comaflotante.org/"></g:plusone> 28 27 <div id="license"> 29 28 <p>&copy; Publicado en <a href="http://comaflotante.org/">http://comaflotante.org/</a> bajo una licencia