Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: strange rounding behaviour

by riffraff (Pilgrim)
on May 18, 2001 at 20:35 UTC ( [id://81543]=note: print w/replies, xml ) Need Help??


in reply to Re: strange rounding behaviour
in thread strange rounding behaviour

Or, if you know exactly how many decimal places you need, you can scale it up by that magnitude, and use ints instead.

I.e., if you need 2 decimal places, use ints * 100, and print like this:

print ($a/100),".","($a%100),"\n";

Replies are listed 'Best First'.
Re: Re: Re: strange rounding behaviour
by srawls (Friar) on May 19, 2001 at 00:03 UTC
    I had written a well thought out reply to this, but then by browser crashed, so I'll just provide some points on why this script fails:

  • First of all, it's syntactically invalid (this, I assume, is just a type): you provide a " before ($a%100), I assume that isn't meant to be there. Also, you need to place parentheses around your print call so all of the items you want to send go to print.
  • Second, It doesn't account for the fact that the computer cannot store the floating point numbers accurately.
  • Third, it produces output like this: (when the typos are corrected)
    1.23.23 4.345.34 5.523.52
    You see, you print ($a/100), but if that is a decimal, then it will print (decimal and all!), and then the added decimal will print, not what you want.
  • Last, it doesn't round. The middle number should read 4.35 (after being rounded), but your code (ignoring the extra decimal and just reading from the last), makes it 4.34.

    The 15 year old, freshman programmer,
    Stephen Rawls
      Right, the " was a typo, and, as I mentioned, if you know the amount of decimal places you need, you scale up, and get rid of decimals entirely. That way you don't have rounding errors due to the inaccuracy of IEEE floating point representation, and it is quite a bit faster (well, it was way back when; I'm not sure now)

      And, yes, you are correct, it doesn't round. I wasn't giving a whole block of code to use, just a quick bit to give an example on how it would be done.

      I used to do this all the time, about 15 years ago, when most computers did not have FPU's, and were much more inaccurate.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found