Not equals to (number to number)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
brozenadenzen
Prole
Posts: 14
Joined: Tue Feb 14, 2012 5:04 pm

Not equals to (number to number)

Post 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
User avatar
Camewel
Prole
Posts: 20
Joined: Thu Apr 19, 2012 5:58 pm

Re: Not equals to (number to number)

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Not equals to (number to number)

Post 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 ...
When I write def I mean function.
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Not equals to (number to number)

Post 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
Your screen is very zoomed in...
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Not equals to (number to number)

Post 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.
Kurosuke needs beta testers
User avatar
Camewel
Prole
Posts: 20
Joined: Thu Apr 19, 2012 5:58 pm

Re: Not equals to (number to number)

Post by Camewel »

Oh, you know what I meant.

Code: Select all

if x < 16 or x > 20 then
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Not equals to (number to number)

Post 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.
When I write def I mean function.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Not equals to (number to number)

Post 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 ;)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 104 guests