in reply to Re: Negative zero? There's gotta be a sprintf that undoes that, right?
in thread Negative zero? There's gotta be a sprintf that undoes that, right?

No wait, I'm not getting the same output using the rounding twice solution. Roy Johnson... what version of perl are you using? I'm on 5.8.5, RHLinux
print "print an example of negative zero\n"; @range = (-0.0000001, -0.000001, -0.00001, -0.0001); foreach $example (@range) { my $preround = sprintf ("%.5f",$example); my $tret = sprintf("rounded float:%7.5f \n",$preround); print "The Number $example is represented as $tret, here's the rou +nded num $preround\n"; }
Output:
print an example of negative zero
The Number -1e-07 is represented as rounded float:-0.00000 
, here's the rounded num -0.00000
The Number -1e-06 is represented as rounded float:-0.00000 
, here's the rounded num -0.00000
The Number -1e-05 is represented as rounded float:-0.00001 
, here's the rounded num -0.00001
The Number -0.0001 is represented as rounded float:-0.00010 
, here's the rounded num -0.00010
  • Comment on Re^2: Negative zero? There's gotta be a sprintf that undoes that, right?
  • Download Code

Replies are listed 'Best First'.
Re^3: Negative zero? There's gotta be a sprintf that undoes that, right?
by Roy Johnson (Monsignor) on Dec 12, 2007 at 19:17 UTC
    That's weird. I'm using ActivePerl 5.8.1. But I just tried it on 5.8.6 for Solaris, and got your result there. The fix is to add zero:
    my $preround = 0 + sprintf ("%.5f",$example);

    Caution: Contents may have been coded under pressure.