Well, another strange problem. Presumably this is due to the old "computers count in binary" thing. I have two equal numbers, but Perl doesn't think they are equal.

sub check_price_paid { my $price_paid = param("some_param"); my $price_to_pay = &calculate_price; return $price_to_pay <= $price_paid; # This is where we get pr +oblems!! } sub calculate_price { my $price = &calculate_base_price; $price += &calculate_tax($price); return $price; } sub calculate_base_price { my $price = 0; foreach (@item) { $price += $_ -> price; } return $price; # this will always be to 2 sig figs. } sub calculate_tax { my $price = shift; my $tax; $tax = $price * $CONFIGS->{tax} / 100; # need to be accounting correct $tax *= 10 ** $currency_decimal_places; if ($tax =~ /\.(\d)/ && $1 >= 5) { $tax ++} # a cheat, but sim +ple. This rounds up or down, in accounting-correct manner. $tax = int $tax; $tax /= 10 ** $currency_decimal_places; return $tax; }

Now my problem is that sometimes, the price_paid (a parameter returned to the script) is, e.g. 41.79. And the price_to_pay is calculated as 41.79. So if I do 'warn "$price_to_pay and $price_paid"' I get "41.79 and 41.79". But then, Perl returns 0 from check_price_paid.

Is there any way round this? Do I need, e.g., to somehow make sure both variables are converted to numbers? But Perl has weak typing so that don't apply. Surely there must be some reliable way of checking that two rounded numbers are identical?

dave hj~


In reply to strange arithmetic results by dash2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.