Results 1 to 7 of 7
- 11-11-2011, 05:26 AM #1
Member
- 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.
- 11-11-2011, 06:15 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 11-11-2011, 09:00 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-11-2011, 05:10 PM #4
Member
- 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];
Probably not very good code, but I hope it gets the point across. That would be correct, right?Java Code:angle %= TWO_PI; if (angle < PI) { //unchanged } else { angle -= TWO_PI; } if (angle >= -PI) { //unchanged } else { angle += TWO_PI; } return angle;
- 11-12-2011, 01:35 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Question about ternary operators.
That depends. The horrible ternary expression begins: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?
- 11-12-2011, 07:09 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Question about ternary operators.
It works as expected, so further discussion is just discussion.Python Code:def normalRelativeAngle(angle): angle %= 6.283 if angle > 3.141: angle -= 6.283 if angle < -3.141: angle += 6.283 return angle
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:(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?
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.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; } }
- 11-12-2011, 07:54 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Shorthand ternary operator ?
By Billywizz in forum New To JavaReplies: 3Last Post: 10-02-2011, 09:57 PM -
Question regarding prefix & postfix operators
By pulikarthi in forum New To JavaReplies: 6Last Post: 04-18-2011, 12:47 PM -
doubt regarding ternary operator (?)
By subith86 in forum New To JavaReplies: 2Last Post: 03-10-2011, 08:15 AM -
Use of ternary
By wulfgarpro in forum New To JavaReplies: 1Last Post: 09-21-2010, 06:31 AM -
Quick Question about relational operators
By SwEeTAcTioN in forum New To JavaReplies: 2Last Post: 10-27-2009, 05:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks