~arivigo/scalc

ref: 3ac4e521cc2f999b907bd544662a44a4f449e147 scalc/op.c -rw-r--r-- 4.5 KiB
3ac4e521Ariadna Vigo op.c: added todeg and torad conversion ops 3 months ago
                                                                                
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
/* See LICENSE file for copyright and license details. */

#include <math.h>
#include <stdint.h>
#include <string.h>

#include "op.h"
#include "utils.h"

#define OP_E 2.71828182845904523536
#define OP_PI 3.14159265358979323846

/*
 * EXTENDING OP.C:
 *
 * All mathematical operations take the form of a unary or binary function
 * (one double argument or two double arguments, respectively), always
 * returning a double as a result. All functions are to be defined static,
 * unless they're defined in an external library (e.g. log() and sqrt(), from
 * libm).
 *
 * Mathematical constants are defined as 0-ary functions.
 *
 * Once you've written your function, in order for the rpn.c module to actually
 * know about it, you must add it into the 'op_defs' array below. Leave the
 * 'dummy' entry at the end.
 *
 * The fields must *strictly* follow this format, where 'name' will be the
 * command users will call your function from scalc's prompt or scripts:
 *
 * { "name", N of arguments (N = 0, 1 or 2), { .nN = pointer-to-func } }
 */

static double op_add(double p, double q);
static double op_subst(double p, double q);
static double op_mult(double p, double q);
static double op_div(double p, double q);
static double op_prcnt(double n);
static double op_mod(double p, double q);
static double op_fact(double n);
static double op_npr(double n, double r);
static double op_ncr(double n, double r);
static double op_cot(double n);
static double op_sec(double n);
static double op_csc(double n);
static double op_acot(double n);
static double op_asec(double n);
static double op_acsc(double n);
static double op_todeg(double n);
static double op_torad(double n);

/* Constants */
static double op_cst_e(void);
static double op_cst_pi(void);

const OpReg op_defs[] = {
	{ "+", 2, { .n2 = op_add } },
	{ "-", 2, { .n2 = op_subst } },
	{ "*", 2, { .n2 = op_mult } },
	{ "/", 2, { .n2 = op_div } },
	{ "^", 2, { .n2 = pow } },
	{ "%", 1, { .n1 = op_prcnt } },
	{ "abs", 1, { .n1 = fabs } },
	{ "ln", 1, { .n1 = log } },
	{ "sqrt", 1, { .n1 = sqrt } },
	{ "mod", 2, { .n2 = op_mod } },
	{ "!", 1, { .n1 = op_fact } },
	{ "P", 2, { .n2 = op_npr } },
	{ "C", 2, { .n2 = op_ncr } },
	{ "sin", 1, { .n1 = sin } },
	{ "cos", 1, { .n1 = cos } },
	{ "tan", 1, { .n1 = tan } },
	{ "cot", 1, { .n1 = op_cot } },
	{ "sec", 1, { .n1 = op_sec } },
	{ "csc", 1, { .n1 = op_csc } },
	{ "asin", 1, { .n1 = asin } },
	{ "acos", 1, { .n1 = acos } },
	{ "atan", 1, { .n1 = atan } },
	{ "acot", 1, { .n1 = op_acot } },
	{ "asec", 1, { .n1 = op_asec } },
	{ "acsc", 1, { .n1 = op_acsc } },
	{ "todeg", 1, { .n1 = op_todeg } },
	{ "torad", 1, { .n1 = op_torad } },
	{ "e", 0, { .n0 = op_cst_e } },
	{ "pi", 0, { .n0 = op_cst_pi } },
	{ "", -1, { .n0 = NULL } } /* Dummy "terminator" entry */
};

static double
op_add(double p, double q)
{
	return p + q;
}

static double
op_subst(double p, double q)
{
	return p - q;
}

static double
op_mult(double p, double q)
{
	return p * q;
}

static double
op_div(double p, double q)
{
	return p / q;
}

static double
op_prcnt(double n)
{
	return n / 100;
}

static double
op_mod(double p, double q)
{
	return (double)((int64_t)p % (int64_t)q);
}

static double
op_fact(double n)
{
	double res, i;

	res = 1;
	for (i = n; i > 1; --i)
		res *= i;

	return res;
}

static double
op_npr(double n, double r)
{
	if (r > n)
		return 0;

	return op_fact(n) / op_fact(n - r);
}

static double
op_ncr(double n, double r)
{
	return op_fact(n) / (op_fact(r) * op_fact(n - r));
}

static double
op_cot(double n)
{
	return 1 / tan(n);
}

static double
op_sec(double n)
{
	return 1 / cos(n);
}

static double
op_csc(double n)
{
	return 1 / sin(n);
}

static double
op_acot(double n)
{
	return op_cst_pi() / 2 - atan(n);
}

static double
op_asec(double n)
{
	return acos(1 / n);
}

static double
op_acsc(double n)
{
	return asin(1 / n);
}

static double
op_todeg(double n)
{
	return n * 180 / op_cst_pi();
}

static double
op_torad(double n)
{
	return n * op_cst_pi() / 180;
}

static double
op_cst_e(void)
{
	return OP_E;
}

static double
op_cst_pi(void)
{
	return OP_PI;
}

const OpReg *
op(const char *oper)
{
	const OpReg *ptr;

	for (ptr = op_defs; op_valid(ptr) == 0; ++ptr) {
		if (strncmp(ptr->id, oper, OP_NAME_SIZE) == 0)
			return ptr;
	}

	/* If no match is found, we return the "Null" pointer */
	err = OP_ERR_INVALID;
	return ptr;
}

int
op_valid(const OpReg *ptr)
{
	switch (ptr->arg_n) {
	case 2:
		if (ptr->func.n0 == NULL)
			return -1;
		break;
	case 1:
		if (ptr->func.n1 == NULL)
			return -1;
		break;
	case 0:
		if (ptr->func.n0 == NULL)
			return -1;
		break;
	default:
		return -1;
	}

	return 0;
}