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.

Added linebreaks due to change in kramdown syntax

+39 -2
+2
content/languages/csharp.html
··· 6 6 Floating-Point Types 7 7 -------- 8 8 C# has [IEEE 754](/formats/fp/) single and double precision types supported by keywords: 9 + 9 10 float f = 0.1f; // 32 bit float, note f suffix 10 11 double d = 0.1d; // 64 bit float, suffix optional 11 12 ··· 14 15 ------------- 15 16 C# has a 128 bit [limited-precision](/formats/exact/) decimal type denoted by the keyword 16 17 <code>decimal</code>: 18 + 17 19 decimal myMoney = 300.1m; // note m suffix on the literal 18 20 19 21
+10
content/languages/java.html
··· 6 6 Floating-Point Types 7 7 -------- 8 8 Java has [IEEE 754](/formats/fp/) single and double precision types supported by keywords: 9 + 9 10 float f = 0.1f; // 32 bit float, note f suffix 10 11 double d = 0.1d; // 64 bit float, suffix optional 12 + 11 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. 12 14 13 15 Decimal Types 14 16 ------------- 15 17 Java has an [arbitrary-precision](/formats/exact/) decimal type named <code>java.math.BigDecimal</code>, which 16 18 also allows to choose the [rounding mode](/errors/rounding/). 19 + 17 20 BigDecimal a = new BigDecimal("0.1"); 18 21 BigDecimal b = new BigDecimal("0.2"); 19 22 BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3 ··· 22 25 How to Round 23 26 ------------ 24 27 To get a String: 28 + 25 29 String.format("%.2f", 1.2399) // returns "1.24" 26 30 String.format("%.3f", 1.2399) // returns "1.240" 27 31 String.format("%.2f", 1.2) // returns "1.20" 32 + 28 33 To print to standard output (or any <code>PrintStream</code>): 34 + 29 35 System.out.printf("%.2f", 1.2399) // same syntax as String.format() 36 + 30 37 If you don't want trailing zeroes: 38 + 31 39 new DecimalFormat("0.00").format(1.2)// returns "1.20" 32 40 new DecimalFormat("0.##").format(1.2)// returns "1.2" 41 + 33 42 If you need a specific [rounding mode](/errors/rounding/): 43 + 34 44 new BigDecimal("1.25").setScale(1, RoundingMode.HALF_EVEN); // returns 1.2 35 45 36 46
+5
content/languages/javascript.html
··· 6 6 Floating-Point Types 7 7 -------- 8 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 + 9 10 var num = parseFloat("3.5"); 10 11 11 12 Decimal Types 12 13 ------------- 13 14 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 + 15 17 var a = new BigDecimal("0.01"); 16 18 var b = new BigDecimal("0.02"); 17 19 var c = a.add(b); // 0.03 ··· 20 22 21 23 How to Round 22 24 ------------ 25 + 23 26 var num = 5.123456; 24 27 num.toPrecision(1) //returns 5 as string 25 28 num.toPrecision(2) //returns 5.1 as string 26 29 num.toPrecision(4) //returns 5.123 as string 30 + 27 31 Using a specific rounding mode: 32 + 28 33 new BigDecimal("1.25").setScale(1, BigDecimal.prototype.ROUND_HALF_UP); 29 34 30 35
+11
content/languages/perl.html
··· 12 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 13 14 14 The Math::BigFloat extension provides an arbitrary-precision [decimal type](/formats/exact/): 15 + 15 16 use Math::BigFloat ':constant' 16 17 my $f = 0.1 + 0.2; # returns exactly 0.3 18 + 17 19 The Number::Fraction extension provides a fraction type that overloads the arithmetic operators with [symbolic](/formats/exact/) fraction arithmetic: 20 + 18 21 use Number::Fraction ':constants'; 19 22 my $f = '1/2' - '1/3'; # returns 1/6 23 + 20 24 The Math::BigRat extension provides similar functionality. Its advantage is compatibility with the Math::BigInt and Math::BigFloat extensions, but it does not seem to support fraction literals. 21 25 22 26 How to Round 23 27 ------------ 24 28 To get a string: 29 + 25 30 $result = sprintf("%.2f", 1.2345); # returns 1.23 31 + 26 32 To format output: 33 + 27 34 printf("%.2f", 1.2); # prints 1.20 35 + 28 36 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. 29 37 30 38 The Math::Round extension provides various functions for rounding floating-point values: 39 + 31 40 use Math::Round qw(:all); 32 41 $result = nearest(.1, 4.567) # prints 4.6 33 42 $result = nearest(.01, 4.567) # prints 4.57 43 + 34 44 The Math::BigFloat extension also supports various [rounding modes](/errors/rounding/): 45 + 35 46 use Math::BigFloat; 36 47 my $n = Math::BigFloat->new(123.455); 37 48 my $f1 = $n->round('','-2','common'); # returns 123.46
+3
content/languages/php.html
··· 6 6 Floating-Point Types 7 7 -------- 8 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 + 9 10 $foo = 0 + "10.5"; 10 11 11 12 12 13 Decimal Types 13 14 ------------- 14 15 The BC Math extension implements [arbitrary-precision](/formats/exact/) decimal math: 16 + 15 17 $a = '0.1'; 16 18 $b = '0.2'; 17 19 echo bcadd($a, $b); // prints 0.3 ··· 19 21 How to Round 20 22 ------------ 21 23 Rounding can be done with the `number_format()` function: 24 + 22 25 $number = 4.123; 23 26 echo number_format($number, 2); // prints 4.12 24 27
+8 -2
content/languages/python.html
··· 7 7 -------- 8 8 Almost all platforms map Python floats to [IEEE 754](/formats/fp/) 9 9 double precision. 10 + 10 11 f = 0.1 11 12 12 13 Decimal Types 13 14 ------------- 14 - Python has an [arbitrary-precision](/formats/exact/) decimal type named <code>Decimal</code> in the <code>decimal</code> module, which 15 - also allows to choose the [rounding mode](/errors/rounding/). 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 + 16 17 a = Decimal('0.1') 17 18 b = Decimal('0.2') 18 19 c = a + b # returns a Decimal representing exactly 0.3 ··· 20 21 How to Round 21 22 ------------ 22 23 To get a string: 24 + 23 25 "%.2f" % 1.2399 # returns "1.24" 24 26 "%.3f" % 1.2399 # returns "1.240" 25 27 "%.2f" % 1.2 # returns "1.20" 28 + 26 29 To print to standard output: 30 + 27 31 print "%.2f" % 1.2399 # just use print and string formatting 32 + 28 33 Specific [rounding modes](/errors/rounding/) and other parameters can be defined in a Context object: 34 + 29 35 getcontext().prec = 7 30 36 31 37 Resources