User:Spekkio: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 116: Line 116:


Compare with Mathematica:
Compare with Mathematica:
In: N[ArcSin[1/10], 8]
In: N[ArcSin[1/10], 10]
Out: 0.10016742
Out: 0.1001674212


(myasin) seems to be more accurate, though trying to compute (myasin 1) is very slow.
(myasin) seems to be more accurate, though trying to compute (myasin 1) is very slow.

Revision as of 10:35, 7 December 2011

My Favorite Languages
Language Proficiency
C Active
C++ 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


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)
     1
   (* n (factorial (- n 1)))))

(defun powint (a b)

 (if (= b 0)
     1
   (* a (powint a (- b 1)))))

(defun binom (alpha n)

 (if (= n 1)
     alpha
   (* (binom alpha (- n 1)) (/ (+ (- alpha n) 1) n))))

(defun pow (a b n)

 (if (= n 0)
     1
   (+ (* (binom b n) (powint (- a 1) n)) (pow a b (- n 1)))))

(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 n) (logS (/ (- x 1) (+ 1 x)) n))

(defun expon(x n) (if (= n 0) 1 (+ (/ (powint x n) (factorial n)) (expon x (- n 1)))))

(defun sqrtn(x n) (expon (* 1/2 (loge x n)) n))

(defun pown(x a n) (expon (* a (loge x n)) n))

(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 sinetest(x n)

 (if
     (= (* (sine x (1+ n)) 1.0) (* (sine x n) 1.0))
     (sine x n)
   (sinetest x (1+ n))
     )

)

(defun mysin (x) (sinetest x 1))

(defun cosi(x n) (if (= n 0) 1 (+ (* (/ (powint -1 n) (factorial (* 2 n)) ) (powint x (* 2 n))) (cosi x (- n 1)))))

(defun tang(x n) (/ (sine x n) (cosi x n)))

(defun asine (x n) (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 asinetest(x n)

 (if
     (= (* (asine x (1+ n)) 1.0) (* (asine x n) 1.0))
     (asine x n)
   (asinetest x (1+ n))
     )

)

(defun myasin (x) (asinetest x 1)) (defun fibcalc (n p)

 (let ((sqrtFive (sqrtn 5 p)))
 (/ (- (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))))))

</lang>

test:

[3]> (asin 1/10)
0.100167416
[3]> (* (myasin 1/10) 1.0)
0.10016742

Compare with Mathematica:

In: N[ArcSin[1/10], 10]
Out: 0.1001674212

(myasin) seems to be more accurate, though trying to compute (myasin 1) is very slow.