Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Printf/Sprintf Behavior Change

by Marshall (Prior)
on Sep 17, 2013 at 07:37 UTC ( #1054397=note: print w/ replies, xml ) Need Help??


in reply to Printf/Sprintf Behavior Change

Well 0.34 and 0.35 could very well represent a similar value in binary that is rounded or truncated by different calculation methods.

I have one program that essentially says that: 0.34+-.01 =equals 0.35+-0.01. For a science program, that works great. These values are so close that the program figures that they are equivalent. And this is done in a DB in a very efficient way.

If you are calculating with money, eg. $15.34 vs $15.35, things get more complex. In that case what happens to the .000012345 value matters (say an interest calculation). Now things get more complex because the "rounded value" is not the only thing that matters. This "pesky whatever was left-over after the significant decimal points" matters also.

There is a thing called "BCD arithmetic". BCD means "Binary Coded Decimal". There are Perl subs that can do this type of arithmetic rather than binary, but this is a "lot slower".

However if you understand the rounding rules for Perl, you will be fine.


Comment on Re: Printf/Sprintf Behavior Change
Re^2: Printf/Sprintf Behavior Change
by Anonymous Monk on Sep 17, 2013 at 09:08 UTC

    However if you understand the rounding rules for Perl, you will be fine.

    Handsom too? Whats that mean "fine"

      "fine" means that you understand what is going to happen in whatever number base that you are calculating in. And that no matter what you do, there will most often be imperfect representations in either a decimal or binary format. For example: 1/3. If we aren't in base 3, there is going to be a precision problem.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-08-23 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls