Java Code Examples for java.math.MathContext#DECIMAL64

The following examples show how to use java.math.MathContext#DECIMAL64 . 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: GRBL_1_1_StatusReportParser.java    From sourcerabbit-gcode-sender with GNU General Public License v2.0 6 votes vote down vote up
private BigDecimal getWorkPositionFromMachinePositionAndOffset(double machinePosition, double offset)
{
    // Calculate the work position !!
    // WPos = MPos - WCO (offset)

    BigDecimal result = new BigDecimal(0, MathContext.DECIMAL64);
    result.setScale(0);

    BigDecimal machinePositionBigDecimal = new BigDecimal(0, MathContext.DECIMAL64);
    machinePositionBigDecimal.setScale(0);
    machinePositionBigDecimal = machinePositionBigDecimal.add(new BigDecimal(machinePosition, MathContext.DECIMAL64));

    BigDecimal offsetBigDecimal = new BigDecimal(0, MathContext.DECIMAL64);
    offsetBigDecimal.setScale(0);
    offsetBigDecimal = offsetBigDecimal.add(new BigDecimal(offset, MathContext.DECIMAL64));

    result = result.add(machinePositionBigDecimal).subtract(offsetBigDecimal);

    return result;
}
 
Example 2
Source File: PerformanceBigDecimalMath.java    From big-math with MIT License 5 votes vote down vote up
private static void functionValueAtan2_yx_neg10_to_10() {
	MathContext mathContext = MathContext.DECIMAL64;
	BigDecimal xStart = BigDecimal.valueOf(-10);
	BigDecimal xEnd = BigDecimal.valueOf(10);
	BigDecimal xStep = BigDecimal.valueOf(0.2);
	BigDecimal yStart = BigDecimal.valueOf(-10);
	BigDecimal yEnd = BigDecimal.valueOf(10);
	BigDecimal yStep = BigDecimal.valueOf(0.2);

	String name = "values_atan2_yx_from_-10_to_10.csv";

	try (PrintWriter writer = new PrintWriter(new FileWriter(OUTPUT_DIRECTORY + name))) {
		for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
			writer.print(", ");
			writer.print(x);
		}
		writer.println();

		for (BigDecimal y = yStart; y.compareTo(yEnd) <= 0; y = y.add(yStep)) {
			writer.print(y);
			writer.print(", ");
			for (BigDecimal x = xStart; x.compareTo(xEnd) <= 0; x = x.add(xStep)) {
				if (!x.equals(xStart)) {
					writer.print(", ");
				}
				System.out.println("x = " + x + " y = " + y);
				double result;
				try {
					result = BigDecimalMath.atan2(y, x, mathContext).doubleValue();
				} catch (Exception ex) {
					result = Double.NaN;
				}
				writer.print(result);
			}
			writer.println();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 3
Source File: DivideOperationCalculatorTest.java    From jtwig-core with Apache License 2.0 5 votes vote down vote up
@Test
public void calculate() throws Exception {
    BigDecimal expected = BigDecimal.ONE;
    MathContext mathContext = MathContext.DECIMAL64;
    RenderRequest request = mock(RenderRequest.class, RETURNS_DEEP_STUBS);
    BigDecimal left = mock(BigDecimal.class);
    BigDecimal right = mock(BigDecimal.class);

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

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

    assertEquals(expected, result);
}
 
Example 4
Source File: GRBL_1_1_StatusReportParser.java    From sourcerabbit-gcode-sender with GNU General Public License v2.0 5 votes vote down vote up
private BigDecimal parseMachineOrWorkPositionToBigDecimal64(String value)
{
    double valueToDouble = Double.parseDouble(value);
    BigDecimal bigDecimalValue = new BigDecimal(0, MathContext.DECIMAL64);
    bigDecimalValue = bigDecimalValue.setScale(2);
    bigDecimalValue = bigDecimalValue.add(new BigDecimal(valueToDouble, MathContext.DECIMAL64));
    return bigDecimalValue;
}
 
Example 5
Source File: Price.java    From java-stellar-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns price as a string.
 */
public String toString() {
    MathContext mc = MathContext.DECIMAL64;
    BigDecimal result = new BigDecimal(this.n).divide(new BigDecimal(this.d), mc);

    return result.toString();
}
 
Example 6
Source File: AnnualPercentageYield.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param rate    the target rate, not null.
 * @param periods the periods, &gt;= 0.
 * @return the resulting amount, never null.
 */
public static Rate calculate(Rate rate, int periods) {
    if(periods==0){
        return Rate.zero(toString(rate, periods));
    }
    final BigDecimal ONE = new BigDecimal(1, MathContext.DECIMAL64);
    BigDecimal baseFactor = rate.get().divide(BigDecimal.valueOf(periods),MathContext.DECIMAL64)
            .add(ONE);
    return Rate.of(baseFactor.pow(periods).subtract(ONE),
            toString(rate, periods));
}
 
Example 7
Source File: AnnualPercentageYield.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the calculation.
 *
 * @param amount  the base amount, not null.
 * @param rate    the target rate, not null.
 * @param periods the periods, &gt;= 0.
 * @return the resulting amount, never null.
 */
public static MonetaryAmount calculate(MonetaryAmount amount, Rate rate, int periods) {
    if(periods==0){
        return amount.getFactory().setNumber(0.0).create();
    }
    final BigDecimal ONE = new BigDecimal(1, MathContext.DECIMAL64);
    BigDecimal baseFactor = rate.get().divide(BigDecimal.valueOf(periods),MathContext.DECIMAL64)
            .add(ONE);
    return amount.multiply(baseFactor.pow(periods).subtract(ONE));
}
 
Example 8
Source File: Expression.java    From fin-expr with Apache License 2.0 4 votes vote down vote up
public Expression(String expr) {
	this(expr, MathContext.DECIMAL64);
}
 
Example 9
Source File: PentahoAvroReadWriteTest.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
@Test
public void testConvertToBooleanOnOutput() throws Exception {
  Object[] rowData =
    new Object[] { "Y", "Row1Field2", new Double( 3.1 ), new BigDecimal( "4.1", MathContext.DECIMAL64 ), true,
      new Long( 0 ) };
  String[][] outputSchemaDescription = new String[][] {
    { "avroField1", "pentahoField1", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_STRING ), "0", "0" },
    { "avroField2", "pentahoField2", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_STRING ), "0", "0" },
    { "avroDouble3", "pentahoNumber3", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_NUMBER ), "0", "0" },
    { "avroDecimal4", "pentahoBigNumber4", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BIGNUMBER ), "2", "1" },
    { "avroBoolean6", "pentahoBoolean6", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ), "0", "0" },
    { "avroInt7", "pentahoInt7", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_INTEGER ), "0", "0" }
  };

  String[][] inputSchemaDescription = new String[][] {
    { "avroField1", "pentahoField1", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ) },
    { "avroField2", "pentahoField2", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ) },
    { "avroDouble3", "pentahoNumber3", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ) },
    { "avroDecimal4", "pentahoBigNumber4", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ) },
    { "avroBoolean6", "pentahoBoolean6", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ) },
    { "avroInt7", "pentahoInt7", String.valueOf( AvroSpec.DataType.BOOLEAN.ordinal() ),
      String.valueOf( ValueMetaInterface.TYPE_BOOLEAN ) }
  };


  RowMeta rowMeta = buildRowMeta( outputSchemaDescription );
  RowMetaAndData rowMetaAndData = new RowMetaAndData( rowMeta, rowData );

  Boolean[] expectedResults = new Boolean[ rowData.length ];
  for ( int i = 0; i < rowMetaAndData.size(); i++ ) {
    expectedResults[ i ] = rowMetaAndData.getBoolean( i, true );
  }

  doReadWrite( inputSchemaDescription, outputSchemaDescription, rowData,
    IPentahoAvroOutputFormat.COMPRESSION.UNCOMPRESSED, "avroOutputNone.avro", null, expectedResults, true );
}
 
Example 10
Source File: BigDecimals.java    From development with Apache License 2.0 2 votes vote down vote up
/**
 * Multiplies the big decimal value with the double factor, using BigDecimal
 * values to not loose precision when internal calculation with double
 * precision takes place.
 * 
 * @param x
 *            The value to be multiplied.
 * @param factor
 *            The factor of type double.
 * @return The rounded multiplication result.
 */
public static BigDecimal multiply(final BigDecimal x, double factor) {
    BigDecimal multiplicand = new BigDecimal(factor, MathContext.DECIMAL64);
    BigDecimal multiplicationResult = x.multiply(multiplicand);
    return multiplicationResult;
}
 
Example 11
Source File: PermilOperator.java    From jsr354-ri with Apache License 2.0 2 votes vote down vote up
/**
 * Get {@link MathContext} for {@link PermilOperator} instances.
 *
 * @return the {@link MathContext} to be used, by default
 *         {@link MathContext#DECIMAL64}.
 */
private static MathContext initDefaultMathContext() {
	return MathContext.DECIMAL64;
}