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

Re: Printf/Sprintf Behavior Change

by Marshall (Abbot)
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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054397]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-05-26 20:57 GMT
Find Nodes?
    Voting Booth?