Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Sine Approximator Java

Name: Anonymous 2009-10-08 17:16

if you guys really think you are geniuses then write a java program that can approximate the sin function for any angle between 0 and 360 using taylor expansions

Name: Anonymous 2009-10-08 17:46

>>13
Someone beat me to posting a solution, but try this [warning! code has been barely tested]
(define (fact x)
  (if (zero? x)
      1
      (* x (fact (- x 1)))))

(define (taylor-series x max-power)
  (define (iter total power sign)
    (if (> power max-power)
        total
        (iter (sign total (/ (expt x power)
                             (fact power)))
              (+ power 2)
              (if (eq? sign +)
                  -
                  +))))
  (iter 0 1 +))

(define (deg->rad x)
  (* (/ x 180) pi))

(define (mysin x)
  (taylor-series (deg->rad x) 9)) ;<- 9 is a magic number

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List