...

View Full Version : comparison with -ve returning wrongly



bazz
09-01-2009, 01:31 AM
Hi,

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.

bazz

FishMonger
09-01-2009, 02: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?

bazz
09-01-2009, 03: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.
bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum