BigReal.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.util;


  22. import java.io.Serializable;
  23. import java.math.BigDecimal;
  24. import java.math.BigInteger;
  25. import java.math.MathContext;
  26. import java.math.RoundingMode;

  27. import org.hipparchus.Field;
  28. import org.hipparchus.FieldElement;
  29. import org.hipparchus.exception.LocalizedCoreFormats;
  30. import org.hipparchus.exception.MathRuntimeException;

  31. /**
  32.  * Arbitrary precision decimal number.
  33.  * <p>
  34.  * This class is a simple wrapper around the standard <code>BigDecimal</code>
  35.  * in order to implement the {@link FieldElement} interface.
  36.  */
  37. public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {

  38.     /** A big real representing 0. */
  39.     public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);

  40.     /** A big real representing 1. */
  41.     public static final BigReal ONE = new BigReal(BigDecimal.ONE);

  42.     /** Serializable version identifier. */
  43.     private static final long serialVersionUID = 4984534880991310382L;

  44.     /** Underlying BigDecimal. */
  45.     private final BigDecimal d;

  46.     /** Rounding mode for divisions. **/
  47.     private RoundingMode roundingMode = RoundingMode.HALF_UP;

  48.     /*** BigDecimal scale ***/
  49.     private int scale = 64;

  50.     /** Build an instance from a BigDecimal.
  51.      * @param val value of the instance
  52.      */
  53.     public BigReal(BigDecimal val) {
  54.         d =  val;
  55.     }

  56.     /** Build an instance from a BigInteger.
  57.      * @param val value of the instance
  58.      */
  59.     public BigReal(BigInteger val) {
  60.         d = new BigDecimal(val);
  61.     }

  62.     /** Build an instance from an unscaled BigInteger.
  63.      * @param unscaledVal unscaled value
  64.      * @param scale scale to use
  65.      */
  66.     public BigReal(BigInteger unscaledVal, int scale) {
  67.         d = new BigDecimal(unscaledVal, scale);
  68.     }

  69.     /** Build an instance from an unscaled BigInteger.
  70.      * @param unscaledVal unscaled value
  71.      * @param scale scale to use
  72.      * @param mc to used
  73.      */
  74.     public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
  75.         d = new BigDecimal(unscaledVal, scale, mc);
  76.     }

  77.     /** Build an instance from a BigInteger.
  78.      * @param val value of the instance
  79.      * @param mc context to use
  80.      */
  81.     public BigReal(BigInteger val, MathContext mc) {
  82.         d = new BigDecimal(val, mc);
  83.     }

  84.     /** Build an instance from a characters representation.
  85.      * @param in character representation of the value
  86.      */
  87.     public BigReal(char[] in) {
  88.         d = new BigDecimal(in);
  89.     }

  90.     /** Build an instance from a characters representation.
  91.      * @param in character representation of the value
  92.      * @param offset offset of the first character to analyze
  93.      * @param len length of the array slice to analyze
  94.      */
  95.     public BigReal(char[] in, int offset, int len) {
  96.         d = new BigDecimal(in, offset, len);
  97.     }

  98.     /** Build an instance from a characters representation.
  99.      * @param in character representation of the value
  100.      * @param offset offset of the first character to analyze
  101.      * @param len length of the array slice to analyze
  102.      * @param mc context to use
  103.      */
  104.     public BigReal(char[] in, int offset, int len, MathContext mc) {
  105.         d = new BigDecimal(in, offset, len, mc);
  106.     }

  107.     /** Build an instance from a characters representation.
  108.      * @param in character representation of the value
  109.      * @param mc context to use
  110.      */
  111.     public BigReal(char[] in, MathContext mc) {
  112.         d = new BigDecimal(in, mc);
  113.     }

  114.     /** Build an instance from a double.
  115.      * @param val value of the instance
  116.      */
  117.     public BigReal(double val) {
  118.         d = new BigDecimal(val); // NOPMD - we really want double conversion here
  119.     }

  120.     /** Build an instance from a double.
  121.      * @param val value of the instance
  122.      * @param mc context to use
  123.      */
  124.     public BigReal(double val, MathContext mc) {
  125.         d = new BigDecimal(val, mc); // NOPMD - we really want double conversion here
  126.     }

  127.     /** Build an instance from an int.
  128.      * @param val value of the instance
  129.      */
  130.     public BigReal(int val) {
  131.         d = new BigDecimal(val);
  132.     }

  133.     /** Build an instance from an int.
  134.      * @param val value of the instance
  135.      * @param mc context to use
  136.      */
  137.     public BigReal(int val, MathContext mc) {
  138.         d = new BigDecimal(val, mc);
  139.     }

  140.     /** Build an instance from a long.
  141.      * @param val value of the instance
  142.      */
  143.     public BigReal(long val) {
  144.         d = new BigDecimal(val);
  145.     }

  146.     /** Build an instance from a long.
  147.      * @param val value of the instance
  148.      * @param mc context to use
  149.      */
  150.     public BigReal(long val, MathContext mc) {
  151.         d = new BigDecimal(val, mc);
  152.     }

  153.     /** Build an instance from a String representation.
  154.      * @param val character representation of the value
  155.      */
  156.     public BigReal(String val) {
  157.         d = new BigDecimal(val);
  158.     }

  159.     /** Build an instance from a String representation.
  160.      * @param val character representation of the value
  161.      * @param mc context to use
  162.      */
  163.     public BigReal(String val, MathContext mc)  {
  164.         d = new BigDecimal(val, mc);
  165.     }

  166.     /** {@inheritDoc} */
  167.     @Override
  168.     public double getReal() {
  169.         return doubleValue();
  170.     }

  171.     /***
  172.      * Gets the rounding mode for division operations
  173.      * The default is {@code RoundingMode.HALF_UP}
  174.      * @return the rounding mode.
  175.      */
  176.     public RoundingMode getRoundingMode() {
  177.         return roundingMode;
  178.     }

  179.     /***
  180.      * Sets the rounding mode for decimal divisions.
  181.      * @param roundingMode rounding mode for decimal divisions
  182.      */
  183.     public void setRoundingMode(RoundingMode roundingMode) {
  184.         this.roundingMode = roundingMode;
  185.     }

  186.     /***
  187.      * Sets the scale for division operations.
  188.      * The default is 64
  189.      * @return the scale
  190.      */
  191.     public int getScale() {
  192.         return scale;
  193.     }

  194.     /***
  195.      * Sets the scale for division operations.
  196.      * @param scale scale for division operations
  197.      */
  198.     public void setScale(int scale) {
  199.         this.scale = scale;
  200.     }

  201.     /** {@inheritDoc} */
  202.     @Override
  203.     public BigReal add(BigReal a) {
  204.         return new BigReal(d.add(a.d));
  205.     }

  206.     /** {@inheritDoc} */
  207.     @Override
  208.     public BigReal subtract(BigReal a) {
  209.         return new BigReal(d.subtract(a.d));
  210.     }

  211.     /** {@inheritDoc} */
  212.     @Override
  213.     public BigReal negate() {
  214.         return new BigReal(d.negate());
  215.     }

  216.     /**
  217.      * {@inheritDoc}
  218.      *
  219.      * @throws MathRuntimeException if {@code a} is zero
  220.      */
  221.     @Override
  222.     public BigReal divide(BigReal a) throws MathRuntimeException {
  223.         try {
  224.             return new BigReal(d.divide(a.d, scale, roundingMode));
  225.         } catch (ArithmeticException e) {
  226.             // Division by zero has occurred
  227.             throw new MathRuntimeException(e, LocalizedCoreFormats.ZERO_NOT_ALLOWED);
  228.         }
  229.     }

  230.     /**
  231.      * {@inheritDoc}
  232.      *
  233.      * @throws MathRuntimeException if {@code this} is zero
  234.      */
  235.     @Override
  236.     public BigReal reciprocal() throws MathRuntimeException {
  237.         try {
  238.             return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode));
  239.         } catch (ArithmeticException e) {
  240.             // Division by zero has occurred
  241.             throw new MathRuntimeException(e, LocalizedCoreFormats.ZERO_NOT_ALLOWED);
  242.         }
  243.     }

  244.     /** {@inheritDoc} */
  245.     @Override
  246.     public BigReal multiply(BigReal a) {
  247.         return new BigReal(d.multiply(a.d));
  248.     }

  249.     /** {@inheritDoc} */
  250.     @Override
  251.     public BigReal multiply(final int n) {
  252.         return new BigReal(d.multiply(new BigDecimal(n)));
  253.     }

  254.     /** {@inheritDoc} */
  255.     @Override
  256.     public int compareTo(BigReal a) {
  257.         return d.compareTo(a.d);
  258.     }

  259.     /** Get the double value corresponding to the instance.
  260.      * @return double value corresponding to the instance
  261.      */
  262.     public double doubleValue() {
  263.         return d.doubleValue();
  264.     }

  265.     /** Get the BigDecimal value corresponding to the instance.
  266.      * @return BigDecimal value corresponding to the instance
  267.      */
  268.     public BigDecimal bigDecimalValue() {
  269.         return d;
  270.     }

  271.     /** {@inheritDoc} */
  272.     @Override
  273.     public boolean equals(Object other) {
  274.         if (this == other){
  275.             return true;
  276.         }

  277.         if (other instanceof BigReal){
  278.             return d.equals(((BigReal) other).d);
  279.         }
  280.         return false;
  281.     }

  282.     /** {@inheritDoc} */
  283.     @Override
  284.     public int hashCode() {
  285.         return d.hashCode();
  286.     }

  287.     /** {@inheritDoc} */
  288.     @Override
  289.     public Field<BigReal> getField() {
  290.         return BigRealField.getInstance();
  291.     }
  292. }