in reply to Re: Multiplication problem
in thread Multiplication problem

Thanks for your reply, but the outcome is the same. The multiplication is still incorrect.

Any other suggestions?

gr. John

Replies are listed 'Best First'.
Re^3: Multiplication problem
by dragonchild (Archbishop) on Dec 10, 2007 at 15:44 UTC
    Huh. You're absolutely correct. Floating point match is notoriously problematic when doing rounding. It's just the nature of doing floating point arithmetic on a binary processor.

    You could take mwah's solution. Or, you could convert everything to integers. So, instead of working in dollars, you work in cents.

    my $price = 1795; # Cents, not dollars my $qtyx = 3; # Amount, not a price. my $cost = $price * $qtyx; my $cost_to_display = $cost / 100;
    And that works when tested.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      This is a usefull solution. I think i can manage to rewrite the script.

      Thanks

        Please note an important distinction between the values you use to calculate with internally (denominated in cents) and what you display to the user (denominated in dollars). You will probably want to write some routines hat translate between your internal representation and what you take for input and display for output.

        This, btw, is quite common to do.


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?