Michaels Posted October 23, 2010 Posted October 23, 2010 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 Quote
Lee Mac Posted October 23, 2010 Posted October 23, 2010 (edited) (- 100) Or rather: (- (abs x)) Edited October 23, 2010 by Lee Mac Quote
JohnM Posted October 23, 2010 Posted October 23, 2010 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 Quote
Michaels Posted October 23, 2010 Author Posted October 23, 2010 (- 100) What a question . I concentrated my thoughts on a function , which prevented me to think of it that way . Thank you Lee. Quote
Michaels Posted October 23, 2010 Author Posted October 23, 2010 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. Quote
Michaels Posted October 24, 2010 Author Posted October 24, 2010 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 Quote
Lee Mac Posted October 24, 2010 Posted October 24, 2010 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 Quote
JohnM Posted October 24, 2010 Posted October 24, 2010 this thread is getting way too negitive for me Quote
Michaels Posted October 24, 2010 Author Posted October 24, 2010 this thread is getting way too negitive for me Why it's being negative fo you ? I am sorry for that . Quote
Lee Mac Posted October 24, 2010 Posted October 24, 2010 Also your code without the 1+ would give a correct result . It shouldn't do, as explained in my last post... Quote
Recommended Posts
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.