[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/uyaebBRj    [ raw code | output | fork | 1 comment ]

programmingpraxis - Scheme, pasted on Jan 10:
; calculating sines -- by Bill Cruise

(define epsilon 1e-7)

(define pi 3.141592654)

(define (factorial n)
  (fact-iter 1 1 n))

(define (fact-iter product counter max-count)
  (if (> counter max-count)
      product
      (fact-iter (* counter product)
                 (+ counter 1)
                 max-count)))

(define (term n radians)
  (* (/ (expt radians (+ (* 2 n) 1))
        (factorial (+ (* 2 n) 1)))
     (expt -1 n)))

(define (reduce x)
   (- x (* (round (/ x (* 2 pi))) 2 pi)))

(define (good-enough? current next)
  (< (abs (- current next)) epsilon))

(define (sine-iter radians n current next)
  (if (good-enough? current next)
      next
      (sine-iter radians (+ n 1) next (+ next
                                         (term (+ n 1) radians)))))

(define (taylor-sine radians)
  (sine-iter (reduce radians) 0 0 (term 0 (reduce radians))))

(display (taylor-sine 1)) (newline)
(display (taylor-sine (/ pi 2))) (newline)
(display (taylor-sine 10)) (newline)

(define (cube x) (* x x x))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sicp-sine angle)
   (if (not (> (abs angle) epsilon))
       angle
       (p (sicp-sine (/ angle 3.0)))))

(display (sicp-sine 1)) (newline)
(display (sicp-sine (/ pi 2))) (newline)
(display (sicp-sine 10)) (newline)


Output:
1
2
3
4
5
6
0.841470984648068
1.00000000066278
-0.5440211099969915
0.8414709848078971
1.0
-0.5440211108893757


Create a new paste based on this one


Comments:
posted by conan09 on Jan 9
Do you have a code in python? Thankyou

reply