View Single Post
  #9   Spotlight this post!  
Unread 13-02-2005, 20:38
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: joystick sensitivity

Quote:
Originally Posted by AIBob
abs(p1_x) can be replaced with (p1_x && 0x7F), because the last bit is the sign,...
Not so. For two reasons, the first of which is very important:

Negative numbers are not stored in sign-magnitude form. They are in "two's-complement" form. The value -1 masked with 0x7F yields a value of 127, a far cry from the expected 1.

(The second reason is that && is a logical operator, giving a true or false result. The arithmetic and is represented by a single &.)

The simplest syntax I know of for an absolute value function uses the ternary if operator. It's cryptic, but effective:
Code:
(value<0?-value:value)