From michael at potter.name Fri Apr 27 18:54:33 2012 From: michael at potter.name (Michael Potter) Date: Fri, 27 Apr 2012 21:54:33 -0400 Subject: [Chicago-talk] (no subject) Message-ID: Monks, I am looking for a graceful way to round numbers. The following program #!/bin/perl sub myround { my $src = shift; printf("%.2f\n", $src); } myround("1.335"); myround("0.335"); outputs this: $ perl ./round.pl 1.33 0.34 I know why. It has to do with the well known side effects of using floating point. What I don't know is a graceful way to round. I would like to use something that already comes with perl and void adding any modules. -- potter From joel.a.berger at gmail.com Fri Apr 27 21:09:20 2012 From: joel.a.berger at gmail.com (Joel Berger) Date: Fri, 27 Apr 2012 23:09:20 -0500 Subject: [Chicago-talk] (no subject) In-Reply-To: References: Message-ID: Perhaps I'm mistaken about something, but it should "round half to even". Still, if you look up round in perlfaq4 it gives a little more information about what you are seeing (perldoc -q round). You may have to take its advice and implement your own rounding algorithm, but I'm sure there must be something on CPAN for this. Joel On Fri, Apr 27, 2012 at 8:54 PM, Michael Potter wrote: > Monks, > > I am looking for a graceful way to round numbers. > > The following program > #!/bin/perl > > sub myround > { > my $src = shift; > printf("%.2f\n", $src); > } > > myround("1.335"); > myround("0.335"); > > outputs this: > $ perl ./round.pl > 1.33 > 0.34 > > I know why. It has to do with the well known side effects of using > floating point. > > What I don't know is a graceful way to round. > > I would like to use something that already comes with perl and void > adding any modules. > > -- > potter > _______________________________________________ > Chicago-talk mailing list > Chicago-talk at pm.org > http://mail.pm.org/mailman/listinfo/chicago-talk > -------------- next part -------------- An HTML attachment was scrubbed... URL: From imranjj at gmail.com Fri Apr 27 21:26:35 2012 From: imranjj at gmail.com (imran javaid) Date: Fri, 27 Apr 2012 23:26:35 -0500 Subject: [Chicago-talk] (no subject) In-Reply-To: References: Message-ID: I haven't tested this for all cases, but this should fix the issue for most cases: sub myround2 { my $src = shift; my $pre = shift; print int($src * 10**$pre + .5)/100; print "\n"; } myround2("1.335",2); myround2("0.335",2); myround2("1.334999999999999",2); myround2("0.334999999999999",2); -imran On Fri, Apr 27, 2012 at 8:54 PM, Michael Potter wrote: > Monks, > > I am looking for a graceful way to round numbers. > > The following program > #!/bin/perl > > sub myround > { > my $src = shift; > printf("%.2f\n", $src); > } > > myround("1.335"); > myround("0.335"); > > outputs this: > $ perl ./round.pl > 1.33 > 0.34 > > I know why. It has to do with the well known side effects of using > floating point. > > What I don't know is a graceful way to round. > > I would like to use something that already comes with perl and void > adding any modules. > > -- > potter > _______________________________________________ > Chicago-talk mailing list > Chicago-talk at pm.org > http://mail.pm.org/mailman/listinfo/chicago-talk > -------------- next part -------------- An HTML attachment was scrubbed... URL: From imranjj at gmail.com Fri Apr 27 21:29:36 2012 From: imranjj at gmail.com (imran javaid) Date: Fri, 27 Apr 2012 23:29:36 -0500 Subject: [Chicago-talk] (no subject) In-Reply-To: References: Message-ID: doesn't work for negative number, btw. need to check the sign in that case. On Fri, Apr 27, 2012 at 11:26 PM, imran javaid wrote: > I haven't tested this for all cases, but this should fix the issue for > most cases: > > sub myround2 { > my $src = shift; > my $pre = shift; > print int($src * 10**$pre + .5)/100; > print "\n"; > } > > myround2("1.335",2); > myround2("0.335",2); > myround2("1.334999999999999",2); > myround2("0.334999999999999",2); > > -imran > > On Fri, Apr 27, 2012 at 8:54 PM, Michael Potter wrote: > >> Monks, >> >> I am looking for a graceful way to round numbers. >> >> The following program >> #!/bin/perl >> >> sub myround >> { >> my $src = shift; >> printf("%.2f\n", $src); >> } >> >> myround("1.335"); >> myround("0.335"); >> >> outputs this: >> $ perl ./round.pl >> 1.33 >> 0.34 >> >> I know why. It has to do with the well known side effects of using >> floating point. >> >> What I don't know is a graceful way to round. >> >> I would like to use something that already comes with perl and void >> adding any modules. >> >> -- >> potter >> _______________________________________________ >> Chicago-talk mailing list >> Chicago-talk at pm.org >> http://mail.pm.org/mailman/listinfo/chicago-talk >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at potter.name Sat Apr 28 06:39:36 2012 From: michael at potter.name (Michael Potter) Date: Sat, 28 Apr 2012 09:39:36 -0400 Subject: [Chicago-talk] (no subject) In-Reply-To: References: Message-ID: I ended up using the Math::Decimal module: my $rounded = dec_round("NEAR_AWZ", $unrounded, "0.01"); Limited testing indicated that that matched the rounding of excel. AWZ means away from zero. On Sat, Apr 28, 2012 at 12:29 AM, imran javaid wrote: > doesn't work for negative number, btw. need to check the sign in that case. > > > On Fri, Apr 27, 2012 at 11:26 PM, imran javaid wrote: >> >> I haven't tested this for all cases, but this should fix the issue for >> most cases: >> >> sub myround2 { >> ? my $src = shift; >> ? my $pre = shift; >> ? print int($src * 10**$pre + .5)/100; >> ? print "\n"; >> } >> >> myround2("1.335",2); >> myround2("0.335",2); >> myround2("1.334999999999999",2); >> myround2("0.334999999999999",2); >> >> -imran >> >> On Fri, Apr 27, 2012 at 8:54 PM, Michael Potter >> wrote: >>> >>> Monks, >>> >>> I am looking for a graceful way to round numbers. >>> >>> The following program >>> #!/bin/perl >>> >>> sub myround >>> { >>> ? my $src = shift; >>> ? printf("%.2f\n", $src); >>> } >>> >>> myround("1.335"); >>> myround("0.335"); >>> >>> outputs this: >>> $ perl ./round.pl >>> 1.33 >>> 0.34 >>> >>> I know why. ?It has to do with the well known side effects of using >>> floating point. >>> >>> What I don't know is a graceful way to round. >>> >>> I would like to use something that already comes with perl and void >>> adding any modules. >>> >>> -- >>> potter >>> _______________________________________________ >>> Chicago-talk mailing list >>> Chicago-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/chicago-talk >> >> > > > _______________________________________________ > Chicago-talk mailing list > Chicago-talk at pm.org > http://mail.pm.org/mailman/listinfo/chicago-talk -- Michael Potter ? Tapp Solutions, LLC ? Replatform Technologies, LLC +1 770 815 6142 ?** Atlanta?** michael at potter.name ?** www.linkedin.com/in/michaelpotter From danel at speakeasy.net Sat Apr 28 13:14:40 2012 From: danel at speakeasy.net (Alexander Danel) Date: Sat, 28 Apr 2012 15:14:40 -0500 Subject: [Chicago-talk] (no subject) In-Reply-To: References: Message-ID: <033c01cd257b$8b331bc0$a1995340$@speakeasy.net> The correct way to round numbers in Perl is "printf()" and "sprintf()". Note that rounding is something that makes sense to humans but not to computers. Computers are binary, they don't care about our decimal concepts, unless we ask them to. The times when decimal numbers matter is during input (from a human) and output (for a human.) Therefore, it is appropriate that the Perl method for rounding is an output (or string) function. Alexander Danel -----Original Message----- From: chicago-talk-bounces+danel=speakeasy.net at pm.org [mailto:chicago-talk-bounces+danel=speakeasy.net at pm.org] On Behalf Of Michael Potter Sent: Friday, April 27, 2012 8:55 PM To: Chicago.pm chatter Subject: [Chicago-talk] (no subject) Monks, I am looking for a graceful way to round numbers. The following program #!/bin/perl sub myround { my $src = shift; printf("%.2f\n", $src); } myround("1.335"); myround("0.335"); outputs this: $ perl ./round.pl 1.33 0.34 I know why. It has to do with the well known side effects of using floating point. What I don't know is a graceful way to round. I would like to use something that already comes with perl and void adding any modules. -- potter _______________________________________________ Chicago-talk mailing list Chicago-talk at pm.org http://mail.pm.org/mailman/listinfo/chicago-talk From dcmertens.perl at gmail.com Sat Apr 28 13:27:58 2012 From: dcmertens.perl at gmail.com (David Mertens) Date: Sat, 28 Apr 2012 15:27:58 -0500 Subject: [Chicago-talk] (no subject) In-Reply-To: <033c01cd257b$8b331bc0$a1995340$@speakeasy.net> References: <033c01cd257b$8b331bc0$a1995340$@speakeasy.net> Message-ID: On Sat, Apr 28, 2012 at 3:14 PM, Alexander Danel wrote: > The correct way to round numbers in Perl is "printf()" and "sprintf()". > > Note that rounding is something that makes sense to humans but not to > computers. Computers are binary, they don't care about our decimal > concepts, unless we ask them to. The times when decimal numbers matter is > during input (from a human) and output (for a human.) Therefore, it is > appropriate that the Perl method for rounding is an output (or string) > function. > > Alexander Danel > Your observation that rounding really only makes sense with a string output is a good point that I had never quite realized. However, printf and sprintf do not give you much control over the rounding details (which, though esoteric, are important). In case you missed it, Michael posted that he found a CPAN module that gave him exactly what he needed, called Math::Decimal. David -----Original Message----- > From: chicago-talk-bounces+danel=speakeasy.net at pm.org > [mailto:chicago-talk-bounces+danel=speakeasy.net at pm.org] On Behalf Of > Michael Potter > Sent: Friday, April 27, 2012 8:55 PM > To: Chicago.pm chatter > Subject: [Chicago-talk] (no subject) > > Monks, > > I am looking for a graceful way to round numbers. > > The following program > #!/bin/perl > > sub myround > { > my $src = shift; > printf("%.2f\n", $src); > } > > myround("1.335"); > myround("0.335"); > > outputs this: > $ perl ./round.pl > 1.33 > 0.34 > > I know why. It has to do with the well known side effects of using > floating > point. > > What I don't know is a graceful way to round. > > I would like to use something that already comes with perl and void adding > any modules. > > -- > potter > _______________________________________________ > Chicago-talk mailing list > Chicago-talk at pm.org > http://mail.pm.org/mailman/listinfo/chicago-talk > > _______________________________________________ > Chicago-talk mailing list > Chicago-talk at pm.org > http://mail.pm.org/mailman/listinfo/chicago-talk > -- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -- Brian Kernighan -------------- next part -------------- An HTML attachment was scrubbed... URL: From andy at petdance.com Sat Apr 28 19:06:58 2012 From: andy at petdance.com (Andy Lester) Date: Sat, 28 Apr 2012 21:06:58 -0500 Subject: [Chicago-talk] (no subject) In-Reply-To: References: <033c01cd257b$8b331bc0$a1995340$@speakeasy.net> Message-ID: <924E8ADB-15CB-4138-B60C-36658330627E@petdance.com> On Apr 28, 2012, at 3:27 PM, David Mertens wrote: > On Sat, Apr 28, 2012 at 3:14 PM, Alexander Danel wrote: > The correct way to round numbers in Perl is "printf()" and "sprintf()". perldoc -q round perldoc -q says "Search the FAQ". xoa -- Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance -------------- next part -------------- An HTML attachment was scrubbed... URL: