Results 1 to 11 of 11
  1. #1
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default [SOLVED] How can I dynanically change math signs

    Hi Gang:
    What I'm looking for is a way to dynamically change the plus "+" or minus "-" signs in my program (I searched the forum and didn't see anything similiar).

    Explanation:
    • If the condition is x, then use "+" in all math operations
    • If the condition is y, then use "-" in all math operations

    I know that I can have two sections in the program, one that adds and the other that subtracts, but it would be easier (and more efficient) if could establish this from the begining since the two sections are the same except for the math operations.
    Thanks
    Chris

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by CJSLMAN View Post
    Hi Gang:
    What I'm looking for is a way to dynamically change the plus "+" or minus "-" signs in my program (I searched the forum and didn't see anything similiar).

    Explanation:
    • If the condition is x, then use "+" in all math operations
    • If the condition is y, then use "-" in all math operations

    I know that I can have two sections in the program, one that adds and the other that subtracts, but it would be easier (and more efficient) if could establish this from the begining since the two sections are the same except for the math operations.
    Thanks
    Chris
    Use if-else conditions. That's all. Why you can't do it?

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Indeed, I don't really see the problem here either...
    Just use if statements or multiply some variables with -1...
    I die a little on the inside...
    Every time I get shot.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    Agreed, but I'm trying to avoid repeating the same code twice where the only difference is the math sign. Something like (simple pseudo code):
    Java Code:
    if (conditon = x)
      {
       mathSign = +;
      }
    else if (conditon = y)
      {
       mathSign = -;
      }
    result = 100 mathSign 10; //depending on the value of mathSign the answer 
                                        //could be 90 or 110
    Thanks,
    Chris

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Using the ternary operator
    Java Code:
    result = 100 + 10 * (conditon ? 1 : -1);
    Using if-else
    Java Code:
    int sign = 0;
    if (conditon) {
      sign = 1;
    } else {
      sign = -1;
    }
    result = 100 + 10 * sign;
    db
    Last edited by DarrylBurke; 10-04-2008 at 08:01 PM. Reason: missed a code tag

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default [SOLVED] How can I dynanically change math signs

    Supamagier & Darryl.Burke... thanks for the responses. Multiplying by the Sign variable will do the job.
    Thanks,
    Chris

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by CJSLMAN View Post
    Multiplying by the Sign variable will do the job.
    What you mean by sign variable? Depends on your requirements either use if-else of ternory.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default OK... here we go again

    I will try to be as explicit as possible:
    Problem: I needed to be able to change the math sign (+ or -) depending on some established condition:
    • If the condition is x, then use "+" in all math operations
    • If the condition is y, then use "-" in all math operations

    What I didn't want to do was program two separate sections, one using the plus sign and the other using the minus sign, since the logic is the same in both sections.
    Solution: Establishing a variable (sign variable) that contains a 1 or -1 (see sign variable used in Daryl Burkes example below) provides the solution I was looking for:
    Java Code:
    int sign = 0;
    if (conditon) {
      sign = 1;
    } else {
      sign = -1;
    }
    //logic follows here...
    result = 100 + 10 * sign;
    The ternary operator solution also provides a solution, but I would have to implemented in each line that needed the + or - sign. That is why I chose the solution stated above.
    Thanks

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    ternary operator, what's that?

    EDIT: nvm
    Last edited by Supamagier; 10-06-2008 at 05:08 PM.
    I die a little on the inside...
    Every time I get shot.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Supamagier View Post
    ternary operator, what's that?

    EDIT: nvm
    You don't know about ternary operator? Amazing, how long you are programming in Java, or any other language?

    Ternary operator is the way to write complex if-else syntax, with minimal amount of coding.

    like this,
    Java Code:
    sign = (conditon) ? 1 : -1;

Similar Threads

  1. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 04:06 PM
  2. need help with math for a new program
    By gotenks05 in forum New To Java
    Replies: 13
    Last Post: 09-26-2008, 07:32 AM
  3. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  4. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 AM
  5. Date math
    By orchid in forum New To Java
    Replies: 2
    Last Post: 04-18-2007, 07:01 AM

Posting Permissions

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