Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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]
[Tanktalus]: Discipulus: I'd have tried my hand at it, but OP said something about not replying if you're experienced at regexes :)
[Tanktalus]: And now that I've been using regexes in C++, I'm thinking that might apply :)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-12-18 23:20 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (501 votes). Check out past polls.