CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Odd rounding issue.

 



lightspd
Novice

Mar 25, 2013, 4:38 PM

Post #1 of 5 (515 views)
Odd rounding issue. Can't Post

I've got the following function, wasn't written by me, just being used in the code. For some reason it rounds 29 and 58 down 1, to 28 and 57, but only if I pass it the hash value, if I pass it 29 directly works fine.


Code
sub extFmt 
{
my ($v, $dbug) = @_;
my $decimalPlaces =2;
my $tmp;
$v = sprintf "%" . ($decimalPlaces + 1) . "." . ($decimalPlaces + 1) ."d\n", $v;
$v =~ /(-?)(\d*)(\d{$decimalPlaces})$/;
$tmp = "$1$2.$3";
return $tmp;
}


if I do a print Dumper on the values, the only thing I notice is the 29 is in single quotes, but so are other numbers randomly and they work. Any suggestions? I do know it's sprintf doing it, based on dumping the values, but I'm at a loss as to why.

Edit:
I think I'm just not going to call this function. I'm not sure why they did in the first place. If someone knows why sprintf is treating 29 like a floating point number and why it rounds down, it would be interesting to know.


(This post was edited by lightspd on Mar 25, 2013, 6:02 PM)


Kenosis
User

Mar 25, 2013, 6:08 PM

Post #2 of 5 (503 views)
Re: [lightspd] Odd rounding issue. [In reply to] Can't Post

I couldn't replicate the issue you've described--even with a hash--but I may be sending the wrong vals to the sub. Also, the following produces the same results as that sub:


Code
sub extFmt2 { 
return sprintf '%.2f', $_[0] / 100;
}


However, I don't know that this is too helpful to you. I'd be interested to see how you get it to produce the rounding errors, though...


lightspd
Novice

Mar 25, 2013, 6:46 PM

Post #3 of 5 (501 views)
Re: [Kenosis] Odd rounding issue. [In reply to] Can't Post

Yeah, I'm still not sure why they did all that in the function, but figured I'd leave it as is, because they are calling it from other places.

I'm not sure how I'm getting to the point I am either, I've checked all the math and it shouldn't be anything weird but if you want to reproduce, do this.


Code
my $x = 29; 

$x = sprintf("%0.2f\n", $x/100);
my $y = $x*100;

$y = sprintf("%3.3d\n", $y);
print "$x\n$y";



Kenosis
User

Mar 25, 2013, 8:02 PM

Post #4 of 5 (493 views)
Re: [lightspd] Odd rounding issue. [In reply to] Can't Post

You're demonstrating an internal rounding error when moving from a floating-point representation to an integer. Place the following at the top of your script:


Code
use bignum;


Output from your code when using this module:


Code
0.29 

029


Output without it:


Code
0.29 

028


Here's the bignum documentation. Or print the value as a float--not an integer--without using the module bignum:


Code
$y = sprintf("%03.f\n", $y);



(This post was edited by Kenosis on Mar 25, 2013, 8:09 PM)


lightspd
Novice

Mar 26, 2013, 6:14 AM

Post #5 of 5 (483 views)
Re: [Kenosis] Odd rounding issue. [In reply to] Can't Post

Cheers. Thanks for letting me know about bignum.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives