-
negative to positive
Hi,
This is a really easy question I'm sure but I can't figure it out in a simple way.
Let's say I have A + B = C
and A is 100 and B is -200 so C = -100
How can I get C as a positive number?
I need this for a rectangle drawing function I'm writing where the mouse down and mouse up coordinates can be positive or negative.
What I'm really looking for is the difference between A and B as a positive number.
Any help?
Thanks
T
-
x - y is the same as x + (-y) so in your case wouldnt you get a negative value either way? if this is supposed to be coded why don't you just use C's absolute value?
-
I'm guessing that as it is rectangles all lines are horizontal or vertical, in which case Something Else's code is a little over the top (but required if lines are at an angle) . In VB you can do this:
C = Abs(A + B)
-
If not in VB, then do this:
if C < 0 then
C= 0-C
end if
-
There's an absolute function in nearly every language.
Mathematical notation is
|a|
-
You could also multiply by -1
-
C = 0 - C
or
C = -C
is probably faster than
C = C * -1
-
And if its in C, you could do
C *=-1;
-
-
C *=-1;
is the same as C = C * (-1)
but C = 0-C is definitely faster(a littttttttttttttttttttttle)