subtract

caderudite

Usenet Archive
Joined
Jul 28, 2003
Messages
17
Reaction score
0
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
 
(setq num1 (getreal)
num2 (getreal)
)

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

(princ val)
 
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:

;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
 
<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.
 

Latest threads

Back
Top