001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.util; 018 019 020 import java.io.Serializable; 021 import java.math.BigDecimal; 022 import java.math.BigInteger; 023 import java.math.MathContext; 024 025 import org.apache.commons.math.Field; 026 import org.apache.commons.math.FieldElement; 027 028 /** 029 * Arbitrary precision decimal number. 030 * <p> 031 * This class is a simple wrapper around the standard <code>BigDecimal</code> 032 * in order to implement the {@link FieldElement} interface. 033 * </p> 034 * @since 2.0 035 * @version $Revision: 795963 $ $Date: 2009-07-20 15:23:43 -0400 (Mon, 20 Jul 2009) $ 036 */ 037 public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable { 038 039 /** Serializable version identifier. */ 040 private static final long serialVersionUID = 7887631840434052850L; 041 042 /** A big real representing 0. */ 043 public static final BigReal ZERO = new BigReal(BigDecimal.ZERO); 044 045 /** A big real representing 1. */ 046 public static final BigReal ONE = new BigReal(BigDecimal.ONE); 047 048 /** Underlying BigDecimal. */ 049 private final BigDecimal d; 050 051 /** Build an instance from a BigDecimal. 052 * @param val value of the instance 053 */ 054 public BigReal(BigDecimal val) { 055 d = val; 056 } 057 058 /** Build an instance from a BigInteger. 059 * @param val value of the instance 060 */ 061 public BigReal(BigInteger val) { 062 d = new BigDecimal(val); 063 } 064 065 /** Build an instance from an unscaled BigInteger. 066 * @param unscaledVal unscaled value 067 * @param scale scale to use 068 */ 069 public BigReal(BigInteger unscaledVal, int scale) { 070 d = new BigDecimal(unscaledVal, scale); 071 } 072 073 /** Build an instance from an unscaled BigInteger. 074 * @param unscaledVal unscaled value 075 * @param scale scale to use 076 * @param mc to used 077 */ 078 public BigReal(BigInteger unscaledVal, int scale, MathContext mc) { 079 d = new BigDecimal(unscaledVal, scale, mc); 080 } 081 082 /** Build an instance from a BigInteger. 083 * @param val value of the instance 084 * @param mc context to use 085 */ 086 public BigReal(BigInteger val, MathContext mc) { 087 d = new BigDecimal(val, mc); 088 } 089 090 /** Build an instance from a characters representation. 091 * @param in character representation of the value 092 */ 093 public BigReal(char[] in) { 094 d = new BigDecimal(in); 095 } 096 097 /** Build an instance from a characters representation. 098 * @param in character representation of the value 099 * @param offset offset of the first character to analyze 100 * @param len length of the array slice to analyze 101 */ 102 public BigReal(char[] in, int offset, int len) { 103 d = new BigDecimal(in, offset, len); 104 } 105 106 /** Build an instance from a characters representation. 107 * @param in character representation of the value 108 * @param offset offset of the first character to analyze 109 * @param len length of the array slice to analyze 110 * @param mc context to use 111 */ 112 public BigReal(char[] in, int offset, int len, MathContext mc) { 113 d = new BigDecimal(in, offset, len, mc); 114 } 115 116 /** Build an instance from a characters representation. 117 * @param in character representation of the value 118 * @param mc context to use 119 */ 120 public BigReal(char[] in, MathContext mc) { 121 d = new BigDecimal(in, mc); 122 } 123 124 /** Build an instance from a double. 125 * @param val value of the instance 126 */ 127 public BigReal(double val) { 128 d = new BigDecimal(val); 129 } 130 131 /** Build an instance from a double. 132 * @param val value of the instance 133 * @param mc context to use 134 */ 135 public BigReal(double val, MathContext mc) { 136 d = new BigDecimal(val, mc); 137 } 138 139 /** Build an instance from an int. 140 * @param val value of the instance 141 */ 142 public BigReal(int val) { 143 d = new BigDecimal(val); 144 } 145 146 /** Build an instance from an int. 147 * @param val value of the instance 148 * @param mc context to use 149 */ 150 public BigReal(int val, MathContext mc) { 151 d = new BigDecimal(val, mc); 152 } 153 154 /** Build an instance from a long. 155 * @param val value of the instance 156 */ 157 public BigReal(long val) { 158 d = new BigDecimal(val); 159 } 160 161 /** Build an instance from a long. 162 * @param val value of the instance 163 * @param mc context to use 164 */ 165 public BigReal(long val, MathContext mc) { 166 d = new BigDecimal(val, mc); 167 } 168 169 /** Build an instance from a String representation. 170 * @param val character representation of the value 171 */ 172 public BigReal(String val) { 173 d = new BigDecimal(val); 174 } 175 176 /** Build an instance from a String representation. 177 * @param val character representation of the value 178 * @param mc context to use 179 */ 180 public BigReal(String val, MathContext mc) { 181 d = new BigDecimal(val, mc); 182 } 183 184 /** {@inheritDoc} */ 185 public BigReal add(BigReal a) { 186 return new BigReal(d.add(a.d)); 187 } 188 189 /** {@inheritDoc} */ 190 public BigReal subtract(BigReal a) { 191 return new BigReal(d.subtract(a.d)); 192 } 193 194 /** {@inheritDoc} */ 195 public BigReal divide(BigReal a) throws ArithmeticException { 196 return new BigReal(d.divide(a.d)); 197 } 198 199 /** {@inheritDoc} */ 200 public BigReal multiply(BigReal a) { 201 return new BigReal(d.multiply(a.d)); 202 } 203 204 /** {@inheritDoc} */ 205 public int compareTo(BigReal a) { 206 return d.compareTo(a.d); 207 } 208 209 /** Get the double value corresponding to the instance. 210 * @return double value corresponding to the instance 211 */ 212 public double doubleValue() { 213 return d.doubleValue(); 214 } 215 216 /** Get the BigDecimal value corresponding to the instance. 217 * @return BigDecimal value corresponding to the instance 218 */ 219 public BigDecimal bigDecimalValue() { 220 return d; 221 } 222 223 /** {@inheritDoc} */ 224 @Override 225 public boolean equals(Object other) { 226 try { 227 if (other == null) { 228 return false; 229 } 230 return d.equals(((BigReal) other).d); 231 } catch (ClassCastException cce) { 232 return false; 233 } 234 } 235 236 /** {@inheritDoc} */ 237 @Override 238 public int hashCode() { 239 return d.hashCode(); 240 } 241 242 /** {@inheritDoc} */ 243 public Field<BigReal> getField() { 244 return BigRealField.getInstance(); 245 } 246 247 }