Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Padding with sprintf changing number

by sachin raj aryan (Acolyte)
on Sep 27, 2021 at 12:29 UTC ( [id://11137033]=note: print w/replies, xml ) Need Help??


in reply to Re: Padding with sprintf changing number
in thread Padding with sprintf changing number

I am not sure how things changed please see my output 1348 4887.15 -----> this is the line i copy pasted no operation Checking amnt before conversion 4887.15, Checking amnt after rounding 4887.15, Checking amnt after conversion 488715, 488714 i m checking amount before padding, 0000000000488714 i m checking amount after padding, why it

print " Checking amnt before conversion $amt,\n"; $amt= sprintf("%.2f",$amt); print " Checking amnt after rounding $amt,\n"; $amt = $amt*100; print " Checking amnt after conversion $amt,\n"; sub amnt($amn) { my $amount=$_[0]; $amount=int($amount); say "$amount i m checking amount before padding,\n"; #my $padamnt = sprintf("%016.0f",$amount);---> currently commented to +check integer effect. my $padamnt = sprintf("%016d",$amount); say "$padamnt i m checking amount after padding,\n"; return $padamnt; }

Replies are listed 'Best First'.
Re^3: Padding with sprintf changing number
by syphilis (Archbishop) on Sep 27, 2021 at 14:26 UTC
    I've rewritten your code into a form that I think makes at least some sense:
    use strict; use warnings; my $amt = 4887.15; print " Checking amnt before conversion $amt,\n"; $amt= sprintf("%.2f",$amt); print " Checking amnt after rounding $amt,\n"; $amt = $amt*100; print " Checking amnt after conversion $amt,\n"; printf "Checking the EXACT value of amnt: %.17g\n", $amt; amnt($amt); sub amnt { my $amount=$_[0]; $amount=int($amount); print "$amount i m checking amount before padding,\n"; #my $padamnt = sprintf("%016.0f",$amount);---> currently commented to +check integer effect. my $padamnt = sprintf("%016d",$amount); print "$padamnt i m checking amount after padding,\n"; return $padamnt; }
    When I run that script, I get:
    Checking amnt before conversion 4887.15, Checking amnt after rounding 4887.15, Checking amnt after conversion 488715, Checking the EXACT value of amnt: 488714.99999999994 488714 i m checking amount before padding, 0000000000488714 i m checking amount after padding,
    Note that I've added a line of code that demonstrates that the EXACT value of $amt is NOT 488715.
    Perl's print() function frequently fails to produce an accurate representation of floating point values - and that's what is happening here.

    That output I'm seeing is as I expect.
    Which is the line of output that you don't understand ?

    You need to stay alert to this aspect of perl's print() function.
    Neither python3 nor raku are afflicted with such a poorly designed implementation:
    $ python3 -c "print(4887.15 * 100)" 488714.99999999994 $ raku -e "say 4887.15e0 * 100" 488714.99999999994


    Cheers,
    Rob
      > Neither python3 nor raku

      But

      $ python2 -c 'print(4887.15 * 100)' 488715.0

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Yes, I had good reason to avoid mentioning python2's print() implementation ;-)
        It apparently rounds to even fewer decimal digits than perl5:
        $ python2 -c 'print 2 ** 0.5' 1.41421356237 $ perl -le 'print 2 ** 0.5' 1.4142135623731
        At least they've done something about it.
        Maybe perl7 will do the same.

        Cheers,
        Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-23 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found