File: slib_math.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (282 lines) | stat: -rw-r--r-- 6,491 bytes parent folder | download | duplicates (3)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*  
 *                   COPYRIGHT (c) 1988-1994 BY                             *
 *        PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
 *        See the source file SLIB.C for more information.                  *

 * Reorganization of files (Mar 1999) by Alan W Black <awb@cstr.ed.ac.uk>

 * math functions

*/
#include <cstdio>
#include "siod.h"
#include "siodp.h"

LISP numberp(LISP x)
{if FLONUMP(x) return(truth); else return(NIL);}

static LISP lplus(LISP args)
{
    LISP l;
    double sum;
    for (sum=0.0,l=args; l != NIL; l=cdr(l))
    {
	if (NFLONUMP(car(l))) err("wrong type of argument to plus",car(l));
	sum += FLONM(car(l));
    }
    return flocons(sum);
}

static LISP ltimes(LISP args)
{
    LISP l;
    double product;
    for (product=1.0,l=args; l != NIL; l=cdr(l))
    {
	if (NFLONUMP(car(l))) err("wrong type of argument to times",car(l));
	product *= FLONM(car(l));
    }
    return flocons(product);
}

static LISP difference(LISP x,LISP y)
{if NFLONUMP(x) err("wrong type of argument(1st) to difference",x);
 if NFLONUMP(y) err("wrong type of argument(2nd) to difference",y);
 return(flocons(FLONM(x) - FLONM(y)));}

static LISP quotient(LISP x,LISP y)
{if NFLONUMP(x) err("wrong type of argument(1st) to quotient",x);
 if NFLONUMP(y) err("wrong type of argument(2nd) to quotient",y);
 return(flocons(FLONM(x)/FLONM(y)));}

static LISP greaterp(LISP x,LISP y)
{if NFLONUMP(x) err("wrong type of argument(1st) to greaterp",x);
 if NFLONUMP(y) err("wrong type of argument(2nd) to greaterp",y);
 if (FLONM(x)>FLONM(y)) return(truth);
 return(NIL);}

static LISP lessp(LISP x,LISP y)
{if NFLONUMP(x) err("wrong type of argument(1st) to lessp",x);
 if NFLONUMP(y) err("wrong type of argument(2nd) to lessp",y);
 if (FLONM(x)<FLONM(y)) return(truth);
 return(NIL);}

static LISP l_nint(LISP number)
{
    if (TYPEP(number,tc_flonum))
    {
	int iii = (int)(FLONM(number)+0.5);
	return flocons(iii);
    }
    else if (TYPEP(number,tc_symbol))
    {
	int iii = (int)(atof(get_c_string(number))+0.5);
	return flocons(iii);
    }
    else
	err("nint: argument not a number",number);

    return NIL;
}

static LISP l_log(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(log(FLONM(n)));
    else
	err("log: not a number",n);

    return NIL;
}

static LISP l_rand()
{
    double r = (double)abs(rand())/(double)RAND_MAX;
    
    return flocons(r);
}

static LISP l_srand(LISP seed)
{
    if (seed && (TYPEP(seed,tc_flonum)))
	srand((int) FLONM(seed));
    else
	err("srand: not a number", seed);
    return NIL;
}

static LISP l_exp(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(exp(FLONM(n)));
    else
	err("exp: not a number",n);
    return NIL;
}

static LISP l_sin(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(sin(FLONM(n)));
    else
	err("sin: not a number",n);
    return NIL;
}

static LISP l_cos(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(cos(FLONM(n)));
    else
	err("cos: not a number",n);
    return NIL;
}

static LISP l_tan(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(tan(FLONM(n)));
    else
	err("tan: not a number",n);
    return NIL;
}

static LISP l_asin(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(asin(FLONM(n)));
    else
	err("asin: not a number",n);
    return NIL;
}

static LISP l_acos(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(acos(FLONM(n)));
    else
	err("acos: not a number",n);
    return NIL;
}

static LISP l_atan(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(atan(FLONM(n)));
    else
	err("atan: not a number",n);
    return NIL;
}

