# [SOLVED] How can I dynanically change math signs

• 10-04-2008, 06:43 PM
CJSLMAN
[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
• 10-04-2008, 06:47 PM
Eranga
Quote:

Originally Posted by CJSLMAN
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?
• 10-04-2008, 07:29 PM
Supamagier
Indeed, I don't really see the problem here either...
Just use if statements or multiply some variables with -1...
• 10-04-2008, 07:35 PM
CJSLMAN
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):
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
• 10-04-2008, 07:56 PM
DarrylBurke
Using the ternary operator
Code:

`result = 100 + 10 * (conditon ? 1 : -1);`
Using if-else
Code:

```int sign = 0; if (conditon) {   sign = 1; } else {   sign = -1; } result = 100 + 10 * sign;```
db
• 10-04-2008, 08:29 PM
CJSLMAN
[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
• 10-05-2008, 04:41 AM
Eranga
Quote:

Originally Posted by CJSLMAN
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.
• 10-05-2008, 07:51 PM
CJSLMAN
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:
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
• 10-06-2008, 09:10 AM
Eranga
You can use the ternary operator to define the sign as well.
• 10-06-2008, 05:03 PM
Supamagier
ternary operator, what's that?

EDIT: nvm
• 10-07-2008, 04:07 AM
Eranga
Quote:

Originally Posted by Supamagier
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,
Code:

`sign = (conditon) ? 1 : -1;`