• 11-11-2011, 06:26 AM
Harrison
Hello, I'm fairly new to Java. I have a basic knowledge of it, but not much more. I'm wanting to learn how to program a Robocode bot, but that's not the purpose of this question. For a learning exercise, I'm translating and doing things in Python using pybotwars. However, pybotwars doesn't have some of the utilities Robocode comes with, so I'm porting those as I need them. The problem is with this:

/**
* Normalizes an angle to a relative angle.
* The normalized angle will be in the range from -PI to PI, where PI
* itself is not included.
*
* @param angle the angle to normalize
* @return the normalized angle that will be in the range of [-PI,PI[
*/
public static double normalRelativeAngle(double angle) {
return (angle %= TWO_PI) >= 0 ? (angle < PI) ? angle : angle - TWO_PI : (angle >= -PI) ? angle : angle + TWO_PI;
}

I looked up and learned how the nested ternary operators work, and it seems like I know how it works...but I can't get my Python version to work. My question is, what's keeping the above within the range of [-PI, PI], as the comment says?

Also, would it be correct to say that the line of code I'm looking at, first checks to see if 'angle' is already within -PI and PI, then normalizes it if it isn't?

Thanks in advance for any help. :smash:
• 11-11-2011, 07:15 AM
pbrockway2
No, it first evaluates the modulus of angle and TWO_PI and assigns that to angle.

Depending on your personal preference (and maybe OS) you should use either the backspace or the delete key to normalise that expression.
• 11-11-2011, 10:00 AM
Well, provide us, in words, from left to right (as that is how the statement is read) what you think that statement actually does. Once you get that correct you will know how it is keeping it between -PI and PI.
• 11-11-2011, 06:10 PM
Harrison
Oh, it's read from left to right? I saw somewhere that it was right to left...that suddenly makes a lot of sense, if it does the modulo on 'angle' first. Thanks!

For future reference, here's how I thought it broke down.

(angle %= TWO_PI) >= 0 ? (angle < PI) ? angle : angle - TWO_PI : (angle >= -PI) ? angle : angle + TWO_PI;
-----------------------------
(angle >= -PI) ? angle : angle + TWO_PI;
(angle < PI) ? angle : angle - TWO_PI : [whatever the above line returned];
(angle %= TWO_PI) >= 0 ? [whatever the above line returned];

Code:

```angle %= TWO_PI; if (angle < PI) {     //unchanged } else {     angle -= TWO_PI; } if (angle >= -PI) {     //unchanged } else {     angle += TWO_PI; } return angle;```
Probably not very good code, but I hope it gets the point across. That would be correct, right?
• 11-12-2011, 02:35 AM
pbrockway2
Quote:

That would be correct, right?
That depends. The horrible ternary expression begins:

(angle %= TWO_PI) >= 0 ?

That last part looks at the size of angle after the assignment and takes two paths depending on whether it is nonnegative or not. Basically, your first if/else should only be followed if angle is nonnegative, and the second if/else otherwise.

Does this make any difference to the eventual value of angle? Can you prove that?
• 11-12-2011, 08:09 AM
Harrison
Code:

```def normalRelativeAngle(angle):         angle %= 6.283         if angle > 3.141:             angle -= 6.283         if angle < -3.141:             angle += 6.283         return angle```
It works as expected, so further discussion is just discussion.

Quote:

(angle %= TWO_PI) >= 0 ?

That last part looks at the size of angle after the assignment and takes two paths depending on whether it is nonnegative or not. Basically, your first if/else should only be followed if angle is nonnegative, and the second if/else otherwise.

Does this make any difference to the eventual value of angle? Can you prove that?
I see what you mean, the expression does say that. It makes no difference, though, because if angle is non-negative, it's still checked if it's above PI. The if/else checks if it's above PI, and if it is...well, it's positive. And the same for negative. A more literal...(what would you call it, expansion?)...of the ternary expression would, then, be:
Code:

```angle %= TWO_PI; if (angle >= 0) {     if (angle < PI) {         //unchanged     }     else {         angle -= TWO_PI;     } } else {     if (angle > -PI) {         //unchanged     }     else {         angle += TWO_PI;     } }```
Oh, wait, I see what you mean now. In my previous Java code, it was checking if angle was less than PI first, and if angle was a negative number, that would return the wrong result. My Python version was changed to be simpler, but also fixed that problem, and I didn't even notice it.
• 11-12-2011, 08:54 AM
pbrockway2