OpenCores
URL https://opencores.org/ocsvn/tcp_socket/tcp_socket/trunk

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [examples/] [taylor.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 jondawson
/* taylor.c */
2
/* Jonathan P Dawson */
3
/* 2013-12-23 */
4
 
5
/* globals */
6
float pi=3.14159265359;
7
 
8
/* approximate the cosine function using Taylor series */
9
 
10
float taylor(float angle){
11
 
12
    float old, approximation, sign, power, fact;
13
    unsigned count, i;
14
 
15
    approximation = 1.0;
16
    old = 0.0;
17
    sign = -1.0;
18
    count = 1;
19
    power = 1.0;
20
    fact = 1.0;
21
 
22
    for(i=2; approximation!=old; i+=2){
23
        old = approximation;
24
 
25
        while(count<=i){
26
            power*=angle;
27
            fact*=count;
28
            count++;
29
        }
30
 
31
        approximation += sign*(power/fact);
32
        sign = -sign;
33
 
34
    }
35
    return approximation;
36
}
37
 
38
/* return the cosine of angle in radians */
39
 
40
float cos(float angle){
41
    return taylor(angle);
42
}
43
 
44
/* return the sine of angle in radians */
45
 
46
float sin(float angle){
47
    return cos(angle-(pi/2));
48
}
49
 
50
 
51
/* test routine */
52
 
53
void main(){
54
    float x;
55
    float step=pi/25;
56
 
57
    for(x=-2*pi; x <= 2*pi; x += step){
58
       file_write(x, "x");
59
       file_write(cos(x), "cos_x");
60
       file_write(sin(x), "sin_x");
61
    }
62
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.