Results 1 to 7 of 7
 11112011, 06:26 AM #1Member
 Join Date
 Nov 2011
 Posts
 3
 Rep Power
 0
Question about ternary operators.
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.
 11112011, 07:15 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,671
 Rep Power
 13
Re: Question about ternary operators.
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.
 11112011, 10:00 AM #3Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 9
Re: Question about ternary operators.
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.
 11112011, 06:10 PM #4Member
 Join Date
 Nov 2011
 Posts
 3
 Rep Power
 0
Re: Question about ternary operators.
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];
Java Code:angle %= TWO_PI; if (angle < PI) { //unchanged } else { angle = TWO_PI; } if (angle >= PI) { //unchanged } else { angle += TWO_PI; } return angle;
 11122011, 02:35 AM #5Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,671
 Rep Power
 13
Re: Question about ternary operators.
That would be correct, right?
(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?
 11122011, 08:09 AM #6Member
 Join Date
 Nov 2011
 Posts
 3
 Rep Power
 0
Re: Question about ternary operators.
Python Code:def normalRelativeAngle(angle): angle %= 6.283 if angle > 3.141: angle = 6.283 if angle < 3.141: angle += 6.283 return angle
(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?
Java Code:angle %= TWO_PI; if (angle >= 0) { if (angle < PI) { //unchanged } else { angle = TWO_PI; } } else { if (angle > PI) { //unchanged } else { angle += TWO_PI; } }
 11122011, 08:54 AM #7Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,671
 Rep Power
 13
Similar Threads

Shorthand ternary operator ?
By Billywizz in forum New To JavaReplies: 3Last Post: 10022011, 09:57 PM 
Question regarding prefix & postfix operators
By pulikarthi in forum New To JavaReplies: 6Last Post: 04182011, 12:47 PM 
doubt regarding ternary operator (?)
By subith86 in forum New To JavaReplies: 2Last Post: 03102011, 09:15 AM 
Use of ternary
By wulfgarpro in forum New To JavaReplies: 1Last Post: 09212010, 06:31 AM 
Quick Question about relational operators
By SwEeTAcTioN in forum New To JavaReplies: 2Last Post: 10272009, 06:11 AM
Bookmarks