Results 1 to 10 of 10

Thread: negative to positive

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Location
    california
    Posts
    245

    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

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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?
    Last edited by Dilenger4; Nov 25th, 2003 at 02:16 AM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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)

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    If not in VB, then do this:
    if C < 0 then
    C= 0-C
    end if
    Have I helped you? Please Rate my posts.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There's an absolute function in nearly every language.
    Mathematical notation is
    |a|
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    You could also multiply by -1
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    C = 0 - C
    or
    C = -C
    is probably faster than
    C = C * -1
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    And if its in C, you could do
    C *=-1;
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    still slower.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    C *=-1;

    is the same as C = C * (-1)

    but C = 0-C is definitely faster(a littttttttttttttttttttttle)
    Regards

Posting Permissions

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



Click Here to Expand Forum to Full Width