Source

pyobjc / pyobjc / Modules / Foundation / decimals.m

Full commit
  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
/*
 * Minimal support for the C-type NSDecimal
 *
 * This file provides *very* minimal support for NSDecimal values. A major
 * problem is that the C API does not have a function to initialise these
 * objects (you can create them through NSDecimalNumber instances).
 *
 * There is no implicit conversion to/from Python numbers, because NSDecimal
 * numbers behave differently from Python numbers (explicit rounding)
 *
 * - number methods
 *   NSDecimal objects support +, -, *, /, +=, -=, *= and /=, which directly
 *   correspond with NSDecimal* functions with the NSRoundPlain argument.
 *   They also support unary -, unary + and abs, with the obvious semantics.
 */

typedef struct {
	PyObject_HEAD
	NSDecimal value;
} DecimalObject;

#define Decimal_Value(v) ((DecimalObject*)(v))->value

static PyObject* Decimal_New(NSDecimal* aDecimal);
static PyObject* decimal_repr(PyObject* self);
static PyObject* decimal_richcompare(PyObject* self, PyObject* other, int type);
static void decimal_dealloc(PyObject* self);
static int decimal_init(PyObject* self, PyObject* args, PyObject* kwds);
static PyObject* decimal_new(PyTypeObject* type, PyObject* args, PyObject* kwds);
static PyObject* decimal_asint(PyObject* self);
static PyObject* decimal_asfloat(PyObject* self);
static PyObject* decimal_add(PyObject* left, PyObject* right);
static PyObject* decimal_subtract(PyObject* left, PyObject* right);
static PyObject* decimal_multiply(PyObject* left, PyObject* right);
static PyObject* decimal_divide(PyObject* left, PyObject* right);
static PyObject* decimal_power(PyObject* left, PyObject* right, PyObject* power);
static int decimal_nonzero(PyObject* self);
static int decimal_coerce(PyObject** l, PyObject** r);
static PyObject* decimal_inplace_add(PyObject* left, PyObject* right);
static PyObject* decimal_inplace_subtract(PyObject* left, PyObject* right);
static PyObject* decimal_inplace_multiply(PyObject* left, PyObject* right);
static PyObject* decimal_inplace_divide(PyObject* left, PyObject* right);
static PyObject* decimal_positive(PyObject* self);
static PyObject* decimal_negative(PyObject* self);
static PyObject* decimal_absolute(PyObject* self);

static PyNumberMethods decimal_asnumber = {
	decimal_add,			/* nb_add */
	decimal_subtract,		/* nb_subtract */
	decimal_multiply,		/* nb_multiply */
	decimal_divide,			/* nb_divide */
	NULL,				/* nb_remainder */
	NULL,				/* nb_divmod */
	decimal_power,			/* nb_power */
	decimal_negative,		/* nb_negative */
	decimal_positive,		/* nb_positive */
	decimal_absolute,		/* nb_absolute */
	decimal_nonzero,		/* nb_nonzero */
	NULL,				/* nb_invert */
	NULL,				/* nb_lshift */
	NULL,				/* nb_rshift */
	NULL,				/* nb_and */
	NULL,				/* nb_xor */
	NULL,				/* nb_or */
	decimal_coerce,			/* nb_coerce */
	NULL,				/* nb_int */
	NULL,				/* nb_long */
	NULL,				/* nb_float */
	NULL,				/* nb_oct */
	NULL,				/* nb_hex */
	decimal_inplace_add,		/* nb_inplace_add */
	decimal_inplace_subtract,	/* nb_inplace_subtract */
	decimal_inplace_multiply,	/* nb_inplace_multiply */
	decimal_inplace_divide,		/* nb_inplace_divide */
	NULL,				/* nb_inplace_remainder */
	NULL,				/* nb_inplace_power */
	NULL,				/* nb_inplace_lshift */
	NULL,				/* nb_inplace_rshift */
	NULL,				/* nb_inplace_and */
	NULL,				/* nb_inplace_xor */
	NULL,				/* nb_inplace_or */
	NULL,				/* nb_floor_divide */
	NULL,				/* nb_true_divide */
	NULL,				/* nb_inplace_floor_divide */
	NULL				/* nb_inplace_true_divide */
};

static PyMethodDef decimal_methods[] = {
	{
		"as_int",
		(PyCFunction)decimal_asint,
		METH_NOARGS,
		"Convert decimal to a Python int"
	},
	{
		"as_float",
		(PyCFunction)decimal_asfloat,
		METH_NOARGS,
		"Convert decimal to a Python float"
	},
	{
		NULL,
		NULL,
		0,
		NULL
	}
};


static PyTypeObject Decimal_Type = {
	PyObject_HEAD_INIT(NULL)
	0,					/* ob_size */
	"Foundation.NSDecimal",			/* tp_name */
	sizeof (DecimalObject),			/* tp_basicsize */
	0,					/* tp_itemsize */
  
	/* methods */
	decimal_dealloc,			/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	decimal_repr,				/* tp_repr */
	&decimal_asnumber,			/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	decimal_repr,				/* tp_str */
	PyObject_GenericGetAttr,		/* tp_getattro */
	PyObject_GenericSetAttr,		/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_RICHCOMPARE | Py_TPFLAGS_HAVE_INPLACEOPS, /* tp_flags */
	"NSDecimal wrapper",			/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	decimal_richcompare,			/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	decimal_methods,			/* tp_methods */
	0,					/* tp_members */
	0,					/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	0,					/* tp_init */
	0,					/* tp_alloc */
	decimal_new,				/* tp_new */
	0,					/* tp_free */
	0,					/* tp_is_gc */
	0,					/* tp_bases */
	0,					/* tp_mro */
	0,					/* tp_cache */
	0,					/* tp_subclasses */
	0					/* tp_weaklist */
#if PY_VERSION_HEX >= 0x020300A2
	, 0,					/* tp_del */
#endif
};

#define Decimal_Check(obj) PyObject_TypeCheck(obj, &Decimal_Type)

#ifdef MACOSX

static void DecimalFromString(NSDecimal* aDecimal, NSString* aString, void* locale __attribute__((__unused__)))
{
	NSDecimalNumber* num;

	num = [[NSDecimalNumber alloc] initWithString:aString];
	*aDecimal = [num decimalValue];
	[num release];
}

static void DecimalFromComponents(NSDecimal* aDecimal, unsigned long long mantissa, unsigned short exponent, BOOL negative)
{
	NSDecimalNumber* num;

	num = [[NSDecimalNumber alloc] 
			initWithMantissa:mantissa
			exponent:exponent
			isNegative:negative];

	*aDecimal = [num decimalValue];
	[num release];
}

#else

#define DecimalFromString NSDecimalFromString
#define DecimalFromComponents NSDecimalFromComponents

#endif

static PyObject*
decimal_new(PyTypeObject* type __attribute__((__unused__)), PyObject* args, PyObject* kwds)
{
	DecimalObject* self;

	
	self = PyObject_New(DecimalObject, &Decimal_Type);
	if (self != NULL) {
		memset(&self->value, 0, sizeof(self->value));
	}
	if ((args == NULL || PyTuple_Size(args) == 0) && (kwds == NULL || PyDict_Size(kwds) == 0)) {
		DecimalFromComponents(&self->value, 0, 0, 0);
		return (PyObject*)self;
	}
	if (decimal_init((PyObject*)self, args, kwds) == -1) {
		Py_DECREF(self);
		return NULL;
	}
	return (PyObject*)self;
}

static void
decimal_dealloc(PyObject* self)
{
	PyObject_Free(self);
}


static int
decimal_init(PyObject* self, PyObject* args, PyObject* kwds)
{
static char* keywords[] = { "mantissa", "exponent", "isNegative", NULL };
static char* keywords2[] = { "string", NULL };
	PyObject* pyMantissa;
	PyObject* pyExponent;
	PyObject* pyNegative;
	BOOL negative;
	unsigned long long mantissa;
	short int exponent;

	if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO", keywords, &pyMantissa, &pyExponent, &pyNegative)) {
		PyObject* pyValue;
		NSString* stringVal;

		PyErr_Clear();
		if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", keywords2, &pyValue)) {
			PyErr_SetString(PyExc_TypeError,
			    "NSDecimal(stringValue) or NSDecimal(mantissa, exponent, isNegative)");
			return -1;
		}
		if (PyInt_Check(pyValue)) {
			long lng = PyInt_AsLong(pyValue);
			if (lng < 0) {
				mantissa = -lng;
				exponent = 0;
				negative = YES;
			} else{
				mantissa = lng;
				exponent = 0;
				negative = NO;
			}
				
			DecimalFromComponents(&Decimal_Value(self),
				mantissa, exponent, negative);
			return 0;
		} else if (PyLong_Check(pyValue)) {
			mantissa = PyLong_AsUnsignedLongLong(pyValue);
			if (PyErr_Occurred()) {
				long long lng;
				PyErr_Clear();
				lng = PyLong_AsLongLong(pyValue);
				if (PyErr_Occurred()) {
					return -1;
				}
				if (lng < 0) {
					mantissa = -lng;
					exponent = 0;
					negative = YES;
				} else {
					mantissa = lng;
					exponent = 0;
					negative = NO;
				}
				DecimalFromComponents(&Decimal_Value(self),
					mantissa, exponent, negative);
				return 0;
			} else {
				DecimalFromComponents(&Decimal_Value(self),
					mantissa, 0, NO);
				return 0;
			}
		} else if (!PyString_Check(pyValue) && !PyUnicode_Check(pyValue)) {
			PyErr_Format(PyExc_TypeError, "cannot convert object of %s to NSDecimal", pyValue->ob_type->tp_name);
			return -1;
		}

		stringVal = PyObjC_PythonToId(pyValue);
		NS_DURING
			DecimalFromString(&Decimal_Value(self), stringVal, NULL);
		NS_HANDLER
			PyObjCErr_FromObjC(localException);
		NS_ENDHANDLER

		if (PyErr_Occurred()) return -1;
		return 0;

	}

	negative = PyObject_IsTrue(pyNegative);
	if (PyObjC_PythonToObjC(@encode(short int), pyExponent, &exponent) == -1) {
		return -1;
	}

	if (PyObjC_PythonToObjC(@encode(unsigned long long), pyMantissa, &mantissa) == -1) {
		return -1;
	}


	DecimalFromComponents(&Decimal_Value(self),
		mantissa,
		exponent,
		negative);

	return 0;
}

static PyObject* 
decimal_richcompare(PyObject* self, PyObject* other, int type)
{
	NSComparisonResult res;

	if (!Decimal_Check(other)) {
		if (type == Py_EQ) {
			return PyObjCBool_FromLong(0);
		}
		PyErr_Format(PyExc_TypeError,
			"Cannot compare NSDecimal and %s", 
			other->ob_type->tp_name);
		return NULL;
	}

	res = NSDecimalCompare(&Decimal_Value(self), &Decimal_Value(other));

	switch (type) {
	case Py_LT: return PyObjCBool_FromLong(res ==  NSOrderedAscending);
	case Py_LE: return PyObjCBool_FromLong(res != NSOrderedDescending);
	case Py_EQ: return PyObjCBool_FromLong(res == NSOrderedSame);
	case Py_NE: return PyObjCBool_FromLong(res != NSOrderedSame);
	case Py_GE: return PyObjCBool_FromLong(res != NSOrderedAscending);
	case Py_GT: return PyObjCBool_FromLong(res == NSOrderedDescending);
	default: 
		    PyErr_SetString(PyExc_TypeError,
				  "Bad comparison arg");
		    return NULL;
	}
}


static PyObject* decimal_asint(PyObject* self)
{
	NSDecimalNumber* tmp = [[NSDecimalNumber alloc] initWithDecimal:Decimal_Value(self)];
	long retval = [tmp longValue];
	[tmp release];

	return PyInt_FromLong(retval);
}

