-
Notifications
You must be signed in to change notification settings - Fork 3
/
mydecquad.c
714 lines (475 loc) · 17.8 KB
/
mydecquad.c
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#include "mydecquad.h"
/************************************************************************/
/* global constant for Round and Truncate */
/************************************************************************/
/* decNumber constants for decQuad rounding.
It contains
- 1e0
- 1e-1
- 1e-2
- ...
- 1e-DECQUAD_Pmax (1e-34)
*/
static decQuad G_DECQUAD_QUANTIZER[DECQUAD_Pmax+1]; // 0...34
/* decNumber constants for decQuad rounding.
It contains
- 1e0
- 1e1
- 1e2
- ...
- 1eDECQUAD_Pmax (1e34)
- 1e(DECQUAD_Pmax+1) (1e35)
NOTE: the max index is 35, because rounding 9234567890123456789012345678901234
with 10000000000000000000000000000000000 (1e34)
gives 10000000000000000000000000000000000
and rounding 9234567890123456789012345678901234
with 100000000000000000000000000000000000 (1e35)
gives 0
So, we must allow rounding functions to use an integral part quantizer of 1e35.
*/
static decQuad G_DECQUAD_INTEGRAL_PART_QUANTIZER[DECQUAD_Pmax+2]; // 0...35
/************************************************************************/
/* init and context */
/************************************************************************/
static decQuad static_one; // contains 1, only used by mdq_to_int64
/* initialize the global constants used by this library.
It is called by Go in init() function.
Exit(1) if an error occurs.
*/
void mdq_init(void) {
decContext set;
const char *s;
int i;
//----- check DECLITEND -----
if ( decContextTestEndian(1) ) { // if argument is 0, a warning message is displayed (using printf) if DECLITEND is set incorrectly. If 1, no message is displayed. Returns 0 if correct.
fprintf(stderr, "INITIALIZATION mydecquad.c:mdq_init() FAILED: decnum: decContextTestEndian() failed. Change DECLITEND constant (see \"The decNumber Library\")");
exit(1);
}
assert( DECQUAD_Pmax == 34 ); // we have 34 digits max precision (number of significant digits).
assert( DECQUAD_String > DECQUAD_Pmax ); // because Go function quad.AppendQuad() requires it
//----- put 1 in static_one -----
decQuadFromInt32(&static_one, 1); // IMPORTANT: this means that mdq_to_int64 can only be called after Go init() has been run, as it uses static_one. Method ToInt32() cannot be called to initialize Go global variables.
//----- fill decContext -----
decContextDefault(&set, DEC_INIT_DECQUAD);
if ( decContextGetRounding(&set) != DEC_ROUND_HALF_EVEN ) {
fprintf(stderr, "INITIALIZATION mydecquad.c:mdq_init() FAILED: decnum: decContextGetRounding(&set) != DEC_ROUND_HALF_EVEN");
exit(1);
}
//----- fill G_DECQUAD_QUANTIZER[] -----
decQuadFromInt32(&G_DECQUAD_QUANTIZER[0], 1); // store 1e0 in G_DECQUAD_QUANTIZER[0]
assert( decQuadDigits( &G_DECQUAD_QUANTIZER[0]) == 1 );
assert( decQuadGetExponent(&G_DECQUAD_QUANTIZER[0]) == 0 );
for ( i=1; i<=DECQUAD_Pmax; i++ ) { // in G_DECQUAD_QUANTIZER[1..DECQUAD_Pmax]
decQuadCopy(&G_DECQUAD_QUANTIZER[i], &G_DECQUAD_QUANTIZER[0]);
decQuadSetExponent(&G_DECQUAD_QUANTIZER[i], &set, -i); // store 1e-1 .. 1e-DECQUAD_Pmax
}
assert( decQuadDigits( &G_DECQUAD_QUANTIZER[DECQUAD_Pmax]) == 1 );
assert( decQuadGetExponent(&G_DECQUAD_QUANTIZER[DECQUAD_Pmax]) == -DECQUAD_Pmax ); // -34
//----- fill G_DECQUAD_INTEGRAL_PART_QUANTIZER[] -----
decQuadFromInt32(&G_DECQUAD_INTEGRAL_PART_QUANTIZER[0], 1); // store 1e0 in G_DECQUAD_INTEGRAL_PART_QUANTIZER[0]
assert( decQuadDigits( &G_DECQUAD_INTEGRAL_PART_QUANTIZER[0]) == 1 );
assert( decQuadGetExponent(&G_DECQUAD_INTEGRAL_PART_QUANTIZER[0]) == 0 );
for ( i=1; i<=DECQUAD_Pmax+1; i++ ) { // in G_DECQUAD_INTEGRAL_PART_QUANTIZER[1..DECQUAD_Pmax+1]
decQuadCopy(&G_DECQUAD_INTEGRAL_PART_QUANTIZER[i], &G_DECQUAD_INTEGRAL_PART_QUANTIZER[0]);
decQuadSetExponent(&G_DECQUAD_INTEGRAL_PART_QUANTIZER[i], &set, i); // store 1e1 .. 1e(DECQUAD_Pmax+1)
}
assert( decQuadDigits( &G_DECQUAD_INTEGRAL_PART_QUANTIZER[DECQUAD_Pmax]) == 1 );
assert( decQuadGetExponent(&G_DECQUAD_INTEGRAL_PART_QUANTIZER[DECQUAD_Pmax]) == DECQUAD_Pmax ); // 34
assert( decQuadDigits( &G_DECQUAD_INTEGRAL_PART_QUANTIZER[DECQUAD_Pmax+1]) == 1 );
assert( decQuadGetExponent(&G_DECQUAD_INTEGRAL_PART_QUANTIZER[DECQUAD_Pmax+1]) == DECQUAD_Pmax+1 ); // 35
//----- check for errors or any warning -----
if ( set.status ) {
s = decContextStatusToString(&set);
fprintf(stderr, "INITIALIZATION mydecquad.c:mdq_init() FAILED: decNumber quantizer initialization failed. %s\n", s);
exit(1);
}
}
/************************************************************************/
/* arithmetic operations */
/************************************************************************/
/* returns 0E0.
*/
decQuad mdq_zero() {
decQuad val;
decQuadZero(&val);
return val;
}
/* returns NaN.
*/
decQuad mdq_nan() {
decContext set;
decQuad val;
decContextDefault(&set, DEC_INIT_DECQUAD);
decQuadFromString(&val, "Nan", &set);
//assert(set.status & DEC_Errors == 0); // a status bit is set, because of Nan
return val;
}
/* unary minus.
*/
Quad mdq_minus(Quad a) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status;
decQuadMinus(&res.val, &a.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* addition.
*/
Quad mdq_add(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadAdd(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* subtraction.
*/
Quad mdq_subtract(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadSubtract(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* multiplication.
*/
Quad mdq_multiply(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadMultiply(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* division.
*/
Quad mdq_divide(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadDivide(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* integer division.
*/
Quad mdq_divide_integer(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadDivideInteger(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* modulo.
*/
Quad mdq_remainder(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadRemainder(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* max.
*/
Quad mdq_max(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadMax(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* min.
*/
Quad mdq_min(Quad a, Quad b) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadMin(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* to integral value.
*/
Quad mdq_to_integral(Quad a, int round) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status;
decQuadToIntegralValue(&res.val, &a.val, &set, round); // The DEC_Inexact flag is not set by this function, even if rounding ocurred.
res.status = decContextGetStatus(&set);
return res;
}
/* quantize.
*/
Quad mdq_quantize(Quad a, Quad b, int round) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
decContextSetRounding(&set, round); // change rounding mode
set.status = a.status | b.status;
decQuadQuantize(&res.val, &a.val, &b.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* absolute value.
*/
Quad mdq_abs(Quad a) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status;
decQuadAbs(&res.val, &a.val, &set);
res.status = decContextGetStatus(&set);
return res;
}
/************************************************************************/
/* is_finite, etc */
/************************************************************************/
/* check if a is Finite number.
*/
uint32_t mdq_is_finite(decQuad a) {
return decQuadIsFinite(&a);
}
/* check if a is integer number.
*/
uint32_t mdq_is_integer(decQuad a) {
return decQuadIsInteger(&a);
}
/* check if a is Infinite.
*/
uint32_t mdq_is_infinite(decQuad a) {
return decQuadIsInfinite(&a);
}
/* check if a is Nan.
*/
uint32_t mdq_is_nan(decQuad a) {
return decQuadIsNaN(&a);
}
/* check if a is > 0 and not Nan.
*/
uint32_t mdq_is_positive(decQuad a) {
return decQuadIsPositive(&a);
}
/* check if a is == 0.
*/
uint32_t mdq_is_zero(decQuad a) {
return decQuadIsZero(&a);
}
/* check if a is < 0 and not Nan.
*/
uint32_t mdq_is_negative(decQuad a) {
return decQuadIsNegative(&a);
}
/* get exponent.
*/
int32_t mdq_get_exponent(decQuad a) {
return decQuadGetExponent(&a);
}
/************************************************************************/
/* comparison */
/************************************************************************/
/* compare.
*/
uint32_t mdq_compare(Quad a, Quad b) {
decContext set;
decQuad cmp_val;
uint32_t res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status | b.status;
decQuadCompare(&cmp_val, &a.val, &b.val, &set); // result may be –1, 0, 1, or NaN. NaN is returned only if a or b is a NaN.
if ( decQuadIsNaN(&cmp_val) ) {
return CMP_NAN;
}
if ( decQuadIsZero(&cmp_val) ) {
return CMP_EQUAL;
}
if ( decQuadIsPositive(&cmp_val) ) {
return CMP_GREATER;
}
assert( decQuadIsNegative(&cmp_val) );
return CMP_LESS;
}
/************************************************************************/
/* conversion from string or numbers */
/************************************************************************/
/* conversion from string.
*/
Quad mdq_from_string(char *s) {
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
decQuadFromString(&res.val, s, &set);
res.status = decContextGetStatus(&set);
return res;
}
/* conversion from int32.
*/
Quad mdq_from_int32(int32_t value) {
Quad res;
decQuadFromInt32(&res.val, value);
res.status = 0; // never fails
return res;
}
/* conversion from int64.
*/
Quad mdq_from_int64(int64_t value) {
char buff[30]; // more than enough to store a int64 max val: 9,223,372,036,854,775,807
decContext set;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
sprintf(buff, "%lld", (long long int)value); // write value into buffer
decQuadFromString(&res.val, buff, &set); // raises an error if string is invalid
res.status = decContextGetStatus(&set);
return res;
}
/************************************************************************/
/* conversion to string */
/************************************************************************/
/* write decQuad into byte array.
A terminating 0 is written in the array.
Never fails.
The function decQuadToString() uses exponential notation too often in my opinion. E.g. 0.0000001 returns "1E-7".
Unlike the original function decQuadToString, this function discards '-' sign for negative zero: -0.00 is displayed as "0.00".
*/
Ret_str mdq_QuadToString(decQuad a) {
decQuad a_aux; // copy of a, but with negative sign discarded if a is negative zero
Ret_str res = {.length = 0};
if ( decQuadIsZero(&a) ) {
decQuadCopyAbs(&a_aux, &a); // discard '-' sign if any
decQuadToString(&a_aux, res.s);
} else {
decQuadToString(&a, res.s);
}
res.length = strlen(res.s);
return res;
}
/* write decQuad into BCD_array.
The returned fields are:
BCD: byte array. The coefficient is written one digit per byte.
exp: if a is not Inf or Nan, will contain the exponent.
sign: if negative and not zero, sign bit is set.
THE SIGN IS VALID ALSO IF THE FUNCTION RETURNS MDQ_INFINITE, so that we can know if it is +Inf or -Inf.
*/
Ret_BCD mdq_to_BCD(decQuad a) {
int32_t exp;
uint32_t sign;
Ret_BCD res = {.inf_nan = 0, .exp = 0, .sign = 0};
// convert to BCD
decQuadToBCD(&a, &exp, res.BCD); // this function returns a sign bit, but we don't use it because we don't want -0
sign = decQuadIsNegative(&a); // 0 is never negative
// check that result is not Inf nor Nan
if ( ! decQuadIsFinite(&a) ) {
if ( decQuadIsInfinite(&a) ) {
res.inf_nan = MDQ_INFINITE;
} else {
res.inf_nan = MDQ_NAN;
}
return res;
}
res.exp = exp;
res.sign = sign;
return res;
}
/************************************************************************/
/* conversion to numbers */
/************************************************************************/
/* convert decQuad to int32_t
*/
Ret_int32_t mdq_to_int32(Quad a, int round) {
decContext set;
Ret_int32_t res;
decContextDefault(&set, DEC_INIT_DECQUAD);
res.val = decQuadToInt32(&a.val, &set, round);
res.status = decContextGetStatus(&set);
return res;
}
/* convert decQuad to int64_t
*/
Ret_int64_t mdq_to_int64(Quad a, int round) {
decContext set;
decQuad a_integral;
decQuad a_integral_quantized;
char a_str[DECQUAD_String];
char *tailptr;
int64_t r_val;
Ret_int64_t res;
decContextDefault(&set, DEC_INIT_DECQUAD);
decQuadToIntegralValue(&a_integral, &a.val, &set, round); // rounds the number to an integral. Only numbers with exponent<0 are rounded and shifted so that exponent becomes 0.
decQuadQuantize(&a_integral_quantized, &a_integral, &static_one, &set); // for numbers with exponent>0. E.g. change 1e3 to 1000
if (set.status & DEC_Errors) {
res.status = decContextGetStatus(&set);
res.val = 0;
return res;
}
if (! decQuadIsFinite(&a_integral_quantized)) {
decContextSetStatus(&set, DEC_Invalid_operation);
res.status = decContextGetStatus(&set);
res.val = 0;
return res;
}
assert(decQuadGetExponent(&a_integral_quantized) == 0); // in the absence of decQuadQuantize error, the exponent of the result is always equal to that of the model 'static_one'
decQuadToString(&a_integral_quantized, a_str); // never raises error. Exponential notation never occurs for integral, which allows strtoll() to parse the number.
//printf("xxxxxxxxxxxxxx %s\n", a_str);
errno = 0;
r_val = strtoll(a_str, &tailptr, 10); // changes errno if error
if ( errno ) { // in particular, if a_str is an integer that overflows int64
decContextSetStatus(&set, DEC_Invalid_operation);
res.status = decContextGetStatus(&set);
res.val = 0;
return res;
}
if ( *tailptr != 0 ) { // may happen for e.g. 123e10, because it parses up to 'e'
decContextSetStatus(&set, DEC_Invalid_operation);
res.status = decContextGetStatus(&set);
res.val = 0;
return res;
}
res.status = decContextGetStatus(&set);
res.val = r_val;
return res;
}
/************************************************************************/
/* rounding and truncating */
/************************************************************************/
Quad mdq_roundM(Quad a, int32_t n, int round) {
decContext set;
decQuad r;
decQuad *operation_quantizer;
Quad res;
decContextDefault(&set, DEC_INIT_DECQUAD);
set.status = a.status;
// if n is out-of-range, return Invalid_operation
if ( n > 34 || n < -35 ) {
decContextSetStatus(&set, DEC_Invalid_operation); // add flag to status
res.val = mdq_nan();
res.status = decContextGetStatus(&set);
return res;
}
// operation
decContextSetRounding(&set, round); // change rounding mode
if ( n >= 0 ) { // round or truncate fractional part
operation_quantizer = &G_DECQUAD_QUANTIZER[n]; // n is [0..34]
decQuadQuantize(&res.val, &a.val, operation_quantizer, &set); // rounding, e.g. quaantize(1234.5678, 2) --> 1234.57
} else { // n < 0, round or truncate integral part
operation_quantizer = &G_DECQUAD_INTEGRAL_PART_QUANTIZER[-n]; // -n is [0..35]
decQuadQuantize(&r, &a.val, operation_quantizer, &set); // rounding, e.g. quaantize(1234.5678, -2) --> 12E2
decQuadQuantize(&res.val, &r, &G_DECQUAD_QUANTIZER[0], &set); // right-shift the number, adding missing 0s on the left. E.g. 12E2 --> 1200E0
}
res.status = decContextGetStatus(&set);
return res;
}