Page 1 of 1

Not equals to (number to number)

Posted: Thu Apr 19, 2012 6:13 pm
by brozenadenzen
Hi, I was wondering is it possible to make variable comparison to a range of numbers in a simple way, something like if X is not equal to 16 to 20 (which are 16,17,18,19,20) then do something. Would be so nice :D

Re: Not equals to (number to number)

Posted: Thu Apr 19, 2012 6:16 pm
by Camewel
You can just do something like this:

Code: Select all

if x > 16 or x < 20 then
Meaning it will be true as long as it is not between these two numbers. Seems a bit silly to add a special function for this.

Re: Not equals to (number to number)

Posted: Thu Apr 19, 2012 6:21 pm
by kikito
I usually put the two conditions in a way that makes the numbers increase, from lower to upper:

Code: Select all

if min <= x and x <= max then ... 
if you *really* need a function, this should do:

Code: Select all

function between(x,min,max)
  return min <= x and x <= max
end
Usage:

Code: Select all

if between(x,16,20) then ...

Re: Not equals to (number to number)

Posted: Sun Apr 22, 2012 2:04 pm
by mickeyjm
Camewel wrote:You can just do something like this:

Code: Select all

if x > 16 or x < 20 then
Meaning it will be true as long as it is not between these two numbers. Seems a bit silly to add a special function for this.
Wont that be trur if x is larger than 16 OR less than 20 meaning that anything would return true
Eg. 5<20 therefore continue if statement

Re: Not equals to (number to number)

Posted: Sun Apr 22, 2012 2:10 pm
by tentus
mickeyjm wrote: Wont that be trur if x is larger than 16 OR less than 20 meaning that anything would return true
Eg. 5<20 therefore continue if statement
True.

Re: Not equals to (number to number)

Posted: Sun Apr 22, 2012 2:42 pm
by Camewel
Oh, you know what I meant.

Code: Select all

if x < 16 or x > 20 then

Re: Not equals to (number to number)

Posted: Sun Apr 22, 2012 2:51 pm
by kikito
Oh, I read this incorrectly. OP wanted to check that x is not in the range. Well, once you have a function, doing the contrary is easy:

Code: Select all

if not between(x,16,20) then ... 
If you want to write the whole code with no functions, I sill think it helps to have the numbers in order. I'd do something like this:

Code: Select all

if min > x or x > max then ...
But for the negation, I think that "not between" is more clear.

Re: Not equals to (number to number)

Posted: Mon Apr 23, 2012 7:07 am
by trubblegum
Actually, what OP specified was not inrange(x, 16, 20) - between(x, 16, 20) implies exclusion of the range limits, 16 and 20.
So :

Code: Select all

function inrange(x, lower, upper)
  return x >= lower and x <= upper
end

if not inrange(x, 16, 20) then end
You were closer the first time ;)