Just a little mathematic question

Non-platform specific questions.

Just a little mathematic question

Postby lcl » Wed Mar 28, 2012 11:23 am

So, is there a simpler way to check if value is between two values than:
Code: Select all
if (value >= -10 && value <= 10)

Where 'value' is the changing value and - 10 and 10 are the limits which I want to check it being in between of.
I'm trying to find a shorter way to write it but I can't come up with anything.

I think there must be some easy solution which I just can't figure out right now.
Thanks in advance! =)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Just a little mathematic question

Postby DST » Wed Mar 28, 2012 3:14 pm

Code: Select all
if (abs(value)<10)
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Just a little mathematic question

Postby SuperSonic » Wed Mar 28, 2012 3:41 pm

DST beat me to it ^^
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Just a little mathematic question

Postby lcl » Wed Mar 28, 2012 3:56 pm

Oh, thanks, that's way too easy :lol:

But how about when the limiters are not opposite numbers?
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Just a little mathematic question

Postby SuperSonic » Wed Mar 28, 2012 4:21 pm

In that case, I think you'd have to use the long method :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Just a little mathematic question

Postby lcl » Wed Mar 28, 2012 4:39 pm

Yeah..maybe
I just think to have seen something similar to what DST offered in a way I need it.
I think it was some code of skydereigns.
But it might be that I remember wrong. :P
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Just a little mathematic question

Postby Game A Gogo » Wed Mar 28, 2012 7:07 pm

SuperSonic wrote:In that case, I think you'd have to use the long method :)


if(abs(value-((max+min)/2)))<=max-((max+min)/2))

so if your max is 10 and minimum is -6

if(abs(value-2)<=8)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Just a little mathematic question

Postby lcl » Wed Mar 28, 2012 8:33 pm

Thanks GaG! =D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest