[kaffe] CVS kaffe (guilhem): Fixed rounding operations in divide().

Kaffe CVS cvs-commits at kaffe.org
Wed Dec 31 09:21:02 PST 2003


PatchSet 4255 
Date: 2003/12/31 16:54:58
Author: guilhem
Branch: HEAD
Tag: (none) 
Log:
Fixed rounding operations in divide().

Members: 
	ChangeLog:1.1842->1.1843 
	libraries/javalib/java/math/BigDecimal.java:1.7->1.8 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.1842 kaffe/ChangeLog:1.1843
--- kaffe/ChangeLog:1.1842	Wed Dec 31 16:53:50 2003
+++ kaffe/ChangeLog	Wed Dec 31 16:54:58 2003
@@ -1,5 +1,10 @@
 2003-12-31  Guilhem Lavaux <guilhem at kaffe.org>
 
+	* libraries/javalib/java/math/BigDecimal.java
+	(divide): Fixed rounding operations.
+
+2003-12-31  Guilhem Lavaux <guilhem at kaffe.org>
+
 	* libraries/javalib/java/net/URL.java,
 	libraries/javalib/java/net/URLStreamHandler.java:
 	Implemented authority string generation and fixed toExternalForm()
Index: kaffe/libraries/javalib/java/math/BigDecimal.java
diff -u kaffe/libraries/javalib/java/math/BigDecimal.java:1.7 kaffe/libraries/javalib/java/math/BigDecimal.java:1.8
--- kaffe/libraries/javalib/java/math/BigDecimal.java:1.7	Thu Nov 22 06:21:13 2001
+++ kaffe/libraries/javalib/java/math/BigDecimal.java	Wed Dec 31 16:55:04 2003
@@ -195,7 +195,7 @@
 	}
 
 	int sign = a.signum() * b.signum();
-	// half rounding study (r / b) <=> .5 aka r * .5 <=> b
+	// half rounding study (r / b) <=> .5 aka r * 2 <=> b
 	int half = r.abs().multiply(BigInteger.valueOf(2)).compareTo(b.abs());
 	switch (roundingMode) {
 	case ROUND_CEILING:
@@ -222,9 +222,15 @@
 	    throw new ArithmeticException("Rounding necessary");
 	}
 
-	return new BigDecimal((roundingMode == ROUND_UP)
-			      ? q.add(BigInteger.ONE) : q,
-			      newScale);
+	if (roundingMode == ROUND_UP)
+	  q = q.abs().add(BigInteger.ONE);
+	else
+	  q = q.abs();
+    
+	if (sign > 0)
+	  return new BigDecimal(q, newScale);
+	else
+	  return new BigDecimal(q.negate(), newScale);
     }
 
     public BigDecimal divide(BigDecimal val, int roundingMode) {




More information about the kaffe mailing list