Java Code Examples for java.math.MathContext#DECIMAL128

The following examples show how to use java.math.MathContext#DECIMAL128 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegerDivideOperationCalculatorTest.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Test
public void calculate() throws Exception {
    MathContext mathContext = MathContext.DECIMAL128;
    RoundingMode roundingMode = RoundingMode.CEILING;
    RenderRequest request = mock(RenderRequest.class, RETURNS_DEEP_STUBS);
    BigDecimal left = mock(BigDecimal.class);
    BigDecimal right = mock(BigDecimal.class);
    BigDecimal leftRounded = mock(BigDecimal.class);
    BigDecimal rightRounded = mock(BigDecimal.class);
    BigDecimal divisionResult = mock(BigDecimal.class);
    BigDecimal expected = mock(BigDecimal.class);

    when(request.getEnvironment().getValueEnvironment().getMathContext()).thenReturn(mathContext);
    when(request.getEnvironment().getValueEnvironment().getRoundingMode()).thenReturn(roundingMode);
    when(left.setScale(0, roundingMode)).thenReturn(leftRounded);
    when(right.setScale(0, roundingMode)).thenReturn(rightRounded);
    when(leftRounded.divide(rightRounded, mathContext)).thenReturn(divisionResult);
    when(divisionResult.setScale(0, roundingMode)).thenReturn(expected);

    BigDecimal result = underTest.calculate(request, left, right);

    assertEquals(expected, result);
}
 
Example 2
Source File: DivisibleByTestExpressionCalculatorTest.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Test
public void calculateTrue() throws Exception {
    MathContext mathContext = MathContext.DECIMAL128;
    RenderRequest request = mock(RenderRequest.class, RETURNS_DEEP_STUBS);
    DivisibleByTestExpression test = mock(DivisibleByTestExpression.class);
    Expression argument = mock(Expression.class);
    Object argumentValue = new Object();
    Expression expression = mock(Expression.class);
    Object expressionValue = new Object();
    BigDecimal argumentNumber = mock(BigDecimal.class);
    BigDecimal expressionNumber = mock(BigDecimal.class);
    BigDecimal expected = BigDecimal.ZERO;

    when(test.getExpression()).thenReturn(expression);
    when(request.getEnvironment().getRenderEnvironment().getCalculateExpressionService().calculate(request, argument)).thenReturn(argumentValue);
    when(request.getEnvironment().getRenderEnvironment().getCalculateExpressionService().calculate(request, expression)).thenReturn(expressionValue);
    when(request.getEnvironment().getValueEnvironment().getNumberConverter().convert(argumentValue)).thenReturn(Converter.Result.defined(argumentNumber));
    when(request.getEnvironment().getValueEnvironment().getNumberConverter().convert(expressionValue)).thenReturn(Converter.Result.defined(expressionNumber));
    when(request.getEnvironment().getValueEnvironment().getMathContext()).thenReturn(mathContext);
    when(argumentNumber.remainder(expressionNumber, mathContext)).thenReturn(expected);

    Object result = underTest.calculate(request, mock(Position.class), test, argument);

    assertSame(true, result);
}
 
Example 3
Source File: DivisibleByTestExpressionCalculatorTest.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Test
public void calculateFalse() throws Exception {
    MathContext mathContext = MathContext.DECIMAL128;
    RenderRequest request = mock(RenderRequest.class, RETURNS_DEEP_STUBS);
    DivisibleByTestExpression test = mock(DivisibleByTestExpression.class);
    Expression argument = mock(Expression.class);
    Object argumentValue = new Object();
    Expression expression = mock(Expression.class);
    Object expressionValue = new Object();
    BigDecimal argumentNumber = mock(BigDecimal.class);
    BigDecimal expressionNumber = mock(BigDecimal.class);
    BigDecimal expected = BigDecimal.TEN;

    when(test.getExpression()).thenReturn(expression);
    when(request.getEnvironment().getRenderEnvironment().getCalculateExpressionService().calculate(request, argument)).thenReturn(argumentValue);
    when(request.getEnvironment().getRenderEnvironment().getCalculateExpressionService().calculate(request, expression)).thenReturn(expressionValue);
    when(request.getEnvironment().getValueEnvironment().getNumberConverter().convert(argumentValue)).thenReturn(Converter.Result.defined(argumentNumber));
    when(request.getEnvironment().getValueEnvironment().getNumberConverter().convert(expressionValue)).thenReturn(Converter.Result.defined(expressionNumber));
    when(request.getEnvironment().getValueEnvironment().getMathContext()).thenReturn(mathContext);
    when(argumentNumber.remainder(expressionNumber, mathContext)).thenReturn(expected);

    Object result = underTest.calculate(request, mock(Position.class), test, argument);

    assertSame(false, result);
}
 
Example 4
Source File: BigDecimalStreamExample.java    From big-math with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	MathContext mathContext = MathContext.DECIMAL128;

	System.out.println("Range [0, 10) step 1 (using BigDecimal as input parameters)");
	BigDecimalStream.range(BigDecimal.valueOf(0), BigDecimal.valueOf(10), BigDecimal.ONE, mathContext)
		.forEach(System.out::println);
	System.out.println();
	
	System.out.println("Range [0, 12] step 3 (using long as input parameters)");
	BigDecimalStream.rangeClosed(0, 12, 3, mathContext)
		.forEach(System.out::println);
	System.out.println();

	System.out.println("Range [0, 10) step 3 (using double as input parameters)");
	BigDecimalStream.range(0.0, 10.0, 3.0, mathContext)
		.forEach(System.out::println);
	System.out.println();

	System.out.println("Range [10, 0) step -1");
	BigDecimalStream.range(10, 0, -1, mathContext)
		.forEach(System.out::println);
	System.out.println();

	System.out.println("Range [10, 0] step -1");
	BigDecimalStream.rangeClosed(10, 0, -1, mathContext)
		.forEach(System.out::println);
	System.out.println();
	
	System.out.println("Range [0, 10] step 1 parallel");
	BigDecimalStream.rangeClosed(0, 10, 1, mathContext)
		.parallel()
		.forEach(System.out::println);
	System.out.println();

}
 
