View Full Version : comparison with -ve returning wrongly

09-01-2009, 02:31 AM

Not come up against this before.

I have a -ve value which I am trying to compare with 0 It returns as if it were a +ve number. what have I forgotten/not learned?

my $nights_from_start_of_tariff = -2;
my $nights_to_end_of_tariff = 54;
my $number_of_nights = 3;

if ($nights_from_start_of_tariff >= 0 && $nights_to_end_of_tariff >= $number_of_nights)
$nights_on_first_tariff = $number_of_nights;
elsif ( $nights_from_start_of_tariff >= 0 && $nights_to_end_of_tariff < $number_of_nights)
$nights_on_first_tariff = $nights_to_end_of_tariff;
elsif ( $nights_from_start_of_tariff < 0 && $nights_to_end_of_tariff >= $number_of_nights )
$nights_on_second_tariff = ($number_of_nights + $nights_from_start_of_tariff );

The blue bit works as it should but the red bit returns a result as if -2 was the same as +2.


09-01-2009, 03:44 AM
What are the values of the other vars?

For debugging purposes, have you tried expanding the if/elsif blocks so that they are not dealing with compound conditions?

09-01-2009, 04:09 AM
Thanks FishMonger.

I might have it shortly. :) I'll double check that compound conditions issue but but I don't think it is a feature.

I have changed it such that it runs a sub, to ascertain whether the number is -ve and, based on that, it runs an if clause ahead of the others in the if/elsif block.

tired now (03:08) so I'll look at it again tomorrow.