static LISP l_sqrt(LISP n)
{
    if (n && (TYPEP(n,tc_flonum)))
	return flocons(sqrt(FLONM(n)));
    else
	err("sqrt: not a number",n);
    return NIL;
}

static LISP l_pow(LISP x, LISP y)
{
    if (x && (TYPEP(x,tc_flonum)) &&
	y && (TYPEP(y,tc_flonum)))
	return flocons(pow(FLONM(x),FLONM(y)));
    else
	err("pow: x or y not a number",cons(x,cons(y,NIL)));
    return NIL;
}

static LISP l_mod(LISP x, LISP y)
{
    if (x && (TYPEP(x,tc_flonum)) &&
	y && (TYPEP(y,tc_flonum)))
    {
	int a,b;

	a = (int)FLONM(x);
	b = (int)FLONM(y);
	if (b == 0)
	    err("mod: y cannot be 0",cons(x,cons(y,NIL)));

	return flocons((float)(a%b));
    }
    else
	err("mod: x or y not a number",cons(x,cons(y,NIL)));
    return NIL;
}

void init_subrs_math(void)
{
 init_subr_1("number?",numberp,
 "(number? DATA)\n\
  Returns t if DATA is a number, nil otherwise.");
 init_lsubr("+",lplus,
 "(+ NUM1 NUM2 ...)\n\
  Returns the sum of NUM1 and NUM2 ...  An error is given is any argument\n\
  is not a number.");
 init_subr_2("-",difference,
 "(- NUM1 NUM2)\n\
  Returns the difference between NUM1 and NUM2.  An error is given is any\n\
  argument is not a number.");
 init_lsubr("*",ltimes,
 "(* NUM1 NUM2 ...)\n\
  Returns the product of NUM1 and NUM2 ...  An error is given is any\n\
  argument is not a number.");
 init_subr_2("/",quotient,
 "(/ NUM1 NUM2)\n\
  Returns the quotient of NUM1 and NUM2.  An error is given is any\n\
  argument is not a number.");
 init_subr_2(">",greaterp,
 "(> NUM1 NUM2)\n\
  Returns t if NUM1 is greater than NUM2, nil otherwise.  An error is\n\
  given is either argument is not a number.");
 init_subr_2("<",lessp,
 "(< NUM1 NUM2)\n\
  Returns t if NUM1 is less than NUM2, nil otherwise.  An error is\n\
  given is either argument is not a number.");
 init_subr_1("nint",l_nint,
 "(nint NUMBER)\n\
  Returns nearest int to NUMBER.");
 init_subr_1("log",l_log,
 "(log NUM)\n\
 Return natural log of NUM.");
 init_subr_0("rand",l_rand,
 "(rand)\n\
 Returns a pseudo random number between 0 and 1 using the libc rand()\n\
 function.");
 init_subr_1("srand",l_srand,
 "(srand SEED)\n\
 Seeds the libc pseudo random number generator with the integer SEED.");
 init_subr_1("exp",l_exp,
 "(exp NUM)\n\
 Return e**NUM.");
 init_subr_1("sin",l_sin,
 "(sin NUM)\n\
 Return sine of NUM.");
 init_subr_1("cos",l_cos,
 "(cos NUM)\n\
 Return cosine of NUM.");
 init_subr_1("tan",l_tan,
 "(tan NUM)\n\
 Return tangent of NUM.");
 init_subr_1("asin",l_asin,
 "(asin NUM)\n\
 Return arcsine of NUM.");
 init_subr_1("acos",l_acos,
 "(acos NUM)\n\
 Return arccosine of NUM.");
 init_subr_1("atan",l_atan,
 "(atan NUM)\n\
 Return arctangent of NUM.");
 init_subr_1("sqrt",l_sqrt,
 "(sqrt NUM)\n\
 Return square root of NUM.");
 init_subr_2("pow",l_pow,
 "(pow X Y)\n\
 Return X**Y.");
 init_subr_2("%",l_mod,
 "(% X Y)\n\
 Return X%Y.");

}