Example 5
Source File: JexlArithmetic.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a JexlArithmetic.
 * <p>The constructor to define in derived classes.
 *
 * @param astrict     whether this arithmetic is lenient or strict
 * @param bigdContext the math context instance to use for +,-,/,*,% operations on big decimals.
 * @param bigdScale   the scale used for big decimals.
 */
public JexlArithmetic(boolean astrict, MathContext bigdContext, int bigdScale) {
    this.strict = astrict;
    this.mathContext = bigdContext == null ? MathContext.DECIMAL128 : bigdContext;
    this.mathScale = bigdScale == Integer.MIN_VALUE ? BIGD_SCALE : bigdScale;
    Constructor<? extends JexlArithmetic> actor = null;
    try {
        actor = getClass().getConstructor(boolean.class, MathContext.class, int.class);
    } catch (Exception xany) {
        // ignore
    }
    this.ctor = actor;
}
 
Example 6
Source File: EnvironmentConfigurationBuilderTest.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Test
public void valueConfig() throws Exception {
    ValueComparator valueComparator = mock(ValueComparator.class);
    StringConverter stringConverter = mock(StringConverter.class);
    Converter<BigDecimal> numberConverter = mock(Converter.class);
    Converter<Boolean> booleanConverter = mock(Converter.class);
    Converter<Character> charConverter = mock(Converter.class);
    Converter<WrappedCollection> collectionConverter = mock(Converter.class);
    MathContext mathContext = MathContext.DECIMAL128;
    RoundingMode roundingMode = RoundingMode.HALF_UP;

    EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder
            .configuration()
            .value()
            .withMathContext(mathContext)
            .withRoundingMode(roundingMode)
            .withValueComparator(valueComparator)
            .withStringConverter(stringConverter)
            .numberConverters().add(numberConverter).and()
            .booleanConverters().add(booleanConverter).and()
            .charConverters().add(charConverter).and()
            .collectionConverters().add(collectionConverter).and()
            .and()
            .build();

    assertSame(configuration.getValueConfiguration().getMathContext(), mathContext);
    assertSame(configuration.getValueConfiguration().getRoundingMode(), roundingMode);
    assertSame(configuration.getValueConfiguration().getStringConverter(), stringConverter);
    assertSame(configuration.getValueConfiguration().getValueComparator(), valueComparator);
    assertThat(configuration.getValueConfiguration().getBooleanConverters(), hasItem(booleanConverter));
    assertThat(configuration.getValueConfiguration().getNumberConverters(), hasItem(numberConverter));
    assertThat(configuration.getValueConfiguration().getCharConverters(), hasItem(charConverter));
    assertThat(configuration.getValueConfiguration().getCollectionConverters(), hasItem(collectionConverter));
}
 
Example 7
Source File: ModOperationCalculatorTest.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Test
public void calculate() throws Exception {
    MathContext mathContext = MathContext.DECIMAL128;
    RenderRequest request = mock(RenderRequest.class, RETURNS_DEEP_STUBS);
    BigDecimal left = mock(BigDecimal.class);
    BigDecimal right = mock(BigDecimal.class);
    BigDecimal expected = mock(BigDecimal.class);

    when(request.getEnvironment().getValueEnvironment().getMathContext()).thenReturn(mathContext);
    when(left.remainder(right, mathContext)).thenReturn(expected);

    BigDecimal result = underTest.calculate(request, left, right);

    assertSame(expected, result);
}
 
Example 8
Source File: SqoopHCatImportHelper.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 5 votes vote down vote up
private Object convertStringTypes(Object val, HCatFieldSchema hfs) {
  HCatFieldSchema.Type hfsType = hfs.getType();
  if (hfsType == HCatFieldSchema.Type.STRING
      || hfsType == HCatFieldSchema.Type.VARCHAR
      || hfsType == HCatFieldSchema.Type.CHAR) {
    String str = val.toString();
    if (doHiveDelimsReplacement) {
      str = FieldFormatter.hiveStringReplaceDelims(str,
        hiveDelimsReplacement, hiveDelimiters);
    }
    if (hfsType == HCatFieldSchema.Type.STRING) {
      return str;
    } else if (hfsType == HCatFieldSchema.Type.VARCHAR) {
      VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo();
      HiveVarchar hvc = new HiveVarchar(str, vti.getLength());
      return hvc;
    } else if (hfsType == HCatFieldSchema.Type.CHAR) {
      CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo();
      HiveChar hc = new HiveChar(val.toString(), cti.getLength());
      return hc;
    }
  } else if (hfsType == HCatFieldSchema.Type.DECIMAL) {
    BigDecimal bd = new BigDecimal(val.toString(), MathContext.DECIMAL128);
    HiveDecimal hd = HiveDecimal.create(bd);
    return hd;
  }
  return null;
}
 
Example 9
Source File: LegendreHighPrecisionRuleFactory.java    From hipparchus with Apache License 2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 10
Source File: PrimitiveFactory.java    From mr4c with Apache License 2.0 4 votes vote down vote up
public BigDecimal parse(String str) {
	return new BigDecimal(str, MathContext.DECIMAL128);
}
 
Example 11
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 12
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 13
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 14
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 15
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 16
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}
 
Example 17
Source File: LegendreHighPrecisionRuleFactory.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}.
 */
public LegendreHighPrecisionRuleFactory() {
    this(MathContext.DECIMAL128);
}