Maclaurin series, math library

Freeform discussion about anything related to modding Transcendence.
Post Reply
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Hi,
since we have real numbers now, I thought it could be interesting to implement Maclaurin series.
and that means we can have decently good sin(x), cos(x), e^(x) and so on.
https://en.wikipedia.org/wiki/Taylor_series

So, I wrote functions to implement factorial x! and conversion of degrees to radians and back.

Largest factorial we can do in TLISP is 12!=479,001,600
13! wraps back to a negative number.

Code: Select all

(setq we_deg2rad (lambda (d) 
	(block (output PI)
		(setq PI 3.141592653589)
		(setq output (* d (/ PI 180.0)))
	output
	)
))

(setq we_rad2deg (lambda (r)
	(block (output PI)
		(setq PI 3.141592653589)
		(setq output (* r (/ 180.0 pi)))
	output
)))

(setq we_factorial (lambda (imput)
	(block (output tempNumber)
	(setq tempNumber imput)
	(setq output imput)
	(switch 
		
		(eq imput 0)
		(setq output 1)
		
		(eq imput 1)
		(setq output 1)
		
		(loop (neq tempNumber 1)
			(block Nil
			(setq tempNumber (subtract tempNumber 1))
			;(printTo 'console "tempNumber " tempNumber)
			(setq output (multiply output tempNumber))
			;(printTo 'console "output " output)
			)
		)
	)
	output
)))
Then I tried to implement sin(x) using the previous functions, but something is not right? Can someone help me debug the following function?

Code: Select all

(setq we_sin (lambda (number precision)
	;number is real
	;precision MUST be an integer between 1 and 5
	(block (output)
	(setq output 0)
	(setq number (we_deg2rad number))
	(for i 0 precision
		(block (index)
			(setq index (+ (* 2 precision) 1))
			(setq output (+ output (* (/ (pow -1 precision) (we_factorial index)) (pow number index))))
		)
	)
	output
)))
https://upload.wikimedia.org/math/d/3/1 ... 7ee638.png
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

heh, found the problem! I was iterating in the for using "precision" instead of the variable "i".:P :P
silly me

now it works !

Precision is not great, but ok :D


How do I use scientific notation numbers ? I see them popping in the console but I can't use them myself ?
Post Reply