-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSDecimal.m
More file actions
1679 lines (1459 loc) · 37.9 KB
/
NSDecimal.m
File metadata and controls
1679 lines (1459 loc) · 37.9 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
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
NSDecimal functions
Copyright (C) 2000 Free Software Foundation, Inc.
Written by: Fred Kiefer <FredKiefer@gmx.de>
Created: July 2000
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSDecimal class reference</title>
$Date$ $Revision$
*/
#import "common.h"
#include <math.h>
#if !defined(__APPLE__) || !defined(GNU_RUNTIME)
#include <ctype.h>
#endif
#import "Foundation/NSDecimal.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSUserDefaults.h"
#ifndef NAN
#define NAN 0.0
#endif
/*
This file provides two implementations of the NSDecimal functions.
One is based on pure simple decimal mathematics, as we all learned it
in school. This version is rather slow and may be inexact in the extreme
cases.
THIS IS TESTED AND WORKING.
The second implemenation requires the GMP library, the GNU math package,
to do the hard work. This is very fast and accurate. But as GMP is not
available on all computers this has to be switched on at compile time.
THIS IS STILL NOT IMPLEMENTED.
The data structure used for NSDecimals is a bit strange. It also does not
correspond to the description in the OpenStep specification. But this is
not consistent, so a decision had to be made.
The mantissa part (I know D. Knuth does not like this term, but it is used
in the specification so we stay with it) consists of up to 38 digits, this
are stored as an integer (in decimal representation or limps depending on the
USE_GMP flag). And the exponent is stored in a signed character. As a result
the numbers that can be represented are the ranges from -9(38 times)*10**127
to -1*10**-128, the number 0 and 1*10**-128 to 9(38 times)*10**127.
This means we have more big numbers than one would expect (almost up to 10**165)
but small numbers can only be represented with limited exactness (one digit
for -128, two for -127 and so on).
I think this is as close as possible to the specification, but other
interpretations are also valid. (Changing the exponent either absolut
[eg minus 38] or relative to the number of digits in the mantissa [minus length].)
*/
#if USE_GMP
// Define GSDecimal as using a character vector
typedef struct {
signed char exponent; /* Signed exponent - -128 to 127 */
BOOL isNegative; /* Is this negative? */
BOOL validNumber; /* Is this a valid number? */
unsigned char length; /* digits in mantissa. */
unsigned char cMantissa[2*NSDecimalMaxDigit]; /* Make this big enough for multiplication */
} GSDecimal;
static NSDecimal zero = {0, NO, YES, 0, {0}};
static NSDecimal one = {0, NO, YES, 1, {1}};
#define NSDECIMAL_IS_ZERO(num) (0 == num->size)
#define GSDECIMAL_IS_ZERO(num) (0 == num->length)
#else
// Make GSDecimal a synonym of NSDecimal
/** <code>
typedef struct {<br/>
signed char exponent; // Signed exponent - -128 to 127<br/>
BOOL isNegative; // Is this negative?<br/>
BOOL validNumber; // Is this a valid number?<br/>
unsigned char length; // digits in mantissa.<br/>
unsigned char cMantissa[2*NSDecimalMaxDigit];<br/>
}<br/>
</code> */
typedef NSDecimal GSDecimal;
static NSDecimal zero = {0, NO, YES, 0, {0}};
static NSDecimal one = {0, NO, YES, 1, {1}};
#define NSDECIMAL_IS_ZERO(num) (0 == num->length)
#define GSDECIMAL_IS_ZERO(num) (0 == num->length)
#endif
GS_DECLARE void
NSDecimalCopy(NSDecimal *destination, const NSDecimal *source)
{
memcpy(destination, source, sizeof(NSDecimal));
}
static void
GSDecimalCompact(GSDecimal *number)
{
int i, j;
//NSLog(@"Compact start %@ ", NSDecimalString(number, nil));
if (!number->validNumber)
return;
// Cut off leading 0's
for (i = 0; i < number->length; i++)
{
if (number->cMantissa[i] != 0)
break;
}
if (i > 0)
{
for (j = 0; j < number->length-i; j++)
{
number->cMantissa[j] = number->cMantissa[j+i];
}
number->length -= i;
}
// Cut off trailing 0's
for (i = number->length-1; i >= 0; i--)
{
if (0 == number->cMantissa[i])
{
if (127 == number->exponent)
{
// Overflow in compacting!!
// Leave the remaining 0s there.
break;
}
number->length--;
number->exponent++;
}
else
break;
}
if (GSDECIMAL_IS_ZERO(number))
{
number->exponent = 0;
number->isNegative = NO;
}
//NSLog(@"Compact end %@ ", NSDecimalString(number, nil));
}
static NSComparisonResult
GSDecimalCompare(const GSDecimal *leftOperand, const GSDecimal *rightOperand)
{
int i, l;
int s1 = leftOperand->exponent + leftOperand->length;
int s2 = rightOperand->exponent + rightOperand->length;
if (leftOperand->validNumber != rightOperand->validNumber)
{
if (rightOperand->validNumber)
return NSOrderedDescending;
else
return NSOrderedAscending;
}
if (leftOperand->isNegative != rightOperand->isNegative)
{
if (rightOperand->isNegative)
return NSOrderedDescending;
else
return NSOrderedAscending;
}
// Same sign, check size
if (s1 < s2)
{
if (rightOperand->isNegative)
return NSOrderedDescending;
else
return NSOrderedAscending;
}
if (s1 > s2)
{
if (rightOperand->isNegative)
return NSOrderedAscending;
else
return NSOrderedDescending;
}
// Same size, check digits
l = MIN(leftOperand->length, rightOperand->length);
for (i = 0; i < l; i++)
{
int d = rightOperand->cMantissa[i] - leftOperand->cMantissa[i];
if (d > 0)
{
if (rightOperand->isNegative)
return NSOrderedDescending;
else
return NSOrderedAscending;
}
if (d < 0)
{
if (rightOperand->isNegative)
return NSOrderedAscending;
else
return NSOrderedDescending;
}
}
// Same digits, check length
if (leftOperand->length > rightOperand->length)
{
if (rightOperand->isNegative)
return NSOrderedAscending;
else
return NSOrderedDescending;
}
if (leftOperand->length < rightOperand->length)
{
if (rightOperand->isNegative)
return NSOrderedDescending;
else
return NSOrderedAscending;
}
return NSOrderedSame;
}
static NSComparisonResult
NSSimpleCompare(const NSDecimal *leftOperand, const NSDecimal *rightOperand);
static void
GSDecimalRound(GSDecimal *result, int scale, NSRoundingMode mode)
{
int i;
// last valid digit in number
int l = scale + result->exponent + result->length;
if (NSDecimalNoScale == scale)
return;
if (!result->validNumber)
return;
if (result->length <= l)
return;
else if (l < 0)
{
result->length = 0;
result->exponent = 0;
result->isNegative = NO;
return;
}
else
{
int c, n;
BOOL up;
if (l == 0)
{
int x;
x = result->length;
result->length += 1;
l += 1;
while (x > 0)
{
result->cMantissa[x] = result->cMantissa[x-1];
x--;
}
result->cMantissa[0] = 0;
}
// Adjust length and exponent
result->exponent += result->length - l;
result->length = l;
switch (mode)
{
case NSRoundDown:
up = result->isNegative;
break;
case NSRoundUp:
up = !result->isNegative;
break;
case NSRoundPlain:
n = result->cMantissa[l];
up = (n >= 5);
break;
case NSRoundBankers:
n = result->cMantissa[l];
if (n > 5)
{
up = YES;
}
else if (n < 5)
{
up = NO;
}
else
{
c = result->cMantissa[l-1];
up = ((c % 2) != 0);
}
break;
default: // No way to get here
up = NO;
break;
}
if (up)
{
for (i = l-1; i >= 0; i--)
{
if (result->cMantissa[i] != 9)
{
result->cMantissa[i]++;
break;
}
result->cMantissa[i] = 0;
}
// Final overflow?
if (-1 == i)
{
// As all digits are zeros, just change the first
result->cMantissa[0] = 1;
if (127 == result->exponent)
{
// Overflow in rounding!!
// Add one zero add the end. There must be space as
// we just cut off some digits.
result->cMantissa[l] = 0;
result->length++;
}
else
result->exponent++;
}
}
}
GSDecimalCompact(result);
}
static NSCalculationError
GSDecimalNormalize(GSDecimal *n1, GSDecimal *n2, NSRoundingMode mode)
{
// Both are valid numbers and the exponents are not equal
int e1 = n1->exponent;
int e2 = n2->exponent;
int i, l;
// make sure n2 has the bigger exponent
if (e1 > e2)
{
GSDecimal *t;
t = n1;
n1 = n2;
n2 = t;
i = e2;
e2 = e1;
e1 = i;
}
// Add zeros to n2, as far as possible
l = MIN(NSDecimalMaxDigit - n2->length, e2 - e1);
for (i = 0; i < l; i++)
{
n2->cMantissa[i + n2->length] = 0;
}
n2->length += l;
n2->exponent -= l;
if (l != e2 - e1)
{
// Round of some digits from n1 to increase exponent
GSDecimalRound(n1, -n2->exponent, mode);
if (n1->exponent != n2->exponent)
{
// Some zeros where cut of again by compacting
l = MIN(NSDecimalMaxDigit - n1->length, n1->exponent - n2->exponent);
for (i = 0; i < l; i++)
{
n1->cMantissa[(NSInteger)n1->length] = 0;
n1->length++;
}
n1->exponent = n2->exponent;
}
return NSCalculationLossOfPrecision;
}
return NSCalculationNoError;
}
static NSCalculationError
GSSimpleAdd(NSDecimal *result, const NSDecimal *left, const NSDecimal *right,
NSRoundingMode mode);
GS_DECLARE NSCalculationError
NSDecimalAdd(NSDecimal *result, const NSDecimal *left, const NSDecimal *right,
NSRoundingMode mode)
{
NSCalculationError error = NSCalculationNoError;
NSCalculationError error1;
NSDecimal n1;
NSDecimal n2;
NSComparisonResult comp;
if (!left->validNumber || !right->validNumber)
{
result->validNumber = NO;
return error;
}
// check for zero
if (NSDECIMAL_IS_ZERO(left))
{
NSDecimalCopy(result, right);
return error;
}
if (NSDECIMAL_IS_ZERO(right))
{
NSDecimalCopy(result, left);
return error;
}
// For different signs use subtraction
if (left->isNegative != right->isNegative)
{
if (left->isNegative)
{
NSDecimalCopy(&n1, left);
n1.isNegative = NO;
return NSDecimalSubtract(result, right, &n1, mode);
}
else
{
NSDecimalCopy(&n1, right);
n1.isNegative = NO;
return NSDecimalSubtract(result, left, &n1, mode);
}
}
NSDecimalCopy(&n1, left);
NSDecimalCopy(&n2, right);
error = NSDecimalNormalize(&n1, &n2, mode);
comp = NSSimpleCompare(&n1, &n2);
/*
NSLog(@"Add left %@ right %@", NSDecimalString(left, nil),
NSDecimalString(right, nil));
NSLog(@"Add n1 %@ n2 %@ comp %d", NSDecimalString(&n1, nil),
NSDecimalString(&n2, nil), comp);
*/
// both negative, make positive
if (left->isNegative)
{
n1.isNegative = NO;
n2.isNegative = NO;
// SimpleCompare does not look at sign
if (NSOrderedDescending == comp)
{
error1 = GSSimpleAdd(result, &n1, &n2, mode);
}
else
{
error1 = GSSimpleAdd(result, &n2, &n1, mode);
}
result->isNegative = YES;
if (NSCalculationUnderflow == error1)
error1 = NSCalculationOverflow;
else if (NSCalculationOverflow == error1)
error1 = NSCalculationUnderflow;
}
else
{
if (NSOrderedAscending == comp)
{
error1 = GSSimpleAdd(result, &n2, &n1, mode);
}
else
{
error1 = GSSimpleAdd(result, &n1, &n2, mode);
}
}
NSDecimalCompact(result);
if (NSCalculationNoError == error1)
return error;
else
return error1;
}
static NSCalculationError
GSSimpleSubtract(NSDecimal *result, const NSDecimal *left,
const NSDecimal *right, NSRoundingMode mode);
GS_DECLARE NSCalculationError
NSDecimalSubtract(NSDecimal *result, const NSDecimal *left,
const NSDecimal *right, NSRoundingMode mode)
{
NSCalculationError error = NSCalculationNoError;
NSCalculationError error1;
NSDecimal n1;
NSDecimal n2;
NSComparisonResult comp;
if (!left->validNumber || !right->validNumber)
{
result->validNumber = NO;
return error;
}
if (NSDECIMAL_IS_ZERO(right))
{
NSDecimalCopy(result, left);
return error;
}
if (NSDECIMAL_IS_ZERO(left))
{
NSDecimalCopy(result, right);
result->isNegative = !result->isNegative;
return error;
}
// For different signs use addition
if (left->isNegative != right->isNegative)
{
if (left->isNegative)
{
NSDecimalCopy(&n1, left);
n1.isNegative = NO;
error1 = NSDecimalAdd(result, &n1, right, mode);
result->isNegative = YES;
if (NSCalculationUnderflow == error1)
error1 = NSCalculationOverflow;
else if (NSCalculationOverflow == error1)
error1 = NSCalculationUnderflow;
return error1;
}
else
{
NSDecimalCopy(&n1, right);
n1.isNegative = NO;
return NSDecimalAdd(result, left, &n1, mode);
}
}
NSDecimalCopy(&n1, left);
NSDecimalCopy(&n2, right);
error = NSDecimalNormalize(&n1, &n2, mode);
comp = NSDecimalCompare(left, right);
/*
NSLog(@"Sub left %@ right %@", NSDecimalString(left, nil),
NSDecimalString(right, nil));
NSLog(@"Sub n1 %@ n2 %@ comp %d", NSDecimalString(&n1, nil),
NSDecimalString(&n2, nil), comp);
*/
if (NSOrderedSame == comp)
{
NSDecimalCopy(result, &zero);
return NSCalculationNoError;
}
// both negative, make positive and change order
if (left->isNegative)
{
n1.isNegative = NO;
n2.isNegative = NO;
if (NSOrderedAscending == comp)
{
error1 = GSSimpleSubtract(result, &n1, &n2, mode);
result->isNegative = YES;
}
else
{
error1 = GSSimpleSubtract(result, &n2, &n1, mode);
}
}
else
{
if (NSOrderedAscending == comp)
{
error1 = GSSimpleSubtract(result, &n2, &n1, mode);
result->isNegative = YES;
}
else
{
error1 = GSSimpleSubtract(result, &n1, &n2, mode);
}
}
NSDecimalCompact(result);
if (NSCalculationNoError == error1)
return error;
else
return error1;
}
static NSCalculationError
GSSimpleMultiply(NSDecimal *result, NSDecimal *l, NSDecimal *r,
NSRoundingMode mode);
GS_DECLARE NSCalculationError
NSDecimalMultiply(NSDecimal *result, const NSDecimal *l, const NSDecimal *r,
NSRoundingMode mode)
{
NSCalculationError error = NSCalculationNoError;
NSDecimal n1;
NSDecimal n2;
int exp = l->exponent + r->exponent;
BOOL neg = l->isNegative != r->isNegative;
NSComparisonResult comp;
if (!l->validNumber || !r->validNumber)
{
result->validNumber = NO;
return error;
}
// check for zero
if (NSDECIMAL_IS_ZERO(l) || NSDECIMAL_IS_ZERO(r))
{
NSDecimalCopy(result, &zero);
return error;
}
if (exp > 127)
{
result->validNumber = NO;
if (neg)
return NSCalculationUnderflow;
else
return NSCalculationOverflow;
}
NSDecimalCopy(&n1, l);
NSDecimalCopy(&n2, r);
n1.exponent = 0;
n2.exponent = 0;
n1.isNegative = NO;
n2.isNegative = NO;
comp = NSSimpleCompare(&n1, &n2);
if (NSOrderedDescending == comp)
{
error = GSSimpleMultiply(result, &n1, &n2, mode);
}
else
{
error = GSSimpleMultiply(result, &n2, &n1, mode);
}
NSDecimalCompact(result);
if (result->exponent + exp > 127)
{
result->validNumber = NO;
if (neg)
return NSCalculationUnderflow;
else
return NSCalculationOverflow;
}
else if (result->exponent + exp < -128)
{
// We must cut off some digits
NSDecimalRound(result, result, exp+128, mode);
error = NSCalculationLossOfPrecision;
if (result->exponent + exp < -128)
{
NSDecimalCopy(result, &zero);
return error;
}
}
result->exponent += exp;
result->isNegative = neg;
return error;
}
static NSCalculationError
GSSimpleDivide(NSDecimal *result, const NSDecimal *l, const NSDecimal *r,
NSRoundingMode mode);
GS_DECLARE NSCalculationError
NSDecimalDivide(NSDecimal *result, const NSDecimal *l, const NSDecimal *rr,
NSRoundingMode mode)
{
NSCalculationError error = NSCalculationNoError;
NSDecimal n1;
NSDecimal n2;
int exp = l->exponent - rr->exponent;
BOOL neg = l->isNegative != rr->isNegative;
if (!l->validNumber || !rr->validNumber)
{
result->validNumber = NO;
return NSCalculationNoError;
}
// Check for zero
if (NSDECIMAL_IS_ZERO(rr))
{
result->validNumber = NO;
return NSCalculationDivideByZero;
}
if (NSDECIMAL_IS_ZERO(l))
{
NSDecimalCopy(result, &zero);
return error;
}
// Should also check for one
NSDecimalCopy(&n1, l);
n1.exponent = 0;
n1.isNegative = NO;
NSDecimalCopy(&n2, rr);
n2.exponent = 0;
n2.isNegative = NO;
error = GSSimpleDivide(result, &n1, &n2, mode);
NSDecimalCompact(result);
if (result->exponent + exp > 127)
{
result->validNumber = NO;
if (neg)
return NSCalculationUnderflow;
else
return NSCalculationOverflow;
}
else if (result->exponent + exp < -128)
{
// We must cut off some digits
NSDecimalRound(result, result, exp+128, mode);
error = NSCalculationLossOfPrecision;
if (result->exponent + exp < -128)
{
NSDecimalCopy(result, &zero);
return error;
}
}
result->exponent += exp;
result->isNegative = neg;
return error;
}
GS_DECLARE NSCalculationError
NSDecimalPower(NSDecimal *result, const NSDecimal *n, NSUInteger power, NSRoundingMode mode)
{
NSCalculationError error = NSCalculationNoError;
unsigned int e = power;
NSDecimal n1;
BOOL neg = (n->isNegative && (power % 2));
NSDecimalCopy(&n1, n);
n1.isNegative = NO;
NSDecimalCopy(result, &one);
// NSDecimalCopy(result, &zero);
// result->length = 1;
// result->cMantissa[0] = 1;
while (e)
{
if (e & 1)
{
error = NSDecimalMultiply(result, result, &n1, mode);
if (NSCalculationNoError != error)
{
break;
}
}
// keep on squaring the number
error = NSDecimalMultiply(&n1, &n1, &n1, mode);
if (NSCalculationNoError != error)
{
break;
}
e >>= 1;
}
result->isNegative = neg;
NSDecimalCompact(result);
return error;
}
GS_DECLARE NSCalculationError
NSDecimalMultiplyByPowerOf10(NSDecimal *result, const NSDecimal *n, short power, NSRoundingMode mode)
{
int p;
NSDecimalCopy(result, n);
p = result->exponent + power;
if (p > 127)
{
result->validNumber = NO;
return NSCalculationOverflow;
}
if (p < -128)
{
result->validNumber = NO;
return NSCalculationUnderflow;
}
result->exponent += power;
return NSCalculationNoError;
}
static NSString*
GSDecimalString(const GSDecimal *number, NSDictionary *locale)
{
int i;
int d;
NSString *s;
NSMutableString *string;
NSString *sep;
int size;
if (!number->validNumber)
return @"NaN";
if ((nil == locale) ||
(sep = [locale objectForKey: NSDecimalSeparator]) == nil)
sep = @".";
string = [NSMutableString stringWithCapacity: 45];
if (!number->length)
{
[string appendString: @"0"];
[string appendString: sep];
[string appendString: @"0"];
return string;
}
if (number->isNegative)
[string appendString: @"-"];
size = number->length + number->exponent;
if ((number->length <= 6) && (0 < size) && (size < 7))
{
// For small numbers use the normal format
for (i = 0; i < number->length; i++)
{
if (size == i)
[string appendString: sep];
d = number->cMantissa[i];
s = [NSString stringWithFormat: @"%d", d];
[string appendString: s];
}
for (i = 0; i < number->exponent; i++)
{
[string appendString: @"0"];
}
}
else if ((number->length <= 6) && (0 >= size) && (size > -3))
{
// For small numbers use the normal format
[string appendString: @"0"];
[string appendString: sep];
for (i = 0; i > size; i--)
{
[string appendString: @"0"];
}
for (i = 0; i < number->length; i++)
{
d = number->cMantissa[i];
s = [NSString stringWithFormat: @"%d", d];
[string appendString: s];
}
}
else
{
// Scientific format
for (i = 0; i < number->length; i++)
{
if (1 == i)
[string appendString: sep];
d = number->cMantissa[i];
s = [NSString stringWithFormat: @"%d", d];
[string appendString: s];
}
if (size != 1)
{
s = [NSString stringWithFormat: @"E%d", size-1];
[string appendString: s];
}
}
return string;
}
// GNUstep extensions to make the implementation of NSDecimalNumber totaly
// independent for NSDecimals internal representation
// Give back the biggest NSDecimal
GS_DECLARE void
NSDecimalMax(NSDecimal *result)
{
// FIXME: this is too small
NSDecimalFromComponents(result, 9, 127, NO);
}
// Give back the smallest NSDecimal
GS_DECLARE void
NSDecimalMin(NSDecimal *result)
{
// This is the smallest possible not the smallest positive number
// FIXME: this is too big
NSDecimalFromComponents(result, 9, 127, YES);
}
// Give back the value of a NSDecimal as a double
static double
GSDecimalDouble(GSDecimal *number)
{
double d = 0.0;
int i;
if (!number->validNumber)
return NAN;
// Sum up the digits
for (i = 0; i < number->length; i++)
{
d *= 10;
d += number->cMantissa[i];
}
// multiply with the exponent
// There is also a GNU extension pow10!!
d *= pow(10, number->exponent);
if (number->isNegative)
d = -d;
return d;
}
// Create a NSDecimal with a cMantissa, exponent and a negative flag
static void
GSDecimalFromComponents(GSDecimal *result, unsigned long long mantissa,
short exponent, BOOL negative)
{
unsigned char digit;
int i, j;
result->isNegative = negative;
result->exponent = exponent;
result->validNumber = YES;
i = 0;
while (mantissa)
{
digit = mantissa % 10;
// Store the digit starting from the end of the array
result->cMantissa[NSDecimalMaxDigit-i-1] = digit;
mantissa = mantissa / 10;
i++;
}
for (j = 0; j < i; j++)
{
// Move the digits to the beginning
result->cMantissa[j] = result->cMantissa[j + NSDecimalMaxDigit-i];
}
result->length = i;
GSDecimalCompact(result);
}