0

I'm not really sure what to search for on this.

If I have a variable A = 10. And another variable B. If B is negative I want to make A = -10. If B is positive I want A = 10.

Here is how I have been doing this quite often:

A = A * abs(B) / B

The obvious issue here is that if B is zero I get a divide by zero error.

Is there a better (preferably mathematical) way to accomplish this without the complexity of conditional statements?

Backstory. I am working with students in a graphical robotics programming language called Lego EV3. The algorithm above looks like this:

enter image description here

Using a conditional statement it looks like this: enter image description here

Quite the waste of space, especially when you are working on 13" laptop screens. And confusing.

Appleoddity
  • 647
  • 1
  • 6
  • 21
  • 2
    Please tell us which language you are using. – Tim Biegeleisen Nov 28 '18 at 07:00
  • @TimBiegeleisen It isn't one specific language. It shouldn't be so complex that it matters or be specific to one language. If it matters, I am currently working with students in a graphical programming language that can get quite bulky and messy when adding in complex structures like a conditional statement. I'd like to use a simple mathematical statement that is a "one liner." (most mathematical functions are available). – Appleoddity Nov 28 '18 at 07:04
  • 3
    Um...the language absolutely matters, especially on this site. If you want a one-liner, look into using ternary expressions. Conditionals are the way to go here, IMHO. – Tim Biegeleisen Nov 28 '18 at 07:05
  • @TimBiegeleisen Thank you. Ternary expressions are not available. To be specific, this is Lego EV3. But, it is also an issue with other graphical languages like scratch. Maybe the conditional statement is the only way to go. And if that is the case, that is fine. But I thought I might be missing an obvious mathematical approach to this along similar lines as to what I posted. – Appleoddity Nov 28 '18 at 07:08
  • 4
    Do you need this: `A = abs(A) * sign(B)` ? (note - A becomes 0 for zero B - but you don't specify this case clearly) – MBo Nov 28 '18 at 07:13
  • looks like you'll get better answers on [bricks.se] https://bricks.stackexchange.com/questions/tagged/ev3 – phuclv Nov 28 '18 at 16:07
  • suppose B is positive, and A is negative. By your title, A should become positive, but your one-liner leaves A positive. So which is it? – President James K. Polk Nov 29 '18 at 03:35
  • @JamesKPolk Good question. I suppose I wasn’t clear enough. What I actually want is to invert the sign of A if B is negative. The object is to take a value (B) and add the value of A in the same sign as B. Assuming A is positive, If B is negative we go more negative. If B is positive we go more positive. I.e. a motor speed controller can function with the same code, regardless if it is moving backwards (-B) or moving forwards (+B) or if you are trying to speed the motor up or slow it down (+A and -A) . So, I tried to over simplify this and may have introduced more confusion. – Appleoddity Nov 29 '18 at 03:56

3 Answers3

1

Just to turn @MBo's comment into an official answer, note that many languages have a function called sign(x) or signum(x) that returns -1, 0, or 1 if x is negative, zero, or positive respectively, and another function abs(x) (for absolute value) that can be used together to achieve your purpose:

A = abs(A) * sign(B)

will copy the sign from B to A if B ≠ 0. If B == 0 you will have to do something extra.

Many languages (C++, Java, python) also have a straightforward copysign(x, y) function that does exactly what you want, returning x modified to have y's sign.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • [`signum` is a mathematical function](https://en.wikipedia.org/wiki/Signum_function). [Is there a standard sign function (signum, sgn) in C/C++?](https://stackoverflow.com/q/1903954/995714) – phuclv Nov 28 '18 at 16:02
0

In many programming languages, a simple if statement would work:

A = 10;
if (B < 0) {
    A = -1*A;
}

If your language supports ternary expressions, we could reduce the above to a single line:

A = B < 0 ? -1*A : A;

Another option might be to define a helper function:

reverseSign(A, B) {
    if (B < 0) {
        return -1*A;
    }
    else {
        return A;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

C99 has the (POSIX) function copysign that does just this. Fortran has had this for ages. It's also a IEEE 754 recommended function

dmuir
  • 4,211
  • 2
  • 14
  • 12