in reply to Multiplication problem

Replace round() with:
sub round { my ($num) = @_; return int( $num * 100 ) / 100; }
There's a much simpler commas() function, too, but I wouldn't replace what isn't broken.

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?

Replies are listed 'Best First'.
Re^2: Multiplication problem
by John Vanbeek (Initiate) on Dec 10, 2007 at 15:20 UTC

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

    Any other suggestions?

    gr. John

      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