Page 1 of 1
Math Functions
Posted: Tue Mar 18, 2008 5:21 pm
by F50
Are these the only math functions?
add
subtract
multiply
divide
modulus
If so, I request
absouluteValue (possibly abbreviated as abs)
exponent
log (allowing you to specify the log base. The natural constant doesn't need to be implemented)
as well as the ability to get just the first digit of a number.
Absolute value is the only one I would currently like for my mod, but I can see myself using the others in the future.
Posted: Tue Mar 18, 2008 7:26 pm
by Betelgeuse
Code: Select all
(setq absouluteValue (lambda (number)
(if (ls number 0)
(multiply number -1)
number
)
))
Posted: Tue Mar 18, 2008 7:50 pm
by Betelgeuse
and now for the quick exponent function (did you want negative x values? I assumed you didn't but doesn't hurt to ask)

(it is longer than the simple version but much quicker)
Code: Select all
(setq exponent (lambda (x y)
(block (result currentEx)
(setq result Nil)
;shortcuts
(if (or (eq y 0) (eq x 1))
(setq result 1)
(if (and (not result) (eq x 0))
(setq result 0)
(block Nil
;set up the variables
(setq currentEx 1)
(setq result x)
;now for the main body
(loop (ls currentEx y)
(block Nil
(if (leq (multiply currentEx 2) y)
(block Nil
(setq result (multiply result result))
(setq currentEx (multiply currentEx 2))
)
(block Nil
(setq result (multiply result x))
(setq currentEx (add currentEx 1))
)
)
)
)
)
)
)
result
)
))
Posted: Tue Mar 18, 2008 8:08 pm
by Betelgeuse
Code: Select all
(setq firstDigit (lambda (number)
(block (negitive)
;make sure we save if it is negitive or not
(if (ls number 0)
(block Nil
(setq negitive True)
(setq number (multiply number -1))
)
)
(loop (gr number 9)
(setq number (divide number 10))
)
;just in case the loop is never run lets say the number again
(if negitive
(multiply number -1)
number
)
)
))
Posted: Tue Mar 18, 2008 8:46 pm
by F50
put these in the <Globals> tag?
Posted: Tue Mar 18, 2008 8:50 pm
by Betelgeuse
yes you can put these into any Globals tag in an extension
Posted: Tue Mar 18, 2008 10:28 pm
by Betelgeuse
only gives the first digit of the log base x of y and isn't the fastest but best I could think of in integers
Code: Select all
;log base x of y
(setq logApp (lambda (x y)
(block (result)
(setq result 0)
(loop (geq y x)
(setq result (add result 1))
(setq y (divide y x))
)
result
)
))