Added a few shortcut methods and predicates to Dfp

(abs, isZero, negativeOrNull, strictlyNegative, positiveOrNull, strictlyPositive).


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1152266 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-07-29 15:34:10 +00:00
parent f0e28eb000
commit b5d183025d
3 changed files with 158 additions and 0 deletions

View File

@ -796,6 +796,75 @@ public class Dfp implements FieldElement<Dfp> {
return compare(this, x) > 0;
}
/** Check if instance is less than or equal to 0.
* @return true if instance is not NaN and less than or equal to 0, false otherwise
*/
public boolean negativeOrNull() {
if (isNaN()) {
field.setIEEEFlagsBits(DfpField.FLAG_INVALID);
dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero()));
return false;
}
return (sign < 0) || ((mant[mant.length - 1] == 0) && !isInfinite());
}
/** Check if instance is strictly less than 0.
* @return true if instance is not NaN and less than or equal to 0, false otherwise
*/
public boolean strictlyNegative() {
if (isNaN()) {
field.setIEEEFlagsBits(DfpField.FLAG_INVALID);
dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero()));
return false;
}
return (sign < 0) && ((mant[mant.length - 1] != 0) || isInfinite());
}
/** Check if instance is greater than or equal to 0.
* @return true if instance is not NaN and greater than or equal to 0, false otherwise
*/
public boolean positiveOrNull() {
if (isNaN()) {
field.setIEEEFlagsBits(DfpField.FLAG_INVALID);
dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero()));
return false;
}
return (sign > 0) || ((mant[mant.length - 1] == 0) && !isInfinite());
}
/** Check if instance is strictly greater than 0.
* @return true if instance is not NaN and greater than or equal to 0, false otherwise
*/
public boolean strictlyPositive() {
if (isNaN()) {
field.setIEEEFlagsBits(DfpField.FLAG_INVALID);
dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero()));
return false;
}
return (sign > 0) && ((mant[mant.length - 1] != 0) || isInfinite());
}
/** Get the absolute value of instance.
* @return absolute value of instance
*/
public Dfp abs() {
Dfp result = newInstance(this);
result.sign = 1;
return result;
}
/** Check if instance is infinite.
* @return true if instance is infinite
*/
@ -810,6 +879,21 @@ public class Dfp implements FieldElement<Dfp> {
return (nans == QNAN) || (nans == SNAN);
}
/** Check if instance is equal to zero.
* @return true if instance is equal to zero
*/
public boolean isZero() {
if (isNaN()) {
field.setIEEEFlagsBits(DfpField.FLAG_INVALID);
dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero()));
return false;
}
return (mant[mant.length - 1] == 0) && !isInfinite();
}
/** Check if instance is equal to x.
* @param other object to check instance against
* @return true if instance is equal to x and neither are NaN, false otherwise

View File

@ -52,6 +52,10 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces!
-->
<release version="3.0" date="TBD" description="TBD">
<action dev="luc" type="add" >
Added a few shortcut methods and predicates to Dfp (abs, isZero,
negativeOrNull, strictlyNegative, positiveOrNull, strictlyPositive).
</action>
<action dev="erans" type="add" issue="MATH-633" due-to="Sébastien Brisard">
"AbstractRealMatrix" inherits from "RealLinearOperator".
</action>

View File

@ -1515,4 +1515,74 @@ public class DfpTest {
Assert.assertEquals(+1, FastMath.copySign(1, field.newDfp(+0.0).toDouble()), MathUtils.EPSILON);
}
@Test
public void testIsZero() {
Assert.assertTrue(field.getZero().isZero());
Assert.assertTrue(field.getZero().negate().isZero());
Assert.assertTrue(field.newDfp(+0.0).isZero());
Assert.assertTrue(field.newDfp(-0.0).isZero());
Assert.assertFalse(field.newDfp(1.0e-90).isZero());
Assert.assertFalse(nan.isZero());
Assert.assertFalse(nan.negate().isZero());
Assert.assertFalse(pinf.isZero());
Assert.assertFalse(pinf.negate().isZero());
Assert.assertFalse(ninf.isZero());
Assert.assertFalse(ninf.negate().isZero());
}
@Test
public void testSignPredicates() {
Assert.assertTrue(field.getZero().negativeOrNull());
Assert.assertTrue(field.getZero().positiveOrNull());
Assert.assertFalse(field.getZero().strictlyNegative());
Assert.assertFalse(field.getZero().strictlyPositive());
Assert.assertTrue(field.getZero().negate().negativeOrNull());
Assert.assertTrue(field.getZero().negate().positiveOrNull());
Assert.assertFalse(field.getZero().negate().strictlyNegative());
Assert.assertFalse(field.getZero().negate().strictlyPositive());
Assert.assertFalse(field.getOne().negativeOrNull());
Assert.assertTrue(field.getOne().positiveOrNull());
Assert.assertFalse(field.getOne().strictlyNegative());
Assert.assertTrue(field.getOne().strictlyPositive());
Assert.assertTrue(field.getOne().negate().negativeOrNull());
Assert.assertFalse(field.getOne().negate().positiveOrNull());
Assert.assertTrue(field.getOne().negate().strictlyNegative());
Assert.assertFalse(field.getOne().negate().strictlyPositive());
Assert.assertFalse(nan.negativeOrNull());
Assert.assertFalse(nan.positiveOrNull());
Assert.assertFalse(nan.strictlyNegative());
Assert.assertFalse(nan.strictlyPositive());
Assert.assertFalse(nan.negate().negativeOrNull());
Assert.assertFalse(nan.negate().positiveOrNull());
Assert.assertFalse(nan.negate().strictlyNegative());
Assert.assertFalse(nan.negate().strictlyPositive());
Assert.assertFalse(pinf.negativeOrNull());
Assert.assertTrue(pinf.positiveOrNull());
Assert.assertFalse(pinf.strictlyNegative());
Assert.assertTrue(pinf.strictlyPositive());
Assert.assertTrue(pinf.negate().negativeOrNull());
Assert.assertFalse(pinf.negate().positiveOrNull());
Assert.assertTrue(pinf.negate().strictlyNegative());
Assert.assertFalse(pinf.negate().strictlyPositive());
Assert.assertTrue(ninf.negativeOrNull());
Assert.assertFalse(ninf.positiveOrNull());
Assert.assertTrue(ninf.strictlyNegative());
Assert.assertFalse(ninf.strictlyPositive());
Assert.assertFalse(ninf.negate().negativeOrNull());
Assert.assertTrue(ninf.negate().positiveOrNull());
Assert.assertFalse(ninf.negate().strictlyNegative());
Assert.assertTrue(ninf.negate().strictlyPositive());
}
}