Jump to content

What is the opposite function for (abs) ?


Recommended Posts

Posted

Hello ,

 

With the function abs we would get the absolute or the possitive number .

Like ..

 

(abs -100)

 

I want to convert a number to be completely negative for example from 100 to be -100 .

 

Is it possible ?

 

Many thanks

Posted (edited)

  (- 100)  

 

Or rather:

 

(- (abs x))

Edited by Lee Mac
Posted

you will have to make your own function to convert a number to a negitive

 

 
;;;- pass the function a positive number
(defun negnum (aug1 / retval )
 (if (not (minusp aug1));_verify its not negitive
(setq retval(- aug1(* aug1 2)));_make it negitive
       );_if
);_defun

Posted
  (- 100)  

:)

 

What a question . :lol:

 

I concentrated my thoughts on a function , which prevented me to think of it that way .:)

 

Thank you Lee.

Posted
you will have to make your own function to convert a number to a negitive

 

 
;;;- pass the function a positive number
(defun negnum (aug1 / retval )
 (if (not (minusp aug1));_verify its not negitive
(setq retval(- aug1(* aug1 2)));_make it negitive
       );_if
);_defun

 

Really nice, Thank you so much JohnM.

 

Appreciated.

Posted
Another

 

(1+ (~ (abs x)))

 

Thanks Lee , Also your code without the 1+ would give a correct result .

 

like....

 (~ (abs 100))

 

So it returns -100 But how to add one and it would not be added to the result ? :) I do wonder !!!!

 

Appreciated

Posted

For positive 'n'

(~ n)

will return -(n+1) (One's complement)

 

The "~" is a bitwise NOT, hence flipping all the bits (including the highest bit which determines the sign of the number [for a signed integer] ).

 

So, say we take an 8-bit signed integer as an example, the 8th bit being the sign bit:

 

           +/- 64 32 16  8  4  2  1
------------------------------------
  n  =       0  1  0  0  1  1  0  1  =    77
(~ n) =       1  0  1  1  0  0  1  0  =  - 78

 

Perhaps see here also: http://en.wikipedia.org/wiki/Signed_number_representations

 

This also has a great explanation:

 

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators

Posted

this thread is getting way too negitive for me

Posted
this thread is getting way too negitive for me

 

Why it's being negative fo you ?

 

I am sorry for that .

Posted
Also your code without the 1+ would give a correct result .

 

It shouldn't do, as explained in my last post...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...