Results 1 to 7 of 7
  1. #1
    Harrison is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  4. #4
    Harrison is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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;
    Probably not very good code, but I hope it gets the point across. That would be correct, right?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Question about ternary operators.

    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?

  6. #6
    Harrison is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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
    It works as expected, so further discussion is just discussion.

    (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:
    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;
        }
    }
    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.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Question about ternary operators.

    Yes, that Java code expands what the compound ternary does I think.

Similar Threads

  1. Shorthand ternary operator ?
    By Billywizz in forum New To Java
    Replies: 3
    Last Post: 10-02-2011, 10:57 PM
  2. Question regarding prefix & postfix operators
    By pulikarthi in forum New To Java
    Replies: 6
    Last Post: 04-18-2011, 01:47 PM
  3. doubt regarding ternary operator (?)
    By subith86 in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 09:15 AM
  4. Use of ternary
    By wulfgarpro in forum New To Java
    Replies: 1
    Last Post: 09-21-2010, 07:31 AM
  5. Quick Question about relational operators
    By SwEeTAcTioN in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 06:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •