Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Simple Sum - One negative, one positive

Author
Message
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Mar 2016 21:12
I am making a simple check to see if value a is negative while value b is positive, or vice versa. Is there any better way than this...

if (a < 0 and b > 0) or (a > 0 and b < 0)
// do something
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Paronamixxe
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 10th Feb 2012
Location: Sweden
Posted: 29th Mar 2016 21:28 Edited at: 29th Mar 2016 21:29
I don't know if it's better, but maybe...

Might be something? At the very least it only checks one equation.
cheers
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 29th Mar 2016 21:38
if you multiply a negative number with a positive number then the answer will always be negative. Two positives or two negatives multiplied are always positive .
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 30th Mar 2016 08:31
Thanks
I think Matty's solution is the most efficient, and it is one of those moments...why didn't I think of that?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
nickele upgraded
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Birthplace of Democracy
Posted: 30th Mar 2016 09:08
Sorry for interrupting , but if we use something like:
if ((a && b) >> 31) then **do something**
wouldn't it be a little bit faster (because of the bitwise operation)?
That's because if a,b are integers, then the expression logically ANDs the two values and takes the most significant bit (shifting 31 times to right).
If a,b have different signs, the result is always 1.
nickele upgraded
10
Years of Service
User Offline
Joined: 20th Dec 2013
Location: Birthplace of Democracy
Posted: 30th Mar 2016 09:11
Apologies for the inaccuracy.
I meant something like:
if ((a * b) >> 31)
Apologies again.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 30th Mar 2016 11:47
if a*b<0 should be fast enough most of the time... unless this is being used thousands of times per loop is it worth worrying about?
Using AppGameKit V2 Tier 1
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 30th Mar 2016 13:12
Quote: "unless this is being used thousands of times per loop is it worth worrying about?"


valid point I'm aiming at very low specced devices (think fruit-desert based devices ) and trying to keep to 60FPS as it is a reaction based app.
But I'm always keen to find efficient ways to do things.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 30th Mar 2016 13:55
if u can get the a and b in the range -1,0,1 (means up,0,down)
there is only a>b or a<b
AGK (Steam) V2.0.18 : Windows 10 Pro 64 Bit : AMD (15.30.1025) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)

Login to post a reply

Server time is: 2024-09-29 13:21:41
Your offset time is: 2024-09-29 13:21:41