Math Functions

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
F50
Fleet Officer
Fleet Officer
Posts: 1004
Joined: Sat Mar 11, 2006 5:25 pm

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.
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

Code: Select all

(setq absouluteValue (lambda (number)
	(if (ls number 0)
		(multiply number -1)
		number
	)
))
Crying is not a proper retort!
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

and now for the quick exponent function (did you want negative x values? I assumed you didn't but doesn't hurt to ask) :D
(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
	)
))
Last edited by Betelgeuse on Tue Mar 18, 2008 8:45 pm, edited 4 times in total.
Crying is not a proper retort!
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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
		)
	)
))
Crying is not a proper retort!
F50
Fleet Officer
Fleet Officer
Posts: 1004
Joined: Sat Mar 11, 2006 5:25 pm

put these in the <Globals> tag?
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

yes you can put these into any Globals tag in an extension
Crying is not a proper retort!
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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
	)
))
Crying is not a proper retort!
Post Reply