From grand-rapids-pm-list at pm.org Tue Mar 10 08:18:47 2009 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Tue, 10 Mar 2009 11:18:47 -0400 Subject: [grand-rapids-pm-list] Second grade math is giving me problems Message-ID: <7A35DE8C-3229-4300-B8E3-860D492ADE5B@stacksys.com> Hi all, I'm working with bit of code that is so basic and so simple that perhaps I can't see the easy explanation as to why I am not getting the expected output. I'm really baffled here. It simply adds or subtracts numbers from a account balance based on whether it is a deposit or withdraw. Simple enough, right? Well, i guess not for me... Here is the code: my @summary = (); $summary[0] = [ 1, 444.01 ]; $summary[1] = [ 2, 366.41 ]; $summary[2] = [ 1, 62.03 ]; $summary[3] = [ 2, 77.60 ]; $summary[4] = [ 1, 7.23 ]; $summary[5] = [ 2, 66.03 ]; $summary[6] = [ 1, 71.65 ]; $summary[7] = [ 2, 74.88 ]; my $balance = 0; foreach my $line (@summary) { my ($method, $amount) = @{$line}; print "Cur Balance: $balance "; if ($method == 1) { $balance = $balance + $amount; } elsif ($method == 2) { $balance = $balance - $amount; } print "New Balance: $balance\n"; } As you can likely determine, the @summary variable is an array of arrays; the later of which holds two values. The first value being 1 or 2. 1 for a deposit and 2 for a withdrawal. The second value being the amount in question. With an account balance starting at zero, evaluating these numbers should result in an ending balance of zero i.e. the total deposits are equal to the total withdrawals. However, while looping over the array, I'm getting funny results like long decimal values and a final balance of -1.42. Here is the output i'm seeing: Cur Balance: 0 New Balance: 444.01 Cur Balance: 444.01 New Balance: 77.6 Cur Balance: 77.6 New Balance: 139.63 Cur Balance: 139.63 New Balance: 62.03 Cur Balance: 62.03 New Balance: 69.26 Cur Balance: 69.26 New Balance: 3.22999999999998 Cur Balance: 3.22999999999998 New Balance: 74.88 Cur Balance: 74.88 New Balance: -1.4210854715202e-14 I suspect running this code through your perl interpreter would give you the same results. I'm convinced the problem is something simple, but I just don't see it. Anyone have any ideas? - Brian From grand-rapids-pm-list at pm.org Tue Mar 10 08:30:44 2009 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Tue, 10 Mar 2009 10:30:44 -0500 Subject: [grand-rapids-pm-list] Second grade math is giving me problems In-Reply-To: <7A35DE8C-3229-4300-B8E3-860D492ADE5B@stacksys.com> References: <7A35DE8C-3229-4300-B8E3-860D492ADE5B@stacksys.com> Message-ID: <0B60B47C-3767-4205-802C-B82A49CCC09F@petdance.com> On Mar 10, 2009, at 10:18 AM, grand-rapids-pm-list at pm.org wrote: > I suspect running this code through your perl interpreter would give > you the same results. I'm convinced the problem is something simple, > but I just don't see it. Anyone have any ideas? It's just the nature of floating-point numbers. Your last total is effectively zero, but it's a very very small number, so is not zero. What you may want to do is do integer arithmetic, and treat $7.14 as 714 pennies. Then you won't have these floating point problems. xoxo, Adny -- Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance From grand-rapids-pm-list at pm.org Tue Mar 10 11:03:46 2009 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Tue, 10 Mar 2009 14:03:46 -0400 Subject: [grand-rapids-pm-list] Second grade math is giving me problems In-Reply-To: <0B60B47C-3767-4205-802C-B82A49CCC09F@petdance.com> References: <7A35DE8C-3229-4300-B8E3-860D492ADE5B@stacksys.com> <0B60B47C-3767-4205-802C-B82A49CCC09F@petdance.com> Message-ID: <3737AD7B-C42A-495D-B97C-B56EB8866223@stacksys.com> On Mar 10, 2009, at 11:30 AM, grand-rapids-pm-list at pm.org wrote: > > On Mar 10, 2009, at 10:18 AM, grand-rapids-pm-list at pm.org wrote: > >> I suspect running this code through your perl interpreter would >> give you the same results. I'm convinced the problem is something >> simple, but I just don't see it. Anyone have any ideas? > > > It's just the nature of floating-point numbers. Your last total is > effectively zero, but it's a very very small number, so is not zero. > > What you may want to do is do integer arithmetic, and treat $7.14 as > 714 pennies. Then you won't have these floating point problems. Had to run out. Just reading this now. Thanks for the response. Got it working. Funny that i don't recall ever having this problem in the past. Maybe i don't do as much floating point arithmetic as I thought. Or maybe I never caught it ... :-/ - brian From grand-rapids-pm-list at pm.org Tue Mar 17 05:28:20 2009 From: grand-rapids-pm-list at pm.org (grand-rapids-pm-list at pm.org) Date: Tue, 17 Mar 2009 08:28:20 -0400 Subject: [grand-rapids-pm-list] YAPC|10 -- June 22 - 24 in Pittsburgh, PA Message-ID: <5cb4d9a00903170528m3d862edcscaf9f4c822184da9@mail.gmail.com> Mongers, This year will be the 10th anniversary of YAPC (Yet Another Perl Conference), the biggest and best Perl conference you'll find on American soil. No matter if you're new to Perl or have been immersed in it since version 4, YAPC has something that will jolt your neurons. Take the opportunity to be blown away by Damian Conway, laugh with Larry Wall, and spend time among your fellow Perl Mongers. Check out the website linked below for further details. What: YAPC|10 -- http://yapc10.org/ When: June 22 - 24 Where: CMU campus in Pittsburgh, PA Cost: $99 early registration until March 24 --Jason Porritt jasonporritt at gmail.com