static PyObject* decimal_asfloat(PyObject* self)
{
	NSDecimalNumber* tmp = [[NSDecimalNumber alloc] initWithDecimal:Decimal_Value(self)];
	double retval = [tmp doubleValue];
	[tmp release];

	return PyFloat_FromDouble(retval);
}

static PyObject* decimal_power(
	PyObject* left __attribute__((__unused__)),  
	PyObject* right __attribute__((__unused__)), 
	PyObject* extra __attribute__((__unused__)))
{
	PyErr_SetString(PyExc_TypeError,
		"pow() and ** are not supported for NSDecimal");
	return NULL;
}

static PyObject* decimal_add(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;

	err = NSDecimalAdd(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		NSDecimalCompact(&result);
		return Decimal_New(&result);
	}
}

static PyObject* decimal_subtract(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;

	err = NSDecimalSubtract(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		NSDecimalCompact(&result);
		return Decimal_New(&result);
	}
}

static PyObject* decimal_multiply(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;

	err = NSDecimalMultiply(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		NSDecimalCompact(&result);
		return Decimal_New(&result);
	}
}

static PyObject* decimal_divide(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;

	err = NSDecimalDivide(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		NSDecimalCompact(&result);
		return Decimal_New(&result);
	}
}

static PyObject* decimal_inplace_add(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;
	int r = decimal_coerce(&left, &right);
	if (r == 1) {
		PyErr_Format(PyExc_TypeError,
			"unsupported operand type(s) for +=: '%s' and '%s'",
			left->ob_type->tp_name,
			right->ob_type->tp_name);
		return NULL;
	}

	err = NSDecimalAdd(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		Py_DECREF(right);
		NSDecimalCompact(&result);
		Decimal_Value(left) = result;
		return left;
	}
}

static PyObject* decimal_inplace_subtract(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;
	int r = decimal_coerce(&left, &right);
	if (r == 1) {
		PyErr_Format(PyExc_TypeError,
			"unsupported operand type(s) for -=: '%s' and '%s'",
			left->ob_type->tp_name,
			right->ob_type->tp_name);
		return NULL;
	}

	err = NSDecimalSubtract(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		Py_DECREF(right);
		NSDecimalCompact(&result);
		Decimal_Value(left) = result;
		return left;
	}
}

static PyObject* decimal_inplace_multiply(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;
	int r = decimal_coerce(&left, &right);
	if (r == 1) {
		PyErr_Format(PyExc_TypeError,
			"unsupported operand type(s) for *=: '%s' and '%s'",
			left->ob_type->tp_name,
			right->ob_type->tp_name);
		return NULL;
	}

	err = NSDecimalMultiply(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		Py_DECREF(right);
		NSDecimalCompact(&result);
		Decimal_Value(left) = result;
		return left;
	}
}

static PyObject* decimal_inplace_divide(PyObject* left, PyObject* right)
{
	NSDecimal  result;
	NSCalculationError err;
	int r = decimal_coerce(&left, &right);
	if (r == 1) {
		PyErr_Format(PyExc_TypeError,
			"unsupported operand type(s) for /=: '%s' and '%s'",
			left->ob_type->tp_name,
			right->ob_type->tp_name);
		return NULL;
	}

	err = NSDecimalDivide(&result, 
			&Decimal_Value(left), 
			&Decimal_Value(right),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		Py_DECREF(left); Py_DECREF(right);
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		Py_DECREF(right);
		NSDecimalCompact(&result);
		Decimal_Value(left) = result;
		return left;
	}
}

static int decimal_nonzero(PyObject* self)
{
	NSDecimal zero;

	DecimalFromComponents(&zero, 0, 0, 0);

	return NSDecimalCompare(&zero, &Decimal_Value(self)) == NSOrderedSame;
}

static PyObject* decimal_positive(PyObject* self)
{
	Py_INCREF(self);
	return self;
}

