Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    comparison with -ve returning wrongly

    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?

    Code:
    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
    Last edited by bazz; 09-01-2009 at 03:01 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,874
    Thanks
    2
    Thanked 164 Times in 159 Posts
    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?

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •