Get the sign of a number?

Discussion and feedback on Construct 2

Post » Tue Jun 17, 2014 6:29 pm

I realize you could divide the number by its absolute value and that results in a 1 or -1. Is there a computationally cheaper way to isolate the sign? Like maybe could you use getbit() in the most significant bit? (I'm guessing that's where the sign of an int is stored). Or maybe is a condition in C2 that tests for > or < zero faster than division?
B
11
S
4
G
1
Posts: 159
Reputation: 1,803

Post » Tue Jun 17, 2014 6:35 pm

Image ImageImage
B
172
S
50
G
183
Posts: 8,440
Reputation: 115,599

Post » Wed Jun 18, 2014 6:17 am

newt is right, use ternary operators like: (value < 0 ? -value : value ) if you want.
...but, your question have reason - bit operations more faster.
B
57
S
19
G
10
Posts: 191
Reputation: 9,636

Post » Thu Jun 19, 2014 12:00 am

@newt @Kurz So it sounds like >,<,etc is faster than division, and getbit() is faster than both? If I got that right, which bit is the sign?
B
11
S
4
G
1
Posts: 159
Reputation: 1,803

Post » Thu Jun 19, 2014 12:20 am

I don't think you can use the getbit on that regard, but normally it is the highest bit

And performance wise, the difference won't be noticeable anyway
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
54
S
22
G
18
Posts: 2,123
Reputation: 17,150


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 5 guests