improved rounding in MathX.mod, see Bugzilla 50033

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1003504 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2010-10-01 12:02:52 +00:00
parent 7033853c36
commit c27d3438eb
3 changed files with 18 additions and 8 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.7-beta4" date="2010-??-??"> <release version="3.7-beta4" date="2010-??-??">
<action dev="poi-developers" type="fix">50033 - Improved rounding in MOD</action>
<action dev="poi-developers" type="add">Generate SHA1 hashes of distribution files, alongside existing MD5 ones</action> <action dev="poi-developers" type="add">Generate SHA1 hashes of distribution files, alongside existing MD5 ones</action>
</release> </release>
<release version="3.7-beta3" date="2010-09-24"> <release version="3.7-beta3" date="2010-09-24">

View File

@ -17,6 +17,7 @@
package org.apache.poi.hssf.record.formula.functions; package org.apache.poi.hssf.record.formula.functions;
/** /**
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt; * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
* This class is an extension to the standard math library * This class is an extension to the standard math library
@ -349,21 +350,15 @@ final class MathX {
result = Double.NaN; result = Double.NaN;
} }
else if (sign(n) == sign(d)) { else if (sign(n) == sign(d)) {
double t = Math.abs(n / d); result = n % d;
t = t - (long) t;
result = sign(d) * Math.abs(t * d);
} }
else { else {
double t = Math.abs(n / d); result = ((n % d) + d) % d;
t = t - (long) t;
t = Math.ceil(t) - t;
result = sign(d) * Math.abs(t * d);
} }
return result; return result;
} }
/** /**
* inverse hyperbolic cosine * inverse hyperbolic cosine
* @param d * @param d

View File

@ -292,6 +292,20 @@ public class TestMathX extends AbstractNumericTestCase {
} }
public void testMod() { public void testMod() {
//example from Excel help
assertEquals(1.0, MathX.mod(3, 2));
assertEquals(1.0, MathX.mod(-3, 2));
assertEquals(-1.0, MathX.mod(3, -2));
assertEquals(-1.0, MathX.mod(-3, -2));
assertEquals((double) 1.4, MathX.mod(3.4, 2));
assertEquals((double) -1.4, MathX.mod(-3.4, -2));
assertEquals((double) 0.6000000000000001, MathX.mod(-3.4, 2.0));// should actually be 0.6
assertEquals((double) -0.6000000000000001, MathX.mod(3.4, -2.0));// should actually be -0.6
// Bugzilla 50033
assertEquals(1.0, MathX.mod(13, 12));
} }
public void testNChooseK() { public void testNChooseK() {