Title: RE: Need help with what should be simple math
Thanks. But actually Lloyd answered it. I just needed the return value of the printf on a variable, so sprintf was needed. The problem was that I have a function that dissects the value but needs to treat the value as a string. Therefore, the final output of my function was '6.81' instead of the correct '0' value. So by doing an sprintf on it when it hits my formatting code it correctly enters with a '0.000' and from there I can work. But thanks for the help. I've really survived through perl thanks you everyone on this list.

Javier Moreno
==============
Softtek/GXS
EFS NearShore - TradeWeb 2nd Tier
...when you have eliminated the impossible, whatever remains, however improbable, must be the truth.
- Sherlock Holmes

-----Original Message-----
From: Haimov, Eugene [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 20, 2003 9:23 AM
To: Moreno, Javier (GXS, Softtek); $Bill Luebkert
Cc: Activeperl (E-mail)
Subject: RE: Need help with what should be simple math

I am not sure what you mean. You HAVE it in your variable: $thsum.
For all calculation purposes, its value is as good as zero -
try to add 1 for instance and see what the result would be.
If you want to force it to be strictly zero in this special case,
you can do:
 
$thsum = 0 if abs($thsum) < $Threshold;
 
where $Threshold has some very small preset value
that you are comfortable with.
 
Again -- if you discplose the REAL purpose of your calculations,
we could probably find some better way to solve the problem.
Eugene Haimov.
-----Original Message-----
From: Moreno, Javier (GXS, Softtek) [mailto:[EMAIL PROTECTED]]
Sent: Thursday, February 20, 2003 9:57 AM
To: $Bill Luebkert
Cc: Activeperl (E-mail)
Subject: RE: Need help with what should be simple math

And what if I want to store it on a variable. Maybe I missed something while explaining. I will reformat this number later on with a function that takes the value as a string to cut it off and reformat it. So how can I store the value of zero since it's .00000[manyzeros]681, which is really 0 as far as my precision goes. Because the printf will output the value but I need to capture it on a variable.

Thanks in advance...

Javier Moreno
==============
Softtek/GXS
EFS NearShore - TradeWeb 2nd Tier

...when you have eliminated the impossible, whatever remains, however improbable, must be the truth.
- Sherlock Holmes


-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 19, 2003 6:18 PM
To: Moreno, Javier (GXS, Softtek)
Cc: Activeperl (E-mail)
Subject: Re: Need help with what should be simple math


Moreno, Javier (GXS, Softtek) wrote:
> Hi all,
>
> I have the following code:
>
> *** SNIPPET
> $var1='-123456789012.34';
> $var2='-45.9999';
> $var3='0.03';
> $var4='-0.03';
> $var5='123456789012.34';
> $var6='45.9999';
>
> $tsum=0;
> $tsum=$var1+$var2+$var3+$var4+$var5+$var6;
> print "Total sum: $tsum\n";
> print "*********\n";
>
> $rsum=0;
> $rsum+=$var1;
> print "Running total: $rsum += $var1 = $rsum\n";
> $rsum+=$var2;
> print "Running total: $rsum += $var2 = $rsum\n";
> $rsum+=$var3;
> print "Running total: $rsum += $var3 = $rsum\n";
> $rsum+=$var4;
> print "Running total: $rsum += $var4 = $rsum\n";
> $rsum+=$var5;
> print "Running total: $rsum += $var5 = $rsum\n";
> $rsum+=$var6;
> print "Running total: $rsum += $var6 = $rsum\n";
> print "*************\n";
>
> $total=0;
> $thsum=0;
> $hsum{var1}=$var1;
> $hsum{var2}=$var2;
> $hsum{var3}=$var3;
> $hsum{var4}=$var4;
> $hsum{var5}=$var5;
> $hsum{var6}=$var6;
> foreach $total (sort values %hsum) {
>         print "Value: $total\n";
>         $thsum+=$total;
> }
> print "Sorted total: $thsum\n";
> ***SNIPPET
>
> If I do the calculation on an external calculator the value is 0. And
> even if you think about it, I have two numbers identical but each with
> negative and positive sign which should eliminate themselves, right?
> However if you run this code, you will see that the end result for all
> is: 6.81152343418034e-006
>
> Please help, I need to find out how to make this calculation work.

Change the last line to :

printf "Sorted total: %.3f\n", $thsum;



--
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

Reply via email to