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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |