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

Pages: 1-

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:17

It'd be trivial in lisp.

Name: Anonymous 2009-10-08 17:17

>>1
You mean, you want us to do your high school homework?

Name: Anonymous 2009-10-08 17:21

Why would you do that when most popular CPUs offer specialized instructions for calculating trig functions. Most compilers can inline to them. This would only have any importance if you were doing arbitrary precision floating point calculations.

Name: Anonymous 2009-10-08 17:24

hint:
terms of taylor series are x - (x^3/3!) + (x^5/5!) - (x^7/7!) + (x^9/9!)

Name: Anonymous 2009-10-08 17:26

>>3
high school?  i'm not there yet i'm not american.
ok stfu you gonna help soon or not?

Name: Anonymous 2009-10-08 17:26

>>2
Yeah why don't they teach us better languages?
I taught myself python before taking this java course and Java is so lame next to it. I've read about lisp and yeah it would probably be alot easier.. I think we should be allowed to use any language we want for these assignments. Schools are bullshit

Name: Anonymous 2009-10-08 17:28

>>6
1. Neither am I, I'm British. It is however the widely accepted term.
2. Why the fuck should I?

Name: Anonymous 2009-10-08 17:29

>>1
Why would you want to find the value of sin x for x ∈ [0, 360] and not x ∈ [0, 2π]?

Name: Anonymous 2009-10-08 17:30

Name: Anonymous 2009-10-08 17:35

because they want us to convert radians to degrees

Name: Anonymous 2009-10-08 17:39

>>4
Actually, I think you have to use expansions for trigonometric functions on most RISC architectures like MIPS and ARM.

Name: Anonymous 2009-10-08 17:39

According to http://upload.wikimedia.org/math/e/7/2/e72a9c97103eed0fe72a1975a8fd748a.png and I have no idea if it's correct or not

(defun factorial (x)                               
  (reduce #'* (loop for i from 1 upto x collect i)))

(defun help (x)
  (let (result
        (n 0))
    (lambda ()   
      (setq result          
            (* (/ (expt -1 n)             
                  (factorial (1+ (* 2 n))))
               (expt x (1+ (* 2 n)))))
      (incf n)
      result)))
                            
(defun doit (x approximation)
  (do* ((i 0 (1+ i))
        (f (help x))
        (y (funcall f)
           (if (oddp i)
               (- y (funcall f))  
               (+ y (funcall f)))))
       ((= i approximation) y)))

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

Name: Anonymous 2009-10-08 17:50

I think you should read SICP OP.

Here's a somewhat crappy version I wrote a few minutes ago:

;;; This definition needs proper optimization directives on
;;; some implementations for it to do TCO, otherwise I would just have used a LOOP
(defun factorial (x)
  (labels ((rec (x acc)
         (if (< x 2)  
         acc    
         (rec (1- x) (* x acc)))))
    (rec x 1)))

(defun slow-sin (x &optional (precision 0.001))
  "Calculates sin x with a given precision. Returns a ratio. "
  (let ((current-value x)
    (step 1)
    (sign nil))
    (labels ((next-sin ()
           (incf current-value
             (* (if sign 1 -1)
            (/ (expt x step) (factorial step))))
           (incf step 2)
           current-value))
      (let ((result (next-sin)))
    (loop until (< (- result (next-sin)) precision)
         do (setf result current-value))
    result))))

(defun inexact-sin (x &optional (precision 0.00001))
  "Calculates sin x with a given precision. Returns a floating point value."
  (coerce (slow-sin x precision) 'float))

Name: Anonymous 2009-10-08 17:50

Fuck, it seems 2 people beat me to it.

Name: Anonymous 2009-10-08 17:53

>>16
Don't let that stop you.

Name: Anonymous 2009-10-08 17:54

>>14

(filter (lambda (x) (< (abs (- (sin x) (mysin x))) 0.00001))
          (unfold (lambda (x) (> x 360))
          (lambda (x) x)
          (lambda (x) (+ x 1))
          1))
()

Seems fine to me :)

Name: Anonymous 2009-10-08 17:56

Some one enterprise this.

Name: Anonymous 2009-10-08 18:04

>>19
and give the OP what he wants? never. we've helped him enough already ;)

Name: Anonymous 2009-10-08 18:08

>>20
You can't use ENTERPRISE code.  That's the point.  Only a dumbass would turn that in.  But I agree, /prog/ has helped too much and we don't want this to turn into the homework board like last year.

Name: Anonymous 2009-10-08 18:14

>>21
Only a dumbass would turn that in.
Depends on the school I'd guess, If the teacher worked in the industry he might cream himself at the thought of a TaylorSeriesFactoryFactoryManagerBuilder or some such nonsense

Name: Anonymous 2009-10-09 21:19

OP here, I finally worked it out for myself. Here is my code:
<code>
import java.util.Scanner;
public class SineApproximator {
  public static void main(String[] args) {

    final double PI = 3.141592653589793;
   
    //obtain user input

    double angleDegrees, x, epsilon;
 
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter an angle (in degrees): ");
    angleDegrees = keyboard.nextDouble();
    System.out.println("Enter the tolerance: ");
    epsilon = keyboard.nextDouble();

    x = ((angleDegrees * PI) / 180);              //convert  angle to radians
   
    int i;
    double t_i, sum;

    i = 0;        
    t_i = x;   // because i = 0
    sum = x;   //partial sum of t_0 is x
   
    while ((t_i > epsilon )||(t_i < -epsilon)) {  //condition: t_i > abs(epsilon)
      t_i = (t_i*(-1)*x*x)/((2*i+3)*(2*i+2));     //formula (1)
      sum = sum + t_i;                            //Adds each term and stores partial sums
      i = i + 1;                                 
    }
    System.out.println("The sine of this angle is: " + sum);
    System.out.println("The number of terms required: " + (i +1) );
  }
}
</code>

Name: lattdddie 2009-10-10 1:31

<A href="http://toshima.awdccs.com/">出会い豊島区</A><A href="http://kita.awdccs.com/">出会い北区</A><A href="http://arakawa.awdccs.com/">出会い荒川区</A><A href="http://itabashi.awdccs.com/">出会い板橋区</A><A href="http://nerima.awdccs.com/">出会い練馬区</A><A href="http://adachi.awdccs.com/">出会い足立区</A><A href="http://katushika.awdccs.com/">出会い葛飾区</A><A href="http://edogawa.awdccs.com/">出会い江戸川区</A><A href="http://nikibi.goteck.com/">ニキビ</A><A href="http://www.ibf-usba.com/">Cholesterol</A><A href="http://mizumushi.p-qq.net/">水虫</A><A href="http://hieshou.goteck.com/">冷え性</A><A href="http://mukumi.goteck.com/">むくみ</A><A href="http://sango.goteck.com/">産後</A><A href="http://wakiga.goteck.com/">わきが</A><A href="http://medikaru.com/">車買取</A><A href="http://www.s-tsk.com/">転職</A><A href="http://kareisyu.goteck.com/">加齢臭</A>

Name: lattdddie 2009-10-10 1:32

<A href="http://tokyo.awdccs.com/">出会い東京</A><A href="http://tottori.awdccs.com/">出会い鳥取</A><A href="http://toyama.awdccs.com/">出会い富山</A><A href="http://wakayama.awdccs.com/">出会い和歌山</A><A href="http://yamagata.awdccs.com/">出会い山形</A><A href="http://yamaguchi.awdccs.com/">出会い山口</A><A href="http://yamanashi.awdccs.com/">出会い山梨</A><A href="http://kitakyuusyuu.awdccs.com/">出会い北九州</A><A href="http://shimonoseki.awdccs.com/">出会い下関</A><A href="http://kawasaki.awdccs.com/">出会い川崎</A><A href="http://koube.awdccs.com/">出会い神戸</A><A href="http://kurume.awdccs.com/">出会い久留米</A><A href="http://mito.awdccs.com/">出会い水戸</A><A href="http://nagoya.awdccs.com/">出会い名古屋</A><A href="http://oomuta.awdccs.com/">出会い大牟田</A><A href="http://saitama-shi.awdccs.com/">出会い埼玉</A><A href="http://sakai.awdccs.com/">出会い堺</A><A href="http://sendai.awdccs.com/">出会い仙台</A><A href="http://yokohama.awdccs.com/">出会い横浜</A><A href="http://yokosuka.awdccs.com/">出会い横須賀</A><A href="http://sapporo.awdccs.com/">出会い札幌</A><A href="http://kawasaki.awdccs.com/">出会い川崎</A>

Name: lattdddie 2009-10-10 1:33

<A href="http://apiencompass.com/fukui/">福井出会い</A><A href="http://apiencompass.com/aichi/">愛知出会い</A><A href="http://apiencompass.com/gifu/">岐阜出会い</A><A href="http://apiencompass.com/shizuoka/">静岡出会い</A><A href="http://apiencompass.com/mie/">三重出会い</A><A href="http://apiencompass.com/hyogo/">兵庫出会い</A><A href="http://apiencompass.com/osaka/">大阪出会い</A><A href="http://apiencompass.com/wakayama/">和歌山出会い</A><A href="http://apiencompass.com/shiga/">滋賀出会い</A><A href="http://apiencompass.com/kyoto/">京都出会い</A><A href="http://apiencompass.com/nara/">奈良出会い</A><A href="http://apiencompass.com/yamaguchi/">山口出会い</A><A href="http://apiencompass.com/tottori/">鳥取出会い</A><A href="http://apiencompass.com/shimane/">島根出会い</A><A href="http://apiencompass.com/okayama/">岡山出会い</A><A href="http://apiencompass.com/hiroshima/">広島出会い</A><A href="http://apiencompass.com/tokushima/">徳島出会い</A><A href="http://apiencompass.com/kagawa/">香川出会い</A><A href="http://apiencompass.com/ehime/">愛媛出会い</A><A href="http://apiencompass.com/kochi/">高知出会い</A>

Name: lattdddie 2009-10-10 1:33

<A href="http://www.is-pro.com/">出会い</A><A href="http://www.is-pro.com/p/hokaidou1.html">札幌出会い</A><A href="http://www.is-pro.com/p/hokaidou2.html">函館出会い</A><A href="http://www.is-pro.com/p/hokaidou3.html">北海道出会い</A><A href="http://www.is-pro.com/p/akita1.html">秋田出会い</A><A href="http://www.is-pro.com/p/aomori1.html">青森出会い</A><A href="http://www.is-pro.com/p/iwate1.html">岩手出会い</A><A href="http://www.is-pro.com/p/tokyo1.html">東京出会い</A><A href="http://www.is-pro.com/p/tokyo2.html">八王子出会い</A><A href="http://www.is-pro.com/p/tokyo3.html">府中出会い</A><A href="http://www.is-pro.com/p/tokyo4.html">調布出会い</A><A href="http://www.is-pro.com/p/tokyo5.html">銀座出会い</A><A href="http://www.is-pro.com/p/miyagi1.html">仙台出会い</A><A href="http://eurosweaters.com/">優良出会いサイト</A><A href="http://www.logikacourse.com/">アダルト</A><A href="http://tousatu.info/">盗撮</A><A href="http://brainiactive.com/">素人</A><A href="http://delamododge.com/">熟女</A><A href="http://ero--anime.com/">エロアニメ</A><A href="http://av-zyoyuu.com/">AV女優</A>

Name: lattdddie 2009-10-10 1:34

<A href="http://aichi.archercomm.com/">出会い愛知</A><A href="http://akita.archercomm.com/">出会い秋田</A><A href="http://aomori.archercomm.com/">出会い青森</A><A href="http://chiba.archercomm.com/">出会い千葉</A><A href="http://ehime.archercomm.com/">出会い愛媛</A><A href="http://fukui.archercomm.com/">出会い福井</A><A href="http://fukuoka.archercomm.com/">出会い福岡</A><A href="http://fukushima.archercomm.com/">出会い福島</A><A href="http://gifu.archercomm.com/">出会い岐阜</A><A href="http://gunma.archercomm.com/">出会い群馬</A><A href="http://hiroshima.archercomm.com/">出会い広島</A><A href="http://hokaidou.archercomm.com/">出会い北海道</A><A href="http://hyogo.archercomm.com/">出会い兵庫</A><A href="http://ibaragi.archercomm.com/">出会い茨城</A><A href="http://ishikawa.archercomm.com/">出会い石川</A><A href="http://iwate.archercomm.com/">出会い岩手</A><A href="http://kagawa.archercomm.com/">出会い香川</A><A href="http://kagoshima.archercomm.com/">出会い鹿児島</A><A href="http://kanagawa.archercomm.com/">出会い神奈川</A><A href="http://kouchi.archercomm.com/">出会い高知</A>

Name: lattdddie 2009-10-10 1:34

<A href="http://kumamoto.archercomm.com/">出会い熊本</A><A href="http://kyoto.archercomm.com/">出会い京都</A><A href="http://mie.archercomm.com/">出会い三重</A><A href="http://miyagi.archercomm.com/">出会い宮城</A><A href="http://miyazaki.archercomm.com/">出会い宮崎</A><A href="http://nagano.archercomm.com/">出会い長野</A><A href="http://archercomm.com/">出会い長崎</A><A href="http://nara.archercomm.com/">出会い奈良</A><A href="http://niigata.archercomm.com/">出会い新潟</A><A href="http://oita.archercomm.com/">出会い大分</A><A href="http://okayama.archercomm.com/">出会い岡山</A><A href="http://okinawa.archercomm.com/">出会い沖縄</A><A href="http://oosaka.archercomm.com/">出会い大阪</A><A href="http://saga.archercomm.com/">出会い佐賀</A><A href="http://saitama.archercomm.com/">出会い埼玉</A><A href="http://shiga.archercomm.com/">出会い滋賀</A><A href="http://shimane.archercomm.com/">出会い島根</A><A href="http://shizuoka.archercomm.com/">出会い静岡</A><A href="http://tochigi.archercomm.com/">出会い栃木</A><A href="http://tokushima.archercomm.com/">出会い徳島</A>

Name: lattdddie 2009-10-10 1:34

<A href="http://dks.g-ded.com/fukui/">福井出会いカフェ</A><A href="http://dks.g-ded.com/aichi/">愛知出会いカフェ</A><A href="http://dks.g-ded.com/gifu/">岐阜出会いカフェ</A><A href="http://dks.g-ded.com/shizuoka/">静岡出会いカフェ</A><A href="http://dks.g-ded.com/mie/">三重出会いカフェ</A><A href="http://dks.g-ded.com/hyogo/">兵庫出会いカフェ</A><A href="http://dks.g-ded.com/osaka/">大阪出会いカフェ</A><A href="http://dks.g-ded.com/wakayama/">和歌山出会いカフェ</A><A href="http://dks.g-ded.com/shiga/">滋賀出会いカフェ</A><A href="http://dks.g-ded.com/kyoto/">京都出会いカフェ</A><A href="http://dks.g-ded.com/nara/">奈良出会いカフェ</A><A href="http://dks.g-ded.com/yamaguchi/">山口出会いカフェ</A><A href="http://dks.g-ded.com/tottori/">鳥取出会いカフェ</A><A href="http://dks.g-ded.com/shimane/">島根出会いカフェ</A><A href="http://dks.g-ded.com/okayama/">岡山出会いカフェ</A><A href="http://dks.g-ded.com/hiroshima/">広島出会いカフェ</A><A href="http://dks.g-ded.com/tokushima/">徳島出会いカフェ</A><A href="http://dks.g-ded.com/kagawa/">香川出会いカフェ</A><A href="http://dks.g-ded.com/ehime/">愛媛出会いカフェ</A><A href="http://dks.g-ded.com/kochi/">高知出会いカフェ</A>

Name: lattdddie 2009-10-10 1:35

<A href="http://www.tokyo-lin.com/">出会い東京</A><A href="http://www.osaka-borde.com/">出会い大阪</A><A href="http://www.fukuokakyotei.com/">出会い福岡</A><A href="http://www.hyogo-ken.net/">出会い兵庫</A><A href="http://www.kanagawajet.com/">出会い神奈川</A><A href="http://www.miyagi-fnavi.com/">出会い宮城</A><A href="http://www.chibaq.com/">出会い千葉</A><A href="http://www.aichicity.com/">出会い愛知</A><A href="http://www.saitamacom.com/">出会い埼玉</A><A href="http://www.aomori-shi.com/">出会い青森</A><A href="http://www.iwatejet.org/">出会い岩手</A><A href="http://www.akitatsuhan.com/">出会い秋田</A><A href="http://www.chukou-yamagata.com/">出会い山形</A><A href="http://www.1trustmortgage.com/">出会い山口</A><A href="http://tokyo.archercomm.com/">出会い東京</A><A href="http://tottori.archercomm.com/">出会い鳥取</A><A href="http://toyama.archercomm.com/">出会い富山</A><A href="http://wakayama.archercomm.com/">出会い和歌山</A><A href="http://yamagata.archercomm.com/">出会い山形</A>

Name: lattdddie 2009-10-10 1:35

<A href="http://compostreet.com/nara/">出会い奈良</A><A href="http://compostreet.com/shiga/">出会い滋賀</A><A href="http://compostreet.com/mie/">出会い三重</A>・<A href="http://compostreet.com/wakayama/">出会い和歌山</A><A href="http://compostreet.com/aichi/">出会い愛知</A>・<A href="http://compostreet.com/shizuoka/">出会い静岡</A>・<A href="http://compostreet.com/gifu/">出会い岐阜</A><A href="http://compostreet.com/hiroshima/">出会い広島</A>・<A href="http://compostreet.com/okayama/">出会い岡山</A>・<A href="http://compostreet.com/yamaguchi/">出会い山口</A>・<A href="http://compostreet.com/shimane/">出会い島根</A>・<A href="http://compostreet.com/tottori/">出会い鳥取</A><A href="http://compostreet.com/toyama/">出会い富山</A>・<A href="http://compostreet.com/ishikawa/">出会い石川</A>・<A href="http://compostreet.com/fukui/">出会い福井</A><A href="http://compostreet.com/tokushima/">出会い徳島</A>・<A href="http://compostreet.com/kagawa/">出会い香川</A>・<A href="http://compostreet.com/ehime/">出会い愛媛</A>・<A href="http://compostreet.com/kochi/">出会い高知</A><A href="http://compostreet.com/fukuoka/">出会い福岡</A>

Name: lattdddie 2009-10-10 1:36

<A href="http://www.deainy.com/nara/">セフレ奈良</A><A href="http://www.deainy.com/shiga/">セフレ滋賀</A><A href="http://www.deainy.com/mie/">セフレ三重</A>・<A href="http://www.deainy.com/wakayama/">セフレ和歌山</A><A href="http://www.deainy.com/aichi/">セフレ愛知</A>・<A href="http://www.deainy.com/shizuoka/">セフレ静岡</A>・<A href="http://www.deainy.com/gifu/">セフレ岐阜</A><A href="http://www.deainy.com/hiroshima/">セフレ広島</A>・<A href="http://www.deainy.com/okayama/">セフレ岡山</A>・<A href="http://www.deainy.com/yamaguchi/">セフレ山口</A>・<A href="http://www.deainy.com/shimane/">セフレ島根</A>・<A href="http://www.deainy.com/tottori/">セフレ鳥取</A><A href="http://www.deainy.com/toyama/">セフレ富山</A>・<A href="http://www.deainy.com/ishikawa/">セフレ石川</A>・<A href="http://www.deainy.com/fukui/">セフレ福井</A><A href="http://www.deainy.com/tokushima/">セフレ徳島</A>・<A href="http://www.deainy.com/kagawa/">セフレ香川</A>・<A href="http://www.deainy.com/ehime/">セフレ愛媛</A>・<A href="http://www.deainy.com/kochi/">セフレ高知</A><A href="http://www.deainy.com/fukuoka/">セフレ福岡</A>

Name: lattdddie 2009-10-10 1:36

<A href="http://www.g-ded.com/fukui/">福井テレクラ</A><A href="http://www.g-ded.com/aichi/">愛知テレクラ</A><A href="http://www.g-ded.com/gifu/">岐阜テレクラ</A><A href="http://www.g-ded.com/shizuoka/">静岡テレクラ</A><A href="http://www.g-ded.com/mie/">三重テレクラ</A><A href="http://www.g-ded.com/hyogo/">兵庫テレクラ</A><A href="http://www.g-ded.com/osaka/">大阪テレクラ</A><A href="http://www.g-ded.com/wakayama/">和歌山テレクラ</A><A href="http://www.g-ded.com/shiga/">滋賀テレクラ</A><A href="http://www.g-ded.com/kyoto/">京都テレクラ</A><A href="http://www.g-ded.com/nara/">奈良テレクラ</A><A href="http://www.g-ded.com/yamaguchi/">山口テレクラ</A><A href="http://www.g-ded.com/tottori/">鳥取テレクラ</A><A href="http://www.g-ded.com/shimane/">島根テレクラ</A><A href="http://www.g-ded.com/okayama/">岡山テレクラ</A><A href="http://www.g-ded.com/hiroshima/">広島テレクラ</A><A href="http://www.g-ded.com/tokushima/">徳島テレクラ</A><A href="http://www.g-ded.com/kagawa/">香川テレクラ</A><A href="http://www.g-ded.com/ehime/">愛媛テレクラ</A><A href="http://www.g-ded.com/kochi/">高知テレクラ</A>

Name: Anonymous 2011-02-02 23:34

Name: Anonymous 2012-03-26 21:02

Name: Anonymous 2012-03-26 21:17

Math.sin();

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