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

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [chips2/] [examples/] [math.h] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 jondawson
/* This is a very incomplete version of libc math.h
2
 * Not all the funtions and Macros are implemented.
3
 * It has not been tested.
4
 * Special cases have not been catered for*/
5
 
6
 
7
/* globals */
8
const float M_PI=3.14159265359;
9
 
10
/*Taylor series approximation of Cosine function*/
11
 
12
float taylor(float angle){
13
 
14
    float old, approximation, sign, power, fact;
15
    unsigned count, i;
16
 
17
    approximation = 1.0;
18
    old = 0.0;
19
    sign = -1.0;
20
    count = 1;
21
    power = 1.0;
22
    fact = 1.0;
23
 
24
    for(i=2; approximation!=old; i+=2){
25
        old = approximation;
26
 
27
        while(count<=i){
28
            power*=angle;
29
            fact*=count;
30
            count++;
31
        }
32
 
33
        approximation += sign*(power/fact);
34
        sign = -sign;
35
 
36
    }
37
    return approximation;
38
}
39
 
40
/*return cos of angle in radians*/
41
 
42
float cos(float angle){
43
    return taylor(angle);
44
}
45
 
46
/*return sin of angle in radians*/
47
 
48
float sin(float angle){
49
    return cos(angle-(M_PI/2));
50
}
51
 
52
/*return tan of angle in radians*/
53
 
54
float tan(float n){
55
    return sin(n) / cos(n);
56
}
57
 
58
/* Return absolute value of a float n*/
59
 
60
float fabs(float n){
61
    if (n < 0.0) {
62
        return - n;
63
    } else {
64
        return n;
65
    }
66
}
67
 
68
/* Return absolute value of integer n*/
69
 
70
int abs(int n){
71
    if (n < 0) {
72
        return - n;
73
    } else {
74
        return n;
75
    }
76
}
77
 
78
/* return e ** x */
79
 
80
float exp(float x){
81
 
82
    float result = 1.0;
83
    unsigned n = 1;
84
    float power = 1.0;
85
    float factorial = 1.0;
86
    float old = 0.0;
87
 
88
    while(fabs(old - result) > 0.000001){
89
        old = result;
90
        power *= x;
91
        factorial *= n;
92
        result += (power/factorial);
93
        n++;
94
    }
95
 
96
    return result;
97
 
98
}
99
 
100
/* return log_e(n) */
101
 
102
float log(float n){
103
    float antilog, x, old;
104
    x = 10.0;
105
    old = 0.0;
106
    while(fabs(old - x) > 0.000001){
107
        old = x;
108
        antilog = exp(x);
109
        x -= (antilog - n)/antilog;
110
    }
111
    return x;
112
}
113
 
114
/* return log_10(n) */
115
 
116
float log10(float n){
117
    return log(n)/log(10);
118
}
119
 
120
/* return log_2(n) */
121
 
122
float log2(float n){
123
    return log(n)/log(2);
124
}

powered by: WebSVN 2.1.0

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