-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath-postgresql.c
More file actions
321 lines (282 loc) · 9.09 KB
/
Copy pathmath-postgresql.c
File metadata and controls
321 lines (282 loc) · 9.09 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* $SchulteIT: math-postgresql.c 15189 2025-10-27 05:41:45Z schulte $ */
/* $JDTAUS: math-postgresql.c 9511 2026-06-13 19:28:18Z schulte $ */
/*
* Copyright (c) 2018 - 2026 Christian Schulte <cs@schulte.it>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_HOST_H
#include "host.h"
#endif
#include "heap.h"
#include "math.h"
#include "proc.h"
#include <limits.h>
#include <math.h>
#include <pgtypes_numeric.h>
struct Numeric {
numeric *restrict n;
#ifdef ABAG_MATH_DEBUG
char *restrict s;
#endif
};
extern const struct Numeric *restrict const zero;
extern const struct Numeric *restrict const one;
extern const struct Numeric *restrict const n_one;
inline struct Numeric *Numeric_new(void) {
numeric *res = PGTYPESnumeric_new();
if (res == NULL)
panic();
struct Numeric *restrict n = heap_malloc(sizeof(struct Numeric));
n->n = res;
#ifdef ABAG_MATH_DEBUG
n->s = NULL;
#endif
return n;
}
inline void Numeric_delete(void *restrict const n) {
if (n == NULL)
return;
struct Numeric *restrict const num = n;
PGTYPESnumeric_free(num->n);
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(num->s);
#endif
heap_free(num);
}
inline void *Numeric_db(const struct Numeric *restrict const n) { return n->n; }
inline struct Numeric *Numeric_from_char(const char *restrict const s) {
struct Numeric *restrict n = NULL;
// XXX: (char *)
numeric *res = PGTYPESnumeric_from_asc((char *)s, NULL);
if (res != NULL) {
n = heap_malloc(sizeof(struct Numeric));
n->n = res;
#ifdef ABAG_MATH_DEBUG
n->s = Numeric_to_char(n, 20);
#endif
}
return n;
}
inline char *Numeric_to_char(const struct Numeric *restrict const n,
const int d) {
char *restrict const s = PGTYPESnumeric_to_asc(n->n, d);
if (s == NULL)
panic();
return s;
}
inline void Numeric_char_free(char *restrict const s) { PGTYPESchar_free(s); }
inline struct Numeric *Numeric_add(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2) {
struct Numeric *restrict const res = Numeric_new();
Numeric_add_to(n1, n2, res);
return res;
}
inline void Numeric_add_to(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_add(n1->n, n2->n, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(res->s);
res->s = Numeric_to_char(res, 20);
#endif
}
inline struct Numeric *Numeric_sub(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2) {
struct Numeric *restrict const res = Numeric_new();
Numeric_sub_to(n1, n2, res);
return res;
}
inline void Numeric_sub_to(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_sub(n1->n, n2->n, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(res->s);
res->s = Numeric_to_char(res, 20);
#endif
}
inline struct Numeric *Numeric_mul(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2) {
struct Numeric *restrict const res = Numeric_new();
Numeric_mul_to(n1, n2, res);
return res;
}
inline void Numeric_mul_to(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_mul(n1->n, n2->n, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(res->s);
res->s = Numeric_to_char(res, 20);
#endif
}
inline struct Numeric *Numeric_div(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2) {
struct Numeric *restrict const res = Numeric_new();
Numeric_div_to(n1, n2, res);
return res;
}
inline void Numeric_div_to(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_div(n1->n, n2->n, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(res->s);
res->s = Numeric_to_char(res, 20);
#endif
}
inline int Numeric_cmp(const struct Numeric *restrict const n1,
const struct Numeric *restrict const n2) {
const int ret = PGTYPESnumeric_cmp(n1->n, n2->n);
if (ret == INT_MAX)
panic();
return ret;
}
inline struct Numeric *Numeric_from_int(const signed int i) {
struct Numeric *restrict const res = Numeric_new();
Numeric_from_int_to(i, res);
return res;
}
inline void Numeric_from_int_to(const signed int i,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_from_int(i, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
res->s = Numeric_to_char(res, 20);
#endif
}
inline int Numeric_to_int(const struct Numeric *restrict const n) {
int res = 0;
const int ret = PGTYPESnumeric_to_int(n->n, &res);
if (ret < 0)
panic();
return res;
}
inline struct Numeric *Numeric_from_long(const signed long int l) {
struct Numeric *restrict const res = Numeric_new();
Numeric_from_long_to(l, res);
return res;
}
inline void Numeric_from_long_to(const signed long int l,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_from_long(l, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
res->s = Numeric_to_char(res, 20);
#endif
}
inline long Numeric_to_long(const struct Numeric *restrict const n) {
long res = 0;
const int ret = PGTYPESnumeric_to_long(n->n, &res);
if (ret < 0)
panic();
return res;
}
inline struct Numeric *Numeric_copy(const struct Numeric *restrict const n) {
struct Numeric *restrict const res = Numeric_new();
Numeric_copy_to(n, res);
return res;
}
inline void Numeric_copy_to(const struct Numeric *restrict const n,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_copy(n->n, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(res->s);
res->s = Numeric_to_char(res, 20);
#endif
}
inline struct Numeric *Numeric_from_double(const double d) {
struct Numeric *res = Numeric_new();
Numeric_from_double_to(d, res);
return res;
}
inline void Numeric_from_double_to(const double d,
struct Numeric *restrict const res) {
const int ret = PGTYPESnumeric_from_double(d, res->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
res->s = Numeric_to_char(res, 20);
#endif
}
inline double Numeric_to_double(const struct Numeric *restrict const n) {
double res = 0;
const int ret = PGTYPESnumeric_to_double(n->n, &res);
if (ret < 0)
panic();
return res;
}
inline void Numeric_abs(struct Numeric *restrict const n) {
if (Numeric_cmp(n, zero) < 0) {
const int ret = PGTYPESnumeric_mul(n_one->n, n->n, n->n);
if (ret < 0)
panic();
}
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(n->s);
n->s = Numeric_to_char(n, 20);
#endif
}
inline void Numeric_scale(struct Numeric *restrict const n, const int scale) {
char *restrict const s = Numeric_to_char(n, scale);
struct Numeric *restrict const scaled = Numeric_from_char(s);
if (scaled == NULL)
panic();
Numeric_copy_to(scaled, n);
Numeric_delete(scaled);
Numeric_char_free(s);
}
inline void Numeric_inc(struct Numeric *restrict const n) {
const int ret = PGTYPESnumeric_add(n->n, one->n, n->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(n->s);
n->s = Numeric_to_char(n, 20);
#endif
}
inline void Numeric_dec(struct Numeric *restrict const n) {
const int ret = PGTYPESnumeric_sub(n->n, one->n, n->n);
if (ret < 0)
panic();
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(n->s);
n->s = Numeric_to_char(n, 20);
#endif
}
inline struct Numeric *Numeric_atan(const struct Numeric *restrict const n) {
struct Numeric *restrict const res = Numeric_new();
Numeric_atan_to(n, res);
return res;
}
inline void Numeric_atan_to(const struct Numeric *restrict const n,
struct Numeric *restrict const res) {
Numeric_from_double_to(atan(Numeric_to_double(n)), res);
#ifdef ABAG_MATH_DEBUG
Numeric_char_free(res->s);
res->s = Numeric_to_char(res, 20);
#endif
}