User:Spekkio: Difference between revisions
No edit summary |
No edit summary |
||
(14 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
{{mylang|C|Active}} |
{{mylang|C|Active}} |
||
{{mylang|C++|Beginner}} |
{{mylang|C++|Beginner}} |
||
{{mylang|GLSL|Beginner}} |
|||
{{mylang|Java|Active}} |
{{mylang|Java|Active}} |
||
{{mylang|JavaScript|Active}} |
{{mylang|JavaScript|Active}} |
||
Line 16: | Line 17: | ||
{{mylangend}} |
{{mylangend}} |
||
<lang lisp>(defun pie() 31415926535897932384626433832795028841971693993751058209749/10000000000000000000000000000000000000000000000000000000000) |
|||
(defun factorial-rec (n) |
|||
Hello! I'm a Electronic Engineer, working with electronics developement. |
|||
I love computer programming, and my hobby is to program small simple computer programs. |
|||
I usually get stuck on an idea that I think would be fun to try and solve. |
|||
I love learning new programming languages. |
|||
Found this page recently, and it is perfect for me. :D |
|||
Currently I'm working on Huffman coding in C, and implementing a multi precision library to calculate |
|||
maclaurin series for different functions sin/cos/sqrt/pow etc... (see my recent post in [[Talk:Nth root]]) |
|||
<lang lisp> |
|||
(defun factorial (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) |
||
(powint a (- b 1) :result (* result a)) |
|||
(powint a (+ b 1) :result (* result (/ 1 a))) |
|||
) |
|||
) |
|||
) |
|||
(defun binom (alpha n) |
(defun binom (alpha n) |
||
(if (= n 0) |
|||
1 |
|||
(if (= n 1) |
(if (= n 1) |
||
alpha |
alpha |
||
(* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n)))) |
(* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n)))) |
||
) |
|||
(defun |
(defun power (a b n) |
||
(if (= n 0) |
(if (= n 0) |
||
1 |
1 |
||
(+ |
|||
(+ (* (binom b n) (powint (- a 1) n)) (pow a b (- n 1))))) |
|||
(* (binom b n) (powint (- a 1) n)) |
|||
(power a b (- n 1))))) |
|||
(defun powerw(a b n prev) |
|||
(defun logS(x n) (if (= n 0) (* 2 x) (+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) (logS x (- n 1)) ) )) |
|||
(let ((cosn (* (binom b n) (powint (- a 1) n)) |
|||
)) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(if (= (+ prev cosn) prev ) prev |
|||
(powerw a b (1+ n) (+ prev cosn))) |
|||
) |
|||
(arithmetic-error (x) prev )) |
|||
) |
|||
) |
|||
(defun |
(defun pow (a b) |
||
(if (< a 0) |
|||
(if (< b 1) |
|||
0 |
|||
(powerw a b 0 0) |
|||
) |
|||
(if (< a 2) |
|||
(powerw a b 0 0) |
|||
0 |
|||
) |
|||
) |
|||
) |
|||
(defun logw(x n prev) |
|||
(defun expon(x n) (if (= n 0) 1 (+ (/ (powint x n) (factorial n)) (expon x (- n 1))))) |
|||
(let ((cosn (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) |
|||
)) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(logw x (1+ n) (+ prev cosn)) |
|||
) |
|||
(arithmetic-error (x) prev )) |
|||
) |
|||
) |
|||
(defun |
(defun logS(x n) (if (= n 0) (* 2 x) |
||
(+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) |
|||
(logS x (- n 1)) ) )) |
|||
(defun |
(defun loge(x) (logw (/ (- x 1) (+ 1 x)) 0 0) ) |
||
(defun sine(x n) |
|||
(if (= n 0) |
|||
x |
|||
(+ (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) (sine x (- n 1))))) |
|||
(defun |
(defun logetest(a n prev) |
||
(let ((n10 (+ 10 n))) |
|||
(let (( log10 (loge a n10) )) |
|||
(if |
(if |
||
(= ( |
(= (float log10) (float prev)) |
||
log10 |
|||
( |
(logetest a n10 log10) |
||
) |
) |
||
) |
) |
||
)) |
|||
(defun |
(defun ln (x) |
||
(if (> x 0) |
|||
(loge x) |
|||
nil |
|||
)) |
|||
(defun expw(x n prev) |
|||
(defun cosi(x n) (if (= n 0) 1 (+ (* (/ (powint -1 n) (factorial (* 2 n)) ) (powint x (* 2 n))) (cosi x (- n 1))))) |
|||
(let ((cosn (/ (powint x n) (factorial n)) |
|||
)) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(expw x (1+ n) (+ prev cosn)) |
|||
) |
|||
(arithmetic-error (x) prev )) |
|||
) |
|||
) |
|||
(defun |
(defun expon(x) (expw x 0 0)) |
||
(defun |
(defun squareroot(x) (expon (* 1/2 (loge x)))) |
||
(if (= n 0) |
|||
x |
|||
(+ (/ (* (factorial (* 2 n)) (powint x (+ (* 2 n) 1))) (* (powint 4 n) (powint (factorial n) 2) (+ (* 2 n) 1))) (asine x (- n 1))))) |
|||
(defun powern(x a) (expon (* a (loge x)))) |
|||
(defun |
(defun e (x) (expon x)) |
||
(if |
|||
(defun pown (a b) (powern a b)) |
|||
(= (* (asine x (1+ n)) 1.0) (* (asine x n) 1.0)) |
|||
(asine x n) |
|||
(defun sinew(x &key (n 0) (prev 0)) |
|||
(let ((cosn (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) |
|||
) |
|||
)) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(sinew x :n (1+ n) :prev (+ prev cosn)) |
|||
) |
|||
(arithmetic-error (x) (+ prev cosn) )) |
|||
) |
|||
) |
) |
||
(defun |
(defun sine(x) (if (= x 0) 0 (sinew x))) |
||
(defun fibcalc (n p) |
|||
(defun cosinew(x &key (n 0) (prev 0)) |
|||
(let ((sqrtFive (sqrtn 5 p))) |
|||
(let ((cosn (* (/ (powint -1 n) (factorial (* 2 n))) (powint x (* 2 n))))) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(cosinew x :n (1+ n) :prev (+ prev cosn)) |
|||
) |
|||
(arithmetic-error (x) (+ prev cosn) )) |
|||
) |
|||
) |
|||
(defun cosine(x) (cosinew x)) |
|||
(defun tang(x) (/ (sine x) (cosine x))) |
|||
(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))) |
|||
)) |
|||
(handler-case |
|||
(progn |
|||
(float cosn) |
|||
(asinew x :n (1+ n) :prev (+ prev cosn)) |
|||
) |
|||
(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) |
|||
(if (< x 1) |
|||
(if (> x -1) |
|||
(asinew x) |
|||
(/ (pie) -2) |
|||
) |
|||
(/ (pie) 2) |
|||
) |
|||
) |
|||
(defun fibcalc (n) |
|||
(let ((sqrtFive (squareroot 5))) |
|||
(/ (- (powint (* 1/2 (+ 1 sqrtFive)) n) (powint (* 1/2 (- 1 sqrtFive)) n)) sqrtFive))) |
|||
(defun fibrec (n) |
(defun fibrec (n) |
||
Line 108: | Line 211: | ||
1 |
1 |
||
(+ (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: |
||
[ |
[14]> (float (asine 9/100)) |
||
0. |
0.09012195 |
||
[ |
[14]> (asin 9/100) |
||
0. |
0.090121955 |
||
Compare with Mathematica: |
Compare with Mathematica: |
||
In: N[ArcSin[ |
In[14]:= N[ArcSin[9/100], 8] |
||
Out |
Out[14]= 0.090121945 |
||
(myasin) seems to be more accurate, though trying to compute (myasin |
(myasin) seems to be more accurate, though trying to compute (myasin 9999999/10000000) is very slow. |
||
Test 2: |
Test 2: |
||
[4]> ( |
[4]> (float (asine (sine 1))) |
||
1.0 |
|||
0.9999999 |
|||
[4]> (asin (sin 1)) |
[4]> (asin (sin 1)) |
||
1.0 |
1.0 |
||
Test 3: |
|||
[15]> (float (sine (* 45 (/ (* 2 (pie)) 360) ))) |
|||
0.70710677 |
|||
[15]> (float (squareroot 1/2)) |
|||
0.70710677 |
|||
[15]> (= (float (squareroot 1/2)) (float (sine (* 45 (/ (* 2 (pie)) 360) )))) |
|||
T |
|||
[15]> (= (sine (* 45 (/ (* 2 (pie)) 360) )) (sine (/ (pie) 4))) |
|||
T |
|||
[15]> (= (cosine (/ (pie) 4)) (sine (/ (pie) 4))) |
|||
NIL |
|||
[15]> (= (float (cosine (/ (pie) 4))) (float (sine (/ (pie) 4)))) |
|||
T |
Latest revision as of 15:30, 27 August 2013
My Favorite Languages | |
Language | Proficiency |
C | Active |
C++ | Beginner |
GLSL | Beginner |
Java | Active |
JavaScript | Active |
SQL | Active |
Mathematica | Active |
MATLAB | Active |
UNIX Shell | Beginner |
Lisp | Beginner |
Erlang | Beginner |
POV-Ray | Beginner |
Pascal | Rusty |
x86 Assembly | Rusty |
VHDL | Rusty |
<lang lisp>(defun pie() 31415926535897932384626433832795028841971693993751058209749/10000000000000000000000000000000000000000000000000000000000)
(defun factorial-rec (n)
(if (= n 0) 1 (* n (factorial-rec (- n 1)))))
(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) result (if (> b 0) (powint a (- b 1) :result (* result a)) (powint a (+ b 1) :result (* result (/ 1 a))) )
) )
(defun binom (alpha n) (if (= n 0) 1
(if (= n 1) alpha (* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n))))
)
(defun power (a b n)
(if (= n 0) 1 (+
(* (binom b n) (powint (- a 1) n))
(power a b (- n 1)))))
(defun powerw(a b n prev) (let ((cosn (* (binom b n) (powint (- a 1) n)) )) (handler-case (progn (float cosn) (if (= (+ prev cosn) prev ) prev (powerw a b (1+ n) (+ prev cosn))) ) (arithmetic-error (x) prev )) ) )
(defun pow (a b)
(if (< a 0) (if (< b 1)
0 (powerw a b 0 0)
) (if (< a 2)
(powerw a b 0 0)
0 ) )
)
(defun logw(x n prev) (let ((cosn (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1))) )) (handler-case (progn (float cosn) (logw x (1+ n) (+ prev cosn)) ) (arithmetic-error (x) prev )) ) )
(defun logS(x n) (if (= n 0) (* 2 x) (+ (* 2 (/ (powint x (+ (* 2 n) 1)) (+ (* 2 n) 1)))
(logS x (- n 1)) ) ))
(defun loge(x) (logw (/ (- x 1) (+ 1 x)) 0 0) )
(defun logetest(a n prev) (let ((n10 (+ 10 n))) (let (( log10 (loge a n10) ))
(if (= (float log10) (float prev)) log10 (logetest a n10 log10) )
) ))
(defun ln (x) (if (> x 0) (loge x) nil ))
(defun expw(x n prev) (let ((cosn (/ (powint x n) (factorial n)) )) (handler-case (progn (float cosn) (expw x (1+ n) (+ prev cosn)) ) (arithmetic-error (x) prev )) ) )
(defun expon(x) (expw x 0 0))
(defun squareroot(x) (expon (* 1/2 (loge x))))
(defun powern(x a) (expon (* a (loge x))))
(defun e (x) (expon x))
(defun pown (a b) (powern a b))
(defun sinew(x &key (n 0) (prev 0)) (let ((cosn (* (/ (powint -1 n) (factorial (+ (* 2 n) 1)) ) (powint x (+ (* 2 n) 1))) )) (handler-case (progn (float cosn) (sinew x :n (1+ n) :prev (+ prev cosn)) ) (arithmetic-error (x) (+ prev cosn) )) ) )
(defun sine(x) (if (= x 0) 0 (sinew x)))
(defun cosinew(x &key (n 0) (prev 0)) (let ((cosn (* (/ (powint -1 n) (factorial (* 2 n))) (powint x (* 2 n))))) (handler-case (progn (float cosn) (cosinew x :n (1+ n) :prev (+ prev cosn)) ) (arithmetic-error (x) (+ prev cosn) )) ) )
(defun cosine(x) (cosinew x))
(defun tang(x) (/ (sine x) (cosine x)))
(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))) )) (handler-case (progn (float cosn) (asinew x :n (1+ n) :prev (+ prev cosn)) ) (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)
(if (< x 1) (if (> x -1)
(asinew x) (/ (pie) -2) )
(/ (pie) 2) )
)
(defun fibcalc (n)
(let ((sqrtFive (squareroot 5))) (/ (- (powint (* 1/2 (+ 1 sqrtFive)) n) (powint (* 1/2 (- 1 sqrtFive)) n)) sqrtFive)))
(defun fibrec (n)
(if (= n 0) 0 (if (= n 1)
1
(+ (fibrec (- n 1)) (fibrec (- n 2))))))
(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:
[14]> (float (asine 9/100)) 0.09012195 [14]> (asin 9/100) 0.090121955
Compare with Mathematica:
In[14]:= N[ArcSin[9/100], 8] Out[14]= 0.090121945
(myasin) seems to be more accurate, though trying to compute (myasin 9999999/10000000) is very slow.
Test 2:
[4]> (float (asine (sine 1))) 1.0 [4]> (asin (sin 1)) 1.0
Test 3:
[15]> (float (sine (* 45 (/ (* 2 (pie)) 360) ))) 0.70710677 [15]> (float (squareroot 1/2)) 0.70710677 [15]> (= (float (squareroot 1/2)) (float (sine (* 45 (/ (* 2 (pie)) 360) )))) T [15]> (= (sine (* 45 (/ (* 2 (pie)) 360) )) (sine (/ (pie) 4))) T [15]> (= (cosine (/ (pie) 4)) (sine (/ (pie) 4))) NIL [15]> (= (float (cosine (/ (pie) 4))) (float (sine (/ (pie) 4)))) T