User:Spekkio: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 35: | Line 35: | ||
<lang lisp>(defun pie() 31415926535897932384626433832795028841971693993751058209749/10000000000000000000000000000000000000000000000000000000000) |
<lang lisp>(defun pie() 31415926535897932384626433832795028841971693993751058209749/10000000000000000000000000000000000000000000000000000000000) |
||
(defun factorial (n) |
(defun factorial-rec (n) |
||
(if (= n 0) |
(if (= n 0) |
||
1 |
1 |
||
(* n (factorial (- n 1))))) |
(* n (factorial-rec (- n 1))))) |
||
(defun |
(defun factorial (n &key (result 1)) |
||
(if (< n 2) |
|||
result |
|||
(factorial (- n 1) :result (* result n)))) |
|||
(defun powint (a b &key (result 1)) |
|||
(if (= b 0) |
(if (= b 0) |
||
result |
|||
(if (> b 0) |
(if (> b 0) |
||
(powint a (- b 1) :result (* result a)) |
|||
( |
(powint a (+ b 1) :result (* result (/ 1 a))) |
||
) |
) |
||
) |
) |
||
Line 148: | Line 153: | ||
(defun pown (a b) (powern a b)) |
(defun pown (a b) (powern a b)) |
||
(defun sinew(x n prev) |
(defun sinew(x &key (n 0) (prev 0)) |
||
(let ((cosn (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) |
(let ((cosn (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) |
||
)) |
)) |
||
Line 154: | Line 159: | ||
(progn |
(progn |
||
(float cosn) |
(float cosn) |
||
(sinew x (1+ n) (+ prev cosn)) |
(sinew x :n (1+ n) :prev (+ prev cosn)) |
||
) |
) |
||
(arithmetic-error (x) (+ prev cosn) )) |
(arithmetic-error (x) (+ prev cosn) )) |
||
Line 160: | Line 165: | ||
) |
) |
||
(defun sine(x) ( |
(defun sine(x) (if (= x 0) 0 (sinew x))) |
||
(defun cosinew(x n prev) |
(defun cosinew(x &key (n 0) (prev 0)) |
||
(let ((cosn (* (/ (powint -1 n) (factorial (* 2 n))) (powint x (* 2 n))))) |
(let ((cosn (* (/ (powint -1 n) (factorial (* 2 n))) (powint x (* 2 n))))) |
||
(handler-case |
(handler-case |
||
(progn |
(progn |
||
(float cosn) |
(float cosn) |
||
(cosinew x (1+ n) (+ prev cosn)) |
(cosinew x :n (1+ n) :prev (+ prev cosn)) |
||
) |
) |
||
(arithmetic-error (x) (+ prev cosn) )) |
(arithmetic-error (x) (+ prev cosn) )) |
||
Line 173: | Line 178: | ||
) |
) |
||
(defun cosine(x) (cosinew x |
(defun cosine(x) (cosinew x)) |
||
(defun tang(x) (/ (sine x) (cosine x))) |
(defun tang(x) (/ (sine x) (cosine x))) |
||
(defun asinew(x n prev) |
(defun asinew(x &key (n 0) (prev 0)) |
||
(let ((cosn (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) |
(let ((cosn (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) |
||
)) |
)) |
||
Line 183: | Line 188: | ||
(progn |
(progn |
||
(float cosn) |
(float cosn) |
||
(asinew x (1+ n) (+ prev cosn)) |
(asinew x :n (1+ n) :prev (+ prev cosn)) |
||
) |
) |
||
(arithmetic-error (x) prev )) |
(arithmetic-error (x) prev )) |
||
) |
) |
||
) |
) |
||
(defun asinew2(x) |
|||
(let ((n 0)) |
|||
(let ((next 0)) |
|||
(loop while (handler-case (float next) (arithmetic-error (err) NIL)) do |
|||
(progn |
|||
(setq next (+ (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) next)) |
|||
(setq n (+ n 1)) |
|||
) |
|||
) |
|||
))) |
|||
(defun asine (x) |
(defun asine (x) |
||
(if (< x 1) |
(if (< x 1) |
||
(if (> x -1) |
(if (> x -1) |
||
(asinew x |
(asinew x) |
||
(/ (pie) -2) |
(/ (pie) -2) |
||
) |
) |
||
Line 210: | Line 228: | ||
(+ (fibrec (- n 1)) (fibrec (- n 2)))))) |
(+ (fibrec (- n 1)) (fibrec (- n 2)))))) |
||
</lang> |
|||
(defun test() (let ((n 0)) (loop while (< n 10) collect n do (if (= (float (sine n)) (sin n)) () (progn (write n) (write (terpri)) (write (sin n)) (write (terpri)) (write (float (sine n))) (write (terpri)) )) (setq n (+ n 1/10)))))</lang> |
|||
test: |
test: |