-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathfunc.c
More file actions
198 lines (169 loc) · 2.77 KB
/
Copy pathmathfunc.c
File metadata and controls
198 lines (169 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* TABULA VIGILANS: mathematical functions
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "tv.h"
#include "rules.h"
#include "xtab.h"
int prog_argc;
char **prog_argv;
double
mf_abs(double val)
{
return fabs(val);
}
double
mf_argc(void)
{
return (double)prog_argc;
}
double
mf_argv(double idx)
{
int i = (int)(idx + 0.5)-1;
return (i < 0 || i >= prog_argc) ? 0.0 : atof(prog_argv[i]);
}
char *
mf_args(double idx)
{
int i = (int)(idx + 0.5)-1;
return (i < 0 || i >= prog_argc) ? "" : prog_argv[i];
}
double
mf_dimensions(Table *tab)
{
if(tab == 0) {
fprintf(stderr, "TV: attempt made to find number of dimensions of null table variable\n");
tidy_up(7);
}
return (double)tab->tabdim.dimensions;
}
double
mf_dimsize(Table *tab, double fdim)
{
int dim = (int)(fdim-1);
char str[60];
if(tab == 0) {
fprintf(stderr,
"TV: attempt made to find dimension-size of null table variable\n");
tidy_up(7);
}
if(dim < 0 || dim >= tab->tabdim.dimensions) {
sprintf(str,
"TV: table does not have dimension %d\n", dim+1);
fprintf(stderr, str);
tidy_up(7);
}
return (double)tab->tabdim.sizes[dim];
}
double
mf_gamma(void)
{
int i;
int limit = (1 + (int)(drand48() * 10.0))*2;
double result, sum = 1.0;
for(i = 0; i < limit; i++)
sum *= drand48();
result = -log(sum)/(limit * 3.0);
return (result > 1.0 ? 1.0 : result);
}
double
mf_gauss(void)
{
int i, num = 12;
double result, sum = 0.0, halfnum = num/2.0;
for(i = 0; i < num; i++)
sum += drand48();
result = ((sum - halfnum) + 3.0)/halfnum;
result = (result < 0.0 ? 0.0 : result);
return(result > 1.0 ? 1.0: result);
}
double
mf_power(double in1, double in2)
{
double frac; /* fractional part of power arg */
if(in1 < 0)
{
frac = fmod(in1, 1.0);
if(frac)
{
return 0.0;
}
}
return pow(in1, in2);
}
double
mf_rand(void)
{
return drand48();
}
double
mf_random(double low, double high)
{
return low + drand48()*(high-low);
}
double
mf_round(double d)
{
return floor(d+0.5);
}
double
mf_sine(double input)
{
return sin(input);
}
/* New maths functions */
double
mf_cosine(double input)
{
return cos(input);
}
double
mf_tangent(double input)
{
return tan(input);
}
double
mf_arcsine(double input)
{
return asin(input);
}
double
mf_arccosine(double input)
{
return acos(input);
}
double
mf_arctangent(double input)
{
return atan(input);
}
double
mf_natlog(double input) /* RO added 5/11/05 */
{
return log(fabs(input)); /* log will not accept negative input! */
}
double
mf_log10(double input) /* RO added 10/11/05 */
{
if(input == 0.0) {
return 0;
}
return log10(fabs(input));
}
/* end of new */
double
mf_sqrt(double input)
{
return sqrt(input);
}
#ifdef _WIN32
double
drand48(void)
{
return (double)rand()/(double)RAND_MAX;
}
#endif