MJMath
MJMath is a helper class that augments
the IMathOperators
and IMathFunctions
interfaces
supported by the MJDecimal
,
MJInteger
and MJVariant
variable classes. It offers convenience
methods for operating on java.lang.Number
and MJ variables, including binary operations
that are not commutative (e.g., subtraction and division) and alternative method signatures for
functions such as FRAC
and MOD
.
Supports Math on MJString
For backward compatibility with MAPPER, MJMath
also supports math
on MJString
variables. Like MAPPER, MJ requires string
variables to contain valid numeric content. Also like MAPPER, in MJString
variables
subject to useAlphaCompare
(MAPPER "type A"), the scan for characters that
appear numeric ends upon encountering first, non-leading white space.
These examples illustrate the points above:
//////////////////// Divide java.lang.Number by MJDecimal (@CHG) ////////
// @LDV <dvi>f4=7 .
// @CHG <fltQuot>f18.17 22 / <dvi> .
MJDecimal dvi = new MJDecimal(VariableScope.LOCAL, 4, 0, new BigDecimal("7"));
Number quot = MJMath.divide(22, dvi, EnumSet.of(MathOption.EVALUATE_JAVA_TYPE));
assert quot instanceof BigDecimal;
MJDecimal fltQuot = new MJDecimal(VariableScope.LOCAL, 18, 17, (BigDecimal) quot);
assert "3.1428571428571400".equals(fltQuot.toMapperNumericString());
//////////////////// MOD MJString by MJInteger (@ART) ///////////////////
// @LDV <sdvd>s17='112233445566.98765' .
// @LDV <idiv>i13=-44444444533 .
// @ART MOD(<sdvd>,<idiv>) <modRslt>f18.17 .
MJString sdvd = new MJString(VariableScope.LOCAL, MaprptVariableType.STRING, 17,
EnumSet.of(LoadOption.TRUNCATE), "112233445566.98765");
MJInteger idiv = new MJInteger(VariableScope.LOCAL, 13,
EnumSet.noneOf(LoadOption.class), "-44444444533");
Number mod = MJMath.mod(sdvd, idiv, EnumSet.of(MathOption.EVALUATE_MAPPER_TYPE,
MathOption.PREFER_DECIMAL_OVER_FLOAT));
assert mod instanceof BigDecimal;
MJDecimal modRslt = new MJDecimal(VariableScope.LOCAL, 18, 17, (BigDecimal) mod);
assert "23344556500.987600".equals(modRslt.toMapperNumericString());