Perl Sprintf Rounding Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Perl Int To Float
About Us Learn more about Stack Overflow the company Business Learn more about perl string to float hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Perl Floating Point
Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to eliminate perl floating point division Perl rounding errors up vote 3 down vote favorite Consider the following program: $x=12345678901.234567000; $y=($x-int($x))*1000000000; printf("%f:%f\n",$x,$y); Here's what is prints: 12345678901.234568:234567642.211914 I was expecting: 12345678901.234567:234567000 This appears to be some sort of rounding issue in Perl. How could I change it to get 234567000 instead? Did I do something wrong? perl rounding share|improve this question edited Jul 14 '10 at 3:19 Brad Gilbert 15.8k74788 asked perl bigfloat Jul 12 '10 at 22:56 User1 12.7k39144205 add a comment| 5 Answers 5 active oldest votes up vote 5 down vote accepted Make "use bignum;" the first line of your program. Other answers explain what to expect when using floating point arithmetic -- that some digits towards the end are not really part of the answer. This is to make the computations do-able in a reasonable amount of time and space. If you are willing to use unbounded time and space to work with numbers, then you can use arbitrary-precision numbers and math, which is what "use bignum" enables. It's slower and uses more memory, but it works like math you learned in elementary school. In general, it's best to learn more about how floating point math works before converting your program to arbitrary-precision math. It's only needed in very strange situations. share|improve this answer answered Jul 12 '10 at 23:13 jrockway 30.4k55281 3 By "It's only needed in very strange situations" you're referring to money as a very strange situation right? –Evan Plaice Jul 12 '10 at 23:16 4 @Evan Plaice: Money isn't strange; it's (usually) integer numbers of cent
CoolUsesForPerl PerlNews Q&A Tutorials Poetry RecentThreads NewestNodes Donate What'sNew on Jul 03, 2001 at 00:06UTC ( #93305=perlquestion: print w/replies, xml ) Need Help?? Anonymous Monk has asked for the
Perl Force Floating Point Division
wisdom of the Perl Monks concerning the following question: Is there perl division a problem with sprintf when rounding on exactly .5 Below is my code: $a = 2.815;
Perl Bignum
$a = sprintf("%.2f", $a); printf "a =".$a."---\n"; [download] Result: 2.81 Shouldn't it show 2.82? What can I do to get around this? Is this a known problem http://stackoverflow.com/questions/3232921/how-to-eliminate-perl-rounding-errors with perl? Any help is much appreciated! Comment on Rounding problem with sprintf??Download Code Replies are listed 'Best First'. Re: Rounding problem with sprintf?? by japhy (Canon) on Jul 03, 2001 at 00:46UTC This is a known "bug". It happens like that in C as well. Write your own rounding function, or use this one: http://www.perlmonks.org/?node_id=93305 # round(789.456,-2) => 800 # round(789.456,-1) => 790 # round(789.456) => 789 # round(789.456,1) => 789.5 # round(789.456,2) => 789.46 sub round { my ($n, $p) = @_; my $sign = ($n > 0) ? 1 : -1; $p ||= 0; $n *= 10 ** $p; $n = int($n + .5 * $sign); return $n / 10**$p; } [download] japhy -- Perl and Regex Hacker[reply][d/l] Re^2: Rounding problem with sprintf?? by Anonymous Monk on Sep 04, 2014 at 07:26UTC This sub has the same bug as sprintf. round(5.245,2) returns 5.25 but round(9.325,2) returns 9.32[reply][d/l][select] Re^3: Rounding problem with sprintf?? by AnomalousMonk (Canon) on Sep 04, 2014 at 15:56UTC But again, the root 'bug' is in floating point, which cannot exactly represent the numbers you give. See What Every Computer Scientist Should Know About Floating-Point Arithmetic. c:\@Work\Perl>perl -wMstrict -le "for my $f (5.245, 9.325) { print sprintf '%.20f', $f; } " 5.24500000000000010000 9.32499999999999930000 [download] Update: Slight cosmetic change to code example. [re
the builtin arithmetic operators will convert them to integer or floating-point values to perform the http://floating-point-gui.de/languages/perl/ operation. The Math::BigFloat extension provides an arbitrary-precision decimal type: use Math::BigFloat ':constant' my $f = 0.1 + 0.2; # returns exactly 0.3 The http://www.exploringbinary.com/inconsistent-rounding-of-printed-floating-point-numbers/ Number::Fraction extension provides a fraction type that overloads the arithmetic operators with symbolic fraction arithmetic: use Number::Fraction ':constants'; my $f = '1/2' - '1/3'; floating point # returns 1/6 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. How to Round To get a string: $result = sprintf("%.2f", 1.2345); # returns 1.23 To format output: printf("%.2f", 1.2); # perl floating point prints 1.20 Note that this implicitly uses round-to-even. The variable $# contains the default format for printing numbers, but its use is considered deprecated. The Math::Round extension provides various functions for rounding floating-point values: use Math::Round qw(:all); $result = nearest(.1, 4.567) # prints 4.6 $result = nearest(.01, 4.567) # prints 4.57 The Math::BigFloat extension also supports various rounding modes: use Math::BigFloat; my $n = Math::BigFloat->new(123.455); my $f1 = $n->round('','-2','common'); # returns 123.46 my $f2 = $n->round('','-2','zero'); # returns 123.45 Resources Semantics of numbers and numeric operations in Perl sprintf function Math::Round extension Number::Fraction extension Math::BigRat extension Math::BigFloat extension © Published at floating-point-gui.de under the Creative Commons Attribution License (BY) The Floating-Point Guide Home Basic Answers References xkcd Number Formats Binary Fractions Floating-Point Exact Types On Using Integers Errors Rounding Comparison Propagation Languagecheat sheets C# Java JavaScript Perl PHP Python Ruby SQL
Topics Sitemap About Contact Inconsistent Rounding of Printed Floating-Point Numbers What does this C program print? #include