Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by Roy Johnson (Monsignor)
on Dec 11, 2007 at 18:49 UTC ( [id://656450]=note: print w/replies, xml ) Need Help??


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

Revised: using %g doesn't work as expected
Round, then format. Here, I use sprintf with "%.5f" to round to five decimal places, and then use your "%7.5f" to format it.
print "print an example of negative zero\n"; my @range = (-0.0000001, -0.000001, -0.00001, -0.0001); foreach my $example (@range) { my $twice = sprintf "%.5f", $example; my $tret = sprintf("rounded float:%7.5f, other:%7.5e",$twice,$twic +e); print "The Number $example is represented as $tret\n"; }
Output:
print an example of negative zero The Number -1e-007 is represented as rounded float:0.00000, other:0.00 +000e+000 The Number -1e-006 is represented as rounded float:0.00000, other:0.00 +000e+000 The Number -1e-005 is represented as rounded float:-0.00001, other:-1. +00000e-005 The Number -0.0001 is represented as rounded float:-0.00010, other:-1. +00000e-004

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Negative zero? There's gotta be a sprintf that undoes that, right?
by rgiskard (Hermit) on Dec 11, 2007 at 19:47 UTC
    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
    
      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.
Re^2: Negative zero? There's gotta be a sprintf that undoes that, right?
by rgiskard (Hermit) on Dec 11, 2007 at 18:58 UTC

    Schweet! This solves my sprintf dilemma without additional subroutines (and then having to package + implement + distribute + maintain said subroutines).



    Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://656450]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found