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.

add Perl cheat sheet

+49
+48
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 Math::BigFloat extension provides an arbitrary-precision [decimal type](/formats/exact/): 15 + use Math::BigFloat ':constant' 16 + my $f = 0.1 + 0.2; # returns exactly 0.3 17 + The Number::Fraction extension provides a fraction type that overloads the arithmetic operators with [symbolic](/formats/exact/) fraction arithmetic: 18 + use Number::Fraction ':constants'; 19 + my $f = '1/2' - '1/3'; # returns 1/6 20 + 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 + 22 + How to Round 23 + ------------ 24 + To get a string: 25 + $result = sprintf("%.2f", 1.2345); # returns 1.23 26 + To format output: 27 + printf("%.2f", 1.2); # prints 1.20 28 + 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 + 30 + The Math::Round extension provides various functions for rounding floating-point values: 31 + use Math::Round qw(:all); 32 + $result = nearest(.1, 4.567) # prints 4.6 33 + $result = nearest(.01, 4.567) # prints 4.57 34 + The Math::BigFloat extension also supports various [rounding modes](/errors/rounding/): 35 + use Math::BigFloat; 36 + my $n = Math::BigFloat->new(123.455); 37 + my $f1 = $n->round('','-2','common'); # returns 123.46 38 + my $f2 = $n->round('','-2','zero'); # returns 123.45 39 + 40 + Resources 41 + --------- 42 + * [Semantics of numbers and numeric operations in Perl](http://perldoc.perl.org/perlnumber.html) 43 + * [sprintf function](http://perldoc.perl.org/functions/sprintf.html) 44 + * [Math::Round extension](http://search.cpan.org/dist/Math-Round/Round.pm) 45 + * [Number::Fraction extension](http://search.cpan.org/~davecross/Number-Fraction-1.13/lib/Number/Fraction.pm) 46 + * [Math::BigRat extension](http://search.cpan.org/~flora/Math-BigRat-0.26/lib/Math/BigRat.pm) 47 + * [Math::BigFloat](http://search.cpan.org/~flora/Math-BigInt-1.95/lib/Math/BigFloat.pm) 48 +
+1
layouts/default.html
··· 53 53 <li><a href="/languages/csharp/">C#</a></li> 54 54 <li><a href="/languages/java/">Java</a></li> 55 55 <li><a href="/languages/javascript/">JavaScript</a></li> 56 + <li><a href="/languages/perl/">Perl</a></li> 56 57 <li><a href="/languages/php/">PHP</a></li> 57 58 <li><a href="/languages/python/">Python</a></li> 58 59 <li><a href="/languages/sql/">SQL</a></li>