changeset 11:8e5f172ec55c

added comparable
author Paul (paul.d.anderson@comcast.net)
date Thu, 25 Mar 2010 21:37:14 -0700
parents f925fe996255
children 526759f0ee1c
files src/decimal/bcd.d
diffstat 1 files changed, 14 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/decimal/bcd.d	Thu Mar 25 20:05:00 2010 -0700
+++ b/src/decimal/bcd.d	Thu Mar 25 21:37:14 2010 -0700
@@ -687,9 +687,8 @@
 	}
 	
 	const bool opEquals(T:Bcd)(const T that) {
-		Bcd a = canonical(this);
-		Bcd b = canonical(that);
-		// TODO: this is wrong -- signs can differ -0 and + 0
+		Bcd a = comparable(this);
+		Bcd b = comparable(that);
 		if (this.sign != that.sign) return false;
 		if (a.digits.length != b.digits.length) return false;
 		foreach_reverse(int i, Digit digit; a.digits) {
@@ -723,20 +722,23 @@
 
 public bool isCanonical(const Bcd a) {
 	// no leading zeros
-	if (a.hasLeadingZeros) return false;
-	// not -0
-/+	if (a.numDigits == 1 && a.firstDigit == 0) return !a.sign;+/
-	return true;
+	return !a.hasLeadingZeros;
 }
 
-// Strips leading zeros and changes sign if == -0
+// Strips leading zeros
 public Bcd canonical(const Bcd a) {
 	Bcd d = a.dup;
 	if (isCanonical(a)) return d;
 	stripl(d);
-/+	if (d.numDigits == 1 && d.firstDigit == 0) {
+	return d;
+}
+
+// Strips leading zeros and changes sign if == -0
+private Bcd comparable(const Bcd a) {
+	Bcd d = canonical(a);
+	if (d.numDigits == 1 && d.firstDigit == 0) {
 		d.sign = false;
-	}+/
+	}
 	return d;
 }
 
@@ -914,8 +916,8 @@
 }
 
 public int compare(const Bcd m, const Bcd n) {
-	Bcd a = canonical(m);
-	Bcd b = canonical(n);
+	Bcd a = comparable(m);
+	Bcd b = comparable(n);
 	if (!a.isSigned && b.isSigned) return 1;
 	if (a.isSigned && !b.isSigned) return -1;
 	if (!a.isSigned) {