subtract

Discussion in 'AutoCAD' started by caderudite, Aug 20, 2003.

  1. caderudite

    caderudite Guest

    can anyone has a solution for this problem. want to make subtraction and if the result value is negative change that to possitive value (like float) thanks guys
     
    caderudite, Aug 20, 2003
    #1
  2. caderudite

    PF Guest

    (setq num1 (getreal)
    num2 (getreal)
    )

    (setq val (- num1 num2))
    (setq val (abs val))

    (princ val)
     
    PF, Aug 20, 2003
    #2
  3. caderudite

    Joe Burke Guest

    Using the abs function with the subtraction function doesn't seem like a good idea to me.



     



    Command: (abs (- 7 3))
    4



    Command: (abs (- 3 7))
    4



    Command: (abs (- 3 -7))
    10  < duh...



     



    Maybe something like this which always returns the same answer, a positive value, regardless of negative/positive values. Also regardless of the order in which the arguments are presented.



     



    (defun AbsSubtract (v1 v2)
      (setq v1 (abs v1) v2 (abs v2))
      (if (> v1 v2)
        (- v1 v2)
        (- v2 v1)
      )
    )



     



    Command: (AbsSubtract 3 -7)
    4



     



    The basic idea: don't ask an arithmetic function to return a false answer. Rather condition the arguments first.



     



    Joe Burke



    "rdi" <rdi!@!writeme.com> wrote in message news:...



    ;subtract v2 from v1 and



    ;return positive value



    (defun subpos(v1 v2)



      (abs(- v1 v2))



    )



     



     



    (subpos 100 250) ;returns 150



    (subpos 100 75); returns 25




    --

    RDI



     



    (remove the exclamation from the email address)




    "caderudite" <> wrote in message news:...

    can anyone has a solution for this problem. want to make subtraction and if the result value is negative change that to possitive value (like float) thanks guys
     
    Joe Burke, Aug 20, 2003
    #3
  4. caderudite

    PF Guest

    <snip>> Command: (abs (- 7 3)) --> 4
    Command: (abs (- 3 7)) --> 4
    Command: (abs (- 3 -7)) --> 10 <<

    AND...
    Command: (- 3 7) --> -4
    Command: (- 7 3 ) --> 4
    Command: (- -7 3 ) --> -10
    Command: (- -7 -3 ) --> -4
    Command: (- 7 -3 ) --> 10
    Command: (- -3 7) --> -10

    Each computes the difference between values (as a coordinate on a line would
    be computed)

    All you told me was Subtraction is Non-commutative... any one paying
    attention in 4th grade math would tell you the same.
    So, how 'bout (from the above) each of these:
    Command: (setq val (- 3 7)) --> -4
    Command: (setq val (- 7 3 )) --> 4
    Command: (setq val (- -7 3 )) --> -10
    Command: (setq val (- -7 -3 )) --> -4
    Command: (setq val (- 7 -3 )) --> 10
    Command: (setq val (- -3 7)) --> -10
    Same results set to VAL.
    What I'm doing is perfectly valid (if I pay attention to what's where), and
    (abs val) would give the distance in a positive direction.

    There is (of course) the possibility you wanted (abs) applied First... and
    that Would give different results.
     
    PF, Aug 20, 2003
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.