static PyObject* decimal_negative(PyObject* self)
{
	NSDecimal  result;
	NSCalculationError err;
	NSDecimal  zero;
	DecimalFromComponents(&zero, 0, 0, 0);

	err = NSDecimalSubtract(&result, 
			&zero, 
			&Decimal_Value(self),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		NSDecimalCompact(&result);
		return Decimal_New(&result);
	}
}

static PyObject* decimal_absolute(PyObject* self)
{
	NSDecimal  result;
	NSCalculationError err;
	NSDecimal  zero;
	DecimalFromComponents(&zero, 0, 0, 0);


	switch (NSDecimalCompare(&zero, &Decimal_Value(self))) {
	case NSOrderedSame:
	case NSOrderedAscending:
		/* self >= 0 */
		Py_INCREF(self);
		return self;

	case NSOrderedDescending: ;
	}


	err = NSDecimalSubtract(&result, 
			&zero, 
			&Decimal_Value(self),
			NSRoundPlain);
	if (err == NSCalculationOverflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric overflow");
		return NULL;
	} else if (err == NSCalculationUnderflow) {
		PyErr_SetString(PyExc_OverflowError, "Numeric underflow");
		return NULL;
	} else  {
		NSDecimalCompact(&result);
		return Decimal_New(&result);
	}
}

static int decimal_coerce(PyObject** l, PyObject** r)
{
	PyObject* right = NULL;
	PyObject* left = NULL;
	PyObject* args = NULL;
	int res;

	if (Decimal_Check(*l) && Decimal_Check(*r)) {
		Py_INCREF(*l);
		Py_INCREF(*r);
		return 0;
	}

	if (!Decimal_Check(*l)) {
		/* The test is needed to avoid silently converting strings */
		if (!(PyInt_Check(*l) || PyLong_Check(*l))) goto error;
		
		left = (PyObject*)PyObject_New(DecimalObject, &Decimal_Type);
		if (left == NULL) goto error;

		args = Py_BuildValue("(O)", *l);
		if (args == NULL) goto error;

		res = decimal_init(left, args, NULL);
		if (res == -1) goto error;

		Py_DECREF(args); args = NULL;
	}

	if (!Decimal_Check(*r)) {
		/* The test is needed to avoid silently converting strings */
		if (!(PyInt_Check(*r) || PyLong_Check(*r))) goto error;
		
		right = (PyObject*)PyObject_New(DecimalObject, &Decimal_Type);
		if (right == NULL) goto error;

		args = Py_BuildValue("(O)", *r);
		if (args == NULL) goto error;

		res = decimal_init(right, args, NULL);
		if (res == -1) goto error;

		Py_DECREF(args); args = NULL;
	}

	if (left != NULL) {
		*l = left;
	} else {
		Py_INCREF(*l);
	}

	if (right != NULL) {
		*r = right;
	} else {
		Py_INCREF(*r);
	}

	return 0;

error:
	Py_XDECREF(args);
	Py_XDECREF(left);
	Py_XDECREF(right);
	return 1;
}

static PyObject*
decimal_repr(PyObject* self)
{
	NSString* val = NSDecimalString(&Decimal_Value(self), NULL);
	PyObject* tmp =  PyObjC_IdToPython(val);
	PyObject* repr = PyObject_Str(tmp);
	Py_DECREF(tmp);
	return repr;
}

static inline int
Decimal_Convert(PyObject* self, void* val)
{
	if (Decimal_Check(self)) {
		 *(NSDecimal**)val = &Decimal_Value(self);
		 return 1;
	}
	PyErr_SetString(PyExc_TypeError, "Expecting an NSDecimal");
	return 0;
}


static PyObject*
Decimal_New(NSDecimal* aDecimal)
{
	DecimalObject* result;

	result = PyObject_New(DecimalObject, &Decimal_Type);
	if (result == NULL) return NULL;

	result->value = *aDecimal;
	return (PyObject*)result;
}

static int install_decimal(PyObject* module);
static int install_decimal(PyObject* module)
{
	PyType_Ready(&Decimal_Type);
	PyModule_AddObject(module, "NSDecimal", (PyObject*)&Decimal_Type);
	return 0;
}