diff --git a/pom.xml b/pom.xml
index 4e109b8..7d1a18c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,10 +4,29 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
+
+ org.sonatype.oss
+ oss-parent
+ 7
+
+
org.nishi
bigNumber
2.0.0
-
+ A bignum java library to unite java.math.BigDecimal and java.math.BigInteger.
+ http://nishi-inc.github.io/bigNumber/
+
+
+ The Apache Software License, Version 2.0
+ http://www.apache.org/licenses/LICENSE-2.0.txt
+ repo
+
+
+
+ scm:git:git@github.com:Nishi-Inc/bigNumber.git
+ scm:git:git@github.com:Nishi-Inc/bigNumber.git
+ git@github.com:Nishi-Inc/bigNumber.git
+
Alok Shukla
@@ -24,6 +43,18 @@
src/main/java
src/test/java
+
+
+
+ maven-compiler-plugin
+ 2.3.2
+
+ 1.7
+ 1.7
+
+
+
+
diff --git a/src/main/java/org/bigNumber/common/interfaces/NonStaticMethods.java b/src/main/java/org/bigNumber/common/interfaces/NonStaticMethods.java
index b715a82..1b3c248 100644
--- a/src/main/java/org/bigNumber/common/interfaces/NonStaticMethods.java
+++ b/src/main/java/org/bigNumber/common/interfaces/NonStaticMethods.java
@@ -6,7 +6,6 @@
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.math.MathContext;
import java.util.List;
/**
@@ -18,36 +17,35 @@
public interface NonStaticMethods extends Comparable, Serializable {
/**
- * @author Nishi Inc.
* @since v1.0.0
* @param number To add to this
+ * @return BigNumber Sum of this + givenNumber
*/
- public void add(BigNumber number);
+ public BigNumber add(BigNumber number);
/**
- * Now BigNumber this will hold product of this and provided number
- * @author Nishi Inc.
+ * @return BigNumber product of this and provided number
* @since v1.0.0
* @param number To multiply to this
*/
- public void multiply(BigNumber number);
+ public BigNumber multiply(BigNumber number);
/**
- * @author Nishi Inc.
+ * @return BigNumber this - number
* @since v1.0.0
- * @param number To multiply to this
+ * @param number To subtract from this
*/
- public void subtract(BigNumber number);
+ public BigNumber subtract(BigNumber number);
/**
- * @author Nishi Inc.
+ *
* @since v1.0.0
* @param number to divide this with
+ * @return BigNumber this/number
*/
- public void divide(BigNumber number);
+ public BigNumber divide(BigNumber number);
/**
- * @author Nishi Inc.
* @since v1.0.0
* @param number A BigNumber to divide and compute remainder
* @return An array of two BigNumber elements, the quotient (this/number) is the 0th element and remainder (this%number) is the 1st element
@@ -55,29 +53,27 @@ public interface NonStaticMethods extends Comparable, Serializable {
public BigNumber[] divideAndRemainder(BigNumber number);
/**
- * After this operation this will hold value of +ve value of this
- * @author Nishi Inc.
+ * @return If this.isNegative()
is true, - this
+ * else this
* @since v1.0.0
*/
- public void absolute();
+ public BigNumber absolute();
/**
- * @author Nishi Inc.
+ * @return BigNumber containing this % denominator
+ * @param denominator The number to divide this
with
* @since v1.0.0
- * @param denominator
*/
- public void modulus(BigNumber denominator);
+ public BigNumber modulus(BigNumber denominator);
/**
- * After this operation this = this^power
- * @author Nishi Inc.
+ * @return this^power
+ * @param power int to raise power with
* @since v1.0.0
- * @param power
*/
- public void pow(int power);
+ public BigNumber pow(int power);
/**
- * @author Nishi Inc
* @since v1.1.0
* @return A BigNumber, Factorial value of the calling number no change in the calling number will be made
* @deprecated
@@ -163,130 +159,116 @@ public interface NonStaticMethods extends Comparable, Serializable {
/**
* Gives the character at specified index
- * @param index
+ * @param index where?
* @return The character at the specified index
- * @author Nishi Inc.
* @since v0.1.0
*/
public Character charAt(int index);
/**
- * It doesn't return an Integer. It discards the fractional part and keeps the whole number.
- * @author Nishi Inc.
+ * @return An Integer. It discards the fractional part and keeps the whole number.
* @since v0.1.0
*/
- public void convertToInteger();
+ public BigNumber convertToInteger();
/**
- * Converts a number to negative
- * @author Nishi Inc.
+ * @return Converts a number to negative
* @since August 8, 2013, v0.1.0
*/
- public void makeNegative();
+ public BigNumber makeNegative();
/**
- * Rounds off a number to the given number of digits
- * @author Nishi Inc.
+ * @return Rounds off a number to the given number of digits
* @param mathContext A BigNumberMathContext object to define roundingOff parameters
* @since August 8, 2013, v0.1.0
* @see BigNumberMathContext
*/
- public void roundOff(BigNumberMathContext mathContext);
+ public BigNumber roundOff(BigNumberMathContext mathContext);
/**
- * Rounds off a number to the number of digits specified by GlobalConstants.DEFAULT_ROUND_OFF_DIGITS
- * @author Nishi Inc.
+ * @return Rounds off a number to the number of digits specified by GlobalConstants.DEFAULT_ROUND_OFF_DIGITS
* @since August 8, 2013, v0.1.0
*/
- public void roundOff();
+ public BigNumber roundOff();
/**
- * Changes the digit at the given index to the given digit
- * @param index
- * @param newDigit
- * @author Nishi Inc.
+ * @return Changes the digit at the given index to the given digit
+ * @param index where
+ * @param newDigit changes to this newDigit
* @since v0.1.0
*/
- public void modify(int index, int newDigit);
+ public BigNumber modify(int index, int newDigit);
/**
- * Changes the digit at the given index to the given digit
- * @param index
- * @param newDigit
- * @author Nishi Inc.
+ * @return Changes the digit at the given index to the given digit
+ * @param index where to change
+ * @param newDigit what to change to
* @since v0.1.0
*/
- public void modify(int index, char newDigit);
+ public BigNumber modify(int index, char newDigit);
/**
- * Inserts a new digit at the given index
- * @param index
- * @param digit
- * @author Nishi Inc.
+ * @return Inserts a new digit at the given index
+ * @param index where?
+ * @param digit what?
* @since v0.1.0
*/
- public void insert(int index, char digit);
+ public BigNumber insert(int index, char digit);
/**
- * Inserts a new digit at the given index
- * @param index
- * @param digit
- * @author Nishi Inc.
+ * @return Inserts a new digit at the given index
+ * @param index where to insert
+ * @param digit what to insert
* @since v0.1.0
*/
- public void insert(int index, int digit);
+ public BigNumber insert(int index, int digit);
/**
- * Puts the given digit at the beginning of the given number
+ * @return Puts the given digit at the beginning of the given number
* @param digit An int
- * @author Nishi Inc.
* @since August 8, 2013, v0.1.0
*/
- public void putAtFirst(int digit);
+ public BigNumber putAtFirst(int digit);
/**
- * Puts the given character at the beginning of the given number
+ * @return Puts the given character at the beginning of the given number
* @param character A char
* @author Nishi Inc.
* @since August 8, 2013, v0.1.0
*/
- public void putAtFirst(char character);
+ public BigNumber putAtFirst(char character);
/**
- * Reverses the BigNumber
- * @author Nishi Inc.
+ * @return Reverses the BigNumber
* @since v0.1.0
*/
- public void reverse();
+ public BigNumber reverse();
/**
- * Removes leading zeroes as well as trailing zeroes in case of fraction
- * @author Nishi Inc.
+ * @return Removes leading zeroes as well as trailing zeroes in case of fraction
* @since v1.0.0
*/
- public void consolidate();
+ public BigNumber consolidate();
/**
- * Appends the given number to the calling number
- * @param number
- * @author Nishi Inc.
+ * @return Appends the given number to the calling number
+ * @param number A BigNumber
* @since v0.1.0
*/
- public void append(BigNumber number);
+ public BigNumber append(BigNumber number);
/**
- * Appends the given positive number to the BigNumber number
- * @param number
- * @author Nishi Inc.
+ * @return Appends the given positive number to the BigNumber number
+ * @param number A positive int
* @since v0.1.0
*/
- public void append (int number);
+ public BigNumber append (int number);
/**
- * Does nothing if this is already fractional else appends ".00"
+ * @return The same BigNumber if this is already fractional else appends ".00"
* @since v1.0.0
*/
- public void makeFractional();
+ public BigNumber makeFractional();
/**
*
diff --git a/src/main/java/org/bigNumber/common/services/BigNumberUtils.java b/src/main/java/org/bigNumber/common/services/BigNumberUtils.java
index 87a5c1c..f87c44b 100644
--- a/src/main/java/org/bigNumber/common/services/BigNumberUtils.java
+++ b/src/main/java/org/bigNumber/common/services/BigNumberUtils.java
@@ -1,5 +1,7 @@
package org.bigNumber.common.services;
+import org.bigNumber.models.BigNumber;
+
/**
* @author aloks
* Date: 21/11/13
@@ -14,4 +16,241 @@ public final class BigNumberUtils {
// Singleton
private BigNumberUtils(){}
+ /*==========================================================
+ * Public Static Methods
+ * =========================================================
+ */
+
+ /**
+ * Same as sum()
+ * @param numbers BigNumber numbers
+ * @return A BigNumber type variable containing result of arithmetic addition of the provided numbers
+ */
+ public static BigNumber add(BigNumber... numbers) {
+ int total = numbers.length;
+
+ BigNumber result = new BigNumber(numbers[0]);
+
+ for(int i=1; ireverse()
+ * @param number A BigNumber number
+ * @return A BigNumber number with its digits in reverse order
+ * @author Nishi Inc.
+ * @since v1.2.0
+ */
+ public static BigNumber reverse(BigNumber number) {
+ BigNumber result = new BigNumber(number);
+ result.reverse();
+ return result;
+ }
+
+ /**
+ * Works similar to BigNumber.reverse()
+ * @param numbers
+ * @return An array of BigNumber numbers containing BigNumber numbers which have their digits in reverse order
+ * @author Nishi Inc.
+ * @since v1.0.0
+ */
+ public static BigNumber[] reverse(BigNumber... numbers) {
+ int size = numbers.length;
+ BigNumber[] result = new BigNumber[size];
+ for(int i=0; i val = this.getValue();
- for(int digit : val) {
- digit -= GlobalConstants.ASCII_ZERO;
- if(digit>=0 && digit<=9) {
- hash = hash * PRIME + digit;
- }
- }
- return hash;
- }
-
- @Override
- public boolean equals(Object number) {
- if(number instanceof BigNumber && this.compareTo((BigNumber) number) == 0) {
- return true;
- }
- return false;
- }
-
- @Override
- public String toString() {
- StringBuilder result = new StringBuilder();
- List value = this.getValue();
- if(value != null) {
- int size = this.size();
- for(int i=0; i> void setValue(E number) {
this.setValue(number.toString());
}
+ /**
+ * It puts the provided number in the BigNumber variable
+ * @param number BigNumber number
+ * @since v0.1.0
+ */
public void setValue(BigNumber number) {
this.setValue(number.getValue());
}
+ /**
+ * No character accept '0' to '9', '.' and '-' is allowed.
+ * No two '.' are allowed.
+ * Spaces (' ' or " ") in the provided String are removed before setting the value so no problem with spaces.
+ * '-' is allowed only as first character
+ * @param number
+ */
public void setValue(List number) {
StringBuilder chars = new StringBuilder();
int size = number.size();
@@ -216,6 +172,11 @@ public void setValue(List number) {
this.setValue(chars.toString());
}
+ /**
+ * It puts the provided number in the BigNumber variable
+ * @param value As String which may contain only numbers 0 to 9, a leading -ve sign '-' and a decimal point
+ * @since August 6, 2013, v0.1.0
+ */
public void setValue(String value) {
String methodName = BigNumber.SET_VALUE;
this.resetValue();
@@ -264,67 +225,71 @@ public void setValue(String value) {
//================================ Mathematical ====================================
- public void add(BigNumber number) {
- if(!this.isFractional() && !number.isFractional()) {
+ public BigNumber add(BigNumber number) {
+ BigNumber result = new BigNumber(this);
+ if(!result.isFractional() && !number.isFractional()) {
//Both are non-fractional
- this.setBigInteger(this.getBigInteger().add(number.getBigInteger()));
+ result.setBigInteger(result.getBigInteger().add(number.getBigInteger()));
} else {
- this.makeFractional();
- number.makeFractional();
- this.setBigDecimal(this.getBigDecimal().add(number.getBigDecimal()));
- number.consolidate();
+ result = result.makeFractional();
+ number = number.makeFractional();
+ result.setBigDecimal(result.getBigDecimal().add(number.getBigDecimal()));
+ // number = number.consolidate();
}
- this.consolidate();
+ return result.consolidate();
}
- public void multiply(BigNumber number) {
-
+ @Override
+ public BigNumber multiply(BigNumber number) {
+ BigNumber result = new BigNumber();
if(!this.isFractional() && !number.isFractional()) {
//Both are non-fractional
- this.setBigInteger(this.getBigInteger().multiply(number.getBigInteger()));
+ result.setBigInteger(this.getBigInteger().multiply(number.getBigInteger()));
} else {
- this.makeFractional();
- number.makeFractional();
- this.setBigDecimal(this.getBigDecimal().multiply(number.getBigDecimal()));
- number.consolidate();
+ result = this.makeFractional();
+ number = number.makeFractional();
+ result.setBigDecimal(result.getBigDecimal().multiply(number.getBigDecimal()));
+ // number.consolidate();
}
- this.consolidate();
+ return result.consolidate();
}
- public void subtract(BigNumber number) {
- if(!this.isFractional() && !number.isFractional()) {
+ @Override
+ public BigNumber subtract(BigNumber number) {
+ BigNumber result = new BigNumber();
+ if(!this.isFractional() && !number.isFractional()) {
//Both are non-fractional
- this.setBigInteger(this.getBigInteger().subtract(number.getBigInteger()));
+ result.setBigInteger(this.getBigInteger().subtract(number.getBigInteger()));
} else {
- this.makeFractional();
- number.makeFractional();
- this.setBigDecimal(this.getBigDecimal().subtract(number.getBigDecimal()));
- number.consolidate();
+ result = this.makeFractional();
+ number = number.makeFractional();
+ result.setBigDecimal(result.getBigDecimal().subtract(number.getBigDecimal()));
+ // number = number.consolidate();
}
- this.consolidate();
+ return result.consolidate();
}
- public void divide(BigNumber number) {
+ @Override
+ public BigNumber divide(BigNumber number) {
if(number.isZero()) {
throw new ArithmeticException();
}
- this.makeFractional();
- number.makeFractional();
+ BigNumber result = new BigNumber(this.makeFractional());
+ number = number.makeFractional();
try {
- this.setBigDecimal(this.getBigDecimal().divide(number.getBigDecimal()));
+ result.setBigDecimal(result.getBigDecimal().divide(number.getBigDecimal()));
} catch(ArithmeticException e) {
- this.consolidate();
- number.consolidate();
- if(!this.isFractional() && !number.isFractional()) {
- this.getBigInteger().divide(number.getBigInteger());
+ result = result.consolidate();
+ number = number.consolidate();
+ if(!result.isFractional() && !number.isFractional()) {
+ result.setBigInteger(result.getBigInteger().divide(number.getBigInteger()));
} else {
throw e;
}
}
-
- this.consolidate();
- number.consolidate();
+
+ return result.consolidate();
}
public BigNumber[] divideAndRemainder(BigNumber number) {
@@ -350,31 +315,29 @@ public BigNumber[] divideAndRemainder(BigNumber number) {
return result;
}
- public void absolute() {
- /*if(this.isFractional()) {
- this.setBigDecimal(this.getBigDecimal().abs());
- this.();
- } else {
- this.setBigInteger(this.getBigInteger().abs());
- this.();
- }*/
+ @Override
+ public BigNumber absolute() {
if(this.isNegative()) {
- this.getValue().remove(0);
- this.syncFromValue();
- }
+ BigNumber result = new BigNumber(this);
+ result.getValue().remove(0);
+ result.syncFromValue();
+ return result;
+ } else {
+ return this;
+ }
}
- public void modulus(BigNumber denominator) {
- this.setValue((this.divideAndRemainder(denominator))[1]);
- this.syncFromValue();
- this.absolute();
+ @Override
+ public BigNumber modulus(BigNumber denominator) {
+ return new BigNumber(this.divideAndRemainder(denominator)[1]).absolute();
}
- public void pow(int power) {
- if(this.isFractional()) {
- this.setBigDecimal(this.getBigDecimal().pow(power));
+ @Override
+ public BigNumber pow(int power) {
+ if(this.isFractional()) {
+ return new BigNumber(this.getBigDecimal().pow(power));
} else {
- this.setBigInteger(this.getBigInteger().pow(power));
+ return new BigNumber(this.getBigInteger().pow(power));
}
}
@@ -502,10 +465,12 @@ public Double toDouble() {
return result;
}
+ @Override
public Integer size() {
return this.getValue().size();
}
+ @Override
public String getValueBetween(int startIndex, int endIndex) {
if(startIndex > this.size() || endIndex > this.size())
throw new ArrayIndexOutOfBoundsException();
@@ -522,143 +487,159 @@ else if(startIndex > endIndex) {
return result.toString();
}
+ @Override
public String getValueTill(int index) {
return this.getValueBetween(0, index);
}
+ @Override
public Character charAt(int index) {
return this.getValue().get(index);
}
- public void convertToInteger() {
+ @Override
+ public BigNumber convertToInteger() {
if(!this.isFractional())
- return;
+ return this;
else {
- for(int i=this.locationOfDecimal(); i=this.size()) {
- return;
+ return this;
}
if((this.charAt(i) < GlobalConstants.FIVE) || (this.charAt(i) == GlobalConstants.FIVE && this.charAt(j)%2 == 0)) {
- return;
+ return this;
}
- for(;this.charAt(j) == GlobalConstants.NINE; j--) {
- this.modify(j, GlobalConstants.ZERO);
+ BigNumber result = new BigNumber(this);
+ for(;result.charAt(j) == GlobalConstants.NINE; j--) {
+ result.setValue(result.modify(j, GlobalConstants.ZERO));
}
- this.modify(j, this.charAt(j) + 1 - GlobalConstants.ZERO);
+ result.setValue(result.modify(j, result.charAt(j) + 1 - GlobalConstants.ZERO));
- while(i9 || newDigit<0) {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
}
- this.getValue().add(index, (char)(newDigit + GlobalConstants.ZERO));
- this.getValue().remove(index+1);
+ BigNumber result = new BigNumber(this);
+ result.getValue().add(index, (char)(newDigit + GlobalConstants.ZERO));
+ result.getValue().remove(index+1);
+ return result;
}
- public void modify(int index, char newDigit) {
+ @Override
+ public BigNumber modify(int index, char newDigit) {
String methodName = BigNumber.MODIFY;
if(index == 0) {
this.getValue().remove(0);
- this.putAtFirst(newDigit);
- return;
+ return this.putAtFirst(newDigit);
}
-
+
+ BigNumber result = new BigNumber(this);
if(newDigit == GlobalConstants.DECIMAL_POINT) {
- if(!this.isFractional()) {
- this.getValue().add(index, newDigit);
- this.getValue().remove(index+1);
- } else if(this.getValue().get(index) == GlobalConstants.DECIMAL_POINT) {
- return;
+ if(!result.isFractional()) {
+ result.getValue().add(index, newDigit);
+ result.getValue().remove(index+1);
+ } else if(result.getValue().get(index) == GlobalConstants.DECIMAL_POINT) {
+ return result;
} else {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
}
} else if(newDigit>=GlobalConstants.ZERO && newDigit<=GlobalConstants.NINE) {
- this.modify(index, newDigit-GlobalConstants.ZERO);
+ return result.modify(index, newDigit-GlobalConstants.ZERO);
} else {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
}
+ return result;
}
- public void insert(int index, char digit) {
+ @Override
+ public BigNumber insert(int index, char digit) {
String methodName = BigNumber.INSERT;
if(index == 0) {
- this.putAtFirst(digit);
- return;
+ return this.putAtFirst(digit);
}
-
+
+ BigNumber result = new BigNumber(this);
if(digit == GlobalConstants.DECIMAL_POINT) {
- if(!this.isFractional()) {
- this.getValue().add(index, digit);
- this.syncFromValue();
- } else if(this.getValue().get(index) == GlobalConstants.DECIMAL_POINT) {
- return;
+ if(!result.isFractional()) {
+ result.getValue().add(index, digit);
+ result.syncFromValue();
+ } else if(result.getValue().get(index) == GlobalConstants.DECIMAL_POINT) {
+ return result;
} else {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
}
} else if(digit>=GlobalConstants.ZERO && digit<=GlobalConstants.NINE) {
- this.insert(index, digit-GlobalConstants.ZERO);
+ return result.insert(index, digit-GlobalConstants.ZERO);
} else {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
}
+ return result;
}
- public void insert(int index, int digit) {
+ @Override
+ public BigNumber insert(int index, int digit) {
String methodName = BigNumber.INSERT;
if(digit>9 || digit<0) {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
}
- this.getValue().add(index, (char)(digit + GlobalConstants.ZERO));
- this.syncFromValue();
+ BigNumber result = new BigNumber(this);
+ result.getValue().add(index, (char)(digit + GlobalConstants.ZERO));
+ result.syncFromValue();
+ return result;
}
- public void putAtFirst(int digit) {
+ @Override
+ public BigNumber putAtFirst(int digit) {
String methodName = BigNumber.PUT_AT_FIRST;
if(digit>9 || digit<0) {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.NEW_DIGIT_INCOMPATIBLE_MSG.getValue());
@@ -666,66 +647,66 @@ public void putAtFirst(int digit) {
List dig = new ArrayList();
dig.add((char) (digit + GlobalConstants.ZERO));
dig.addAll(this.getValue());
- this.setValue(dig);
+ return new BigNumber(dig);
}
- public void putAtFirst(char character) {
+ @Override
+ public BigNumber putAtFirst(char character) {
List dig = new ArrayList();
dig.add(character);
dig.addAll(this.getValue());
- this.setValue(dig);
+ return new BigNumber(dig);
}
- public void reverse() {
+ @Override
+ public BigNumber reverse() {
int size = this.size();
- int limit = 0;
- if(this.isNegative()) {
- limit = 1;
- }
-
+ int limit = this.isNegative()? 1 : 0;
+
+ BigNumber result = new BigNumber(this);
for(int count=0; count<=size-limit-1; count++) {
- if(this.charAt(size-1) == GlobalConstants.DECIMAL_POINT) {
- this.getValue().add(count+limit, GlobalConstants.DECIMAL_POINT);
- this.getValue().remove(size);
+ if(result.charAt(size - 1) == GlobalConstants.DECIMAL_POINT) {
+ result.getValue().add(count+limit, GlobalConstants.DECIMAL_POINT);
+ result.getValue().remove(size);
continue;
}
- this.insert(count+limit, this.charAt(size-1));
- this.getValue().remove(size);
+ result.insert(count + limit, result.charAt(size - 1));
+ result.getValue().remove(size);
}
- this.syncFromValue();
+ result.syncFromValue();
+ return result;
}
- public void consolidate() {
+ @Override
+ public BigNumber consolidate() {
// Remember -ve and fractions
+ BigNumber result = new BigNumber(this);
- int startIndex = 0;
- if(isNegative()) {
- startIndex = 1;
- }
-
- for(;this.charAt(startIndex) == GlobalConstants.ZERO; this.getValue().remove(startIndex));
+ for(int startIndex = result.isNegative()?1:0;result.charAt(startIndex) == GlobalConstants.ZERO; result.getValue().remove(startIndex));
- if(this.isFractional()) {
+ if(result.isFractional()) {
// Consolidate from other end as well
- for(int i=this.size()-1; this.charAt(i) == GlobalConstants.ZERO; i=this.size()-1) {
- this.getValue().remove(i);
+ for(int i=result.size()-1; result.charAt(i) == GlobalConstants.ZERO; i=result.size()-1) {
+ result.getValue().remove(i);
}
}
- if(this.charAt(this.size()-1) == GlobalConstants.DECIMAL_POINT) {
- this.getValue().remove(this.size()-1);
+ if(result.charAt(result.size() - 1) == GlobalConstants.DECIMAL_POINT) {
+ result.getValue().remove(result.size()-1);
}
- if(this.size() == 0) {
- this.setValue(0);
+ if(result.size() == 0) {
+ result.setValue(0);
}
- this.syncFromValue();
+ result.syncFromValue();
+ return result;
}
- public void append(BigNumber number) {
+ @Override
+ public BigNumber append(BigNumber number) {
String methodName = BigNumber.APPEND;
if(this.isFractional() && number.isFractional()) {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.TWO_DECIMAL_POINTS_MSG.getValue());
@@ -733,31 +714,38 @@ public void append(BigNumber number) {
if(number.isNegative()) {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.SECOND_NUMBER_NEGATIVE_MSG.getValue());
}
- this.getValue().addAll(number.getValue());
- this.syncFromValue();
+
+ BigNumber result = new BigNumber();
+ result.setValue(this.toString() + number.toString());
+ result.syncFromValue();
+ return result;
}
- public void append (int number) {
+ @Override
+ public BigNumber append (int number) {
String methodName = BigNumber.APPEND;
- if(number < 0) {
+ if(number < 0) {
throw new IllegalArgumentException(BigNumber.FROM + methodName + ErrorMessages.INCOMPATIBLE_VALUE_MSG.getValue());
}
- ArrayList num = new ArrayList();
- for(; number>10; number /= 10) {
- num.add(0, (char)(number%10 + GlobalConstants.ZERO));
- }
- this.getValue().addAll(num);
- this.syncFromValue();
+
+ BigNumber result = new BigNumber();
+ result.setValue(this.toString() + ((Integer) number).toString());
+ result.syncFromValue();
+ return result;
}
- public void makeFractional() {
+ @Override
+ public BigNumber makeFractional() {
if(this.isFractional()) {
- return;
+ return this;
}
- this.setValue(this.toString() + GlobalConstants.DECIMAL_POINT + GlobalConstants.ZERO_STR + GlobalConstants.ZERO_STR);
+ BigNumber result = new BigNumber(this);
+ result.setValue(result.toString() + GlobalConstants.DECIMAL_POINT + GlobalConstants.ZERO_STR + GlobalConstants.ZERO_STR);
+ return result;
}
+ @Override
public BigNumber factorial() {
if((this.compareTo(new BigNumber()) == 0) || (this.compareTo(UNITY) == 0)) {
return UNITY;
@@ -767,7 +755,7 @@ public BigNumber factorial() {
BigNumber i = new BigNumber();
result.setValue(this);
- for(i.setValue(subtract(this, UNITY)); i.compareTo(UNITY) != 0; i.setValue(subtract(i, UNITY))) {
+ for(i.setValue(BigNumberUtils.subtract(this, BigNumber.UNITY)); i.compareTo(UNITY) != 0; i.setValue(BigNumberUtils.subtract(i, UNITY))) {
result.multiply(i);
}
@@ -778,30 +766,30 @@ public BigNumber factorial() {
public String toJSON() {
StringBuilder bigNum = new StringBuilder(GlobalConstants.LEFT_BRACE);
+ bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isZero" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + this.isZero());
if(this.isZero()) {
- bigNum.append("\"value\"" + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.ZERO_STR + GlobalConstants.DOUBLE_QUOTE);
- bigNum.append(GlobalConstants.COMMA + "\"isZero\"" + GlobalConstants.COLON + true);
- } else {
bigNum.append(GlobalConstants.DOUBLE_QUOTE + "value" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE +
- StringUtils.combine(this.getValue(), GlobalConstants.BLANK) + GlobalConstants.DOUBLE_QUOTE);
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isZero" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + false);
- }
-
- if(this.isFractional()) {
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isFractional" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + true);
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "bigDecimal" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE + this.getBigDecimal().toString() + GlobalConstants.DOUBLE_QUOTE);
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "locationOfDecimal" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE + this.locationOfDecimal() + GlobalConstants.DOUBLE_QUOTE);
+ GlobalConstants.ZERO_STR + GlobalConstants.DOUBLE_QUOTE);
} else {
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isFractional" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + false);
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "bigInteger" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE + this.getBigInteger().toString() + GlobalConstants.DOUBLE_QUOTE);
+ this.syncFromValue();
}
- if(this.isNegative()) {
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isNegative" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + true);
+ bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isFractional" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + this.isFractional());
+ if(this.isFractional()) {
+ bigNum.append(GlobalConstants.DOUBLE_QUOTE + "value" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE +
+ this.getBigDecimal().toString() + GlobalConstants.DOUBLE_QUOTE);
+ bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "bigDecimal" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON +
+ GlobalConstants.DOUBLE_QUOTE + this.getBigDecimal().toString() + GlobalConstants.DOUBLE_QUOTE);
+ bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "locationOfDecimal" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON +
+ GlobalConstants.DOUBLE_QUOTE + this.locationOfDecimal() + GlobalConstants.DOUBLE_QUOTE);
} else {
- bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isNegative" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + false);
+ bigNum.append(GlobalConstants.DOUBLE_QUOTE + "value" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + GlobalConstants.DOUBLE_QUOTE +
+ this.getBigInteger().toString() + GlobalConstants.DOUBLE_QUOTE);
+ bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "bigInteger" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON +
+ GlobalConstants.DOUBLE_QUOTE + this.getBigInteger().toString() + GlobalConstants.DOUBLE_QUOTE);
}
+ bigNum.append(GlobalConstants.COMMA + GlobalConstants.DOUBLE_QUOTE + "isNegative" + GlobalConstants.DOUBLE_QUOTE + GlobalConstants.COLON + this.isNegative());
bigNum.append(GlobalConstants.RIGHT_BRACE);
return bigNum.toString();
@@ -829,11 +817,8 @@ protected void syncFromValue() {
this.setZero(true);
this.setLocationOfDecimal(-1);
this.setFractional(false);
-
- int i=0;
- if(this.isNegative()) {
- i=1;
- }
+
+ int i = (this.isNegative()?1:0);
for(; iNo character accept '0' to '9', '.' and '-' is allowed.
- * No two '.' are allowed.
- * Spaces (' ' or " ") in the provided String are removed before setting the value so no problem with spaces.
- * '-' is allowed only as first character
- * @param value
- */
-
- /**
- * @return position of decimal point in the fractional number,
- * -1 if it's not a fractional number
- * @author Nishi Inc.
- * @since v0.1.0
- */
-
- /**
- *
- * @return true if the number is a fraction else false
- * @author Nishi Inc.
- * @since v0.1.0
- */
-
- /**
- * @return true if the number is negative else false
- * @author Nishi Inc.
- * @since v0.1.0
- */
-
- /**
- * @return true if the number has value = 0 or "0" else false
- * @author Nishi Inc.
- * @since v0.1.0
- */
-
- /*==========================================================
- * Public Static Methods
- * =========================================================
- */
-
- /**
- * Returns remainder
- * @param numerator
- * @param denominator
- * @return A BigNumber type variable containing remainder of arithmetic division of the provided numerator by the provided denominator
- * @author Nishi Inc.
- * @since v1.1.0
- */
- public static BigNumber modulus(BigNumber numerator, BigNumber denominator) {
- BigNumber result = new BigNumber();
- result.setValue(numerator.divideAndRemainder(denominator)[1]);
- result.absolute();
- return result;
- }
-
- /**
- * Same as modulus()
- * @param numerator
- * @param denominator
- * @return A BigNumber type variable containing remainder of arithmetic division of the provided numerator by the provided denominator
- * @author Nishi Inc.
- * @since v1.1.0
- */
- public static BigNumber mod(BigNumber numerator, BigNumber denominator) {
- return modulus(numerator, denominator);
- }
-
- /**
- * Same as add() method
- * @author Nishi Inc.
- * @since v1.1.0
- * @param numbers BigNumber numbers
- * @return A BigNumber type variable containing result of arithmetic addition of the provided numbers
- */
- public static BigNumber sum(BigNumber... numbers) {
- return BigNumber.add(numbers);
- }
-
- /**
- * Same as subtract()
- * @author Nishi Inc.
- * @since v1.1.0
- * @param firstNumber
- * @param secondNumber
- * @return A BigNumber type variable containing firstNumber - secondNumber
- */
- public static BigNumber sub(BigNumber firstNumber, BigNumber secondNumber) {
- return BigNumber.subtract(firstNumber, secondNumber);
- }
-
- /**
- * Same as absolute()
- * @param number
- * @return A BigNumber type variable containing absolute value of the given BigNumber number
- * @author Nishi Inc.
- * @since v1.1.0
- */
- public static BigNumber abs(BigNumber number) {
- return BigNumber.absolute(number);
- }
-
- /**
- * @param number
- * @param power
- * @return A BigNumber containing number^power
- * @since v1.0.0
- * @author Nishi Incorporation
- */
- public static BigNumber pow(BigNumber number, int power) {
- BigNumber result = new BigNumber(number);
- result.pow(power);
- return result;
- }
-
- /**
- * @author Nishi Incorporation
- * @since v1.1.0
- * @param number
- * @return A BigNumber containing value of factorial(given number)
- */
- public static BigNumber factorialOf(BigNumber number) {
- BigNumber result = new BigNumber(number);
- BigNumber i = new BigNumber();
-
- for(i.setValue(sub(number, UNITY)); !i.isZero(); i.setValue(sub(i, UNITY))) {
- result.multiply(i);
- }
-
- return result;
- }
-
-
-
- //=============================== Utility Methods ==========================================
-
- /**
- * Works similar to BigNumber.reverse()
- * @param number A BigNumber number
- * @return A BigNumber number with its digits in reverse order
- * @author Nishi Inc.
- * @since v1.2.0
- */
- public static BigNumber reverse(BigNumber number) {
- BigNumber result = new BigNumber(number);
- result.reverse();
- return result;
- }
-
- /**
- * Works similar to BigNumber.reverse()
- * @param numbers
- * @return An array of BigNumber numbers containing BigNumber numbers which have their digits in reverse order
- * @author Nishi Inc.
- * @since v1.0.0
- */
- public static BigNumber[] reverse(BigNumber... numbers) {
- int size = numbers.length;
- BigNumber[] result = new BigNumber[size];
- for(int i=0; i
* roundingMode is HALF_EVEN
* precision is always MathContext.UNLIMITED.getPrecision()
- * @author aloks
+ * @author Nishi Inc
* Date: 21/11/13
* Time: 3:27 PM
* @since 2.0.0
@@ -17,9 +17,9 @@ public class BigNumberMathContext {
private static final int DEFAULT_NUMBER_OF_DIGITS_AFTER_DECIMAL = 6;
private static final RoundingMode DEFAULT_ROUNDING_MODE = RoundingMode.HALF_EVEN;
+ private int precision;
private RoundingMode roundingMode;
private Integer numberOfDigitsAfterDecimal;
- private int precision;
/**
* Default constructor
@@ -50,6 +50,14 @@ public BigNumberMathContext(MathContext mathContext) {
this.setNumberOfDigitsAfterDecimal(BigNumberMathContext.DEFAULT_NUMBER_OF_DIGITS_AFTER_DECIMAL);
}
+ /**
+ * @param bigNumberMathContext To build MathContext object
+ * @return A MathContext object constructed with the values of given bigNumberMathContext
+ */
+ public static MathContext getMathContextObject(BigNumberMathContext bigNumberMathContext) {
+ return new MathContext(bigNumberMathContext.getPrecision(), bigNumberMathContext.getRoundingMode());
+ }
+
public Integer getNumberOfDigitsAfterDecimal() {
return numberOfDigitsAfterDecimal;
}
diff --git a/src/main/java/org/bigNumber/models/BigNumberParent.java b/src/main/java/org/bigNumber/models/BigNumberParent.java
index 569b65e..4dc3c31 100644
--- a/src/main/java/org/bigNumber/models/BigNumberParent.java
+++ b/src/main/java/org/bigNumber/models/BigNumberParent.java
@@ -16,6 +16,8 @@
import java.util.List;
import org.bigNumber.common.interfaces.NonStaticMethods;
+import org.nishi.helper.GlobalConstants;
+import org.nishi.helper.StringUtils;
/**
* Parent class for BigNumber
@@ -60,4 +62,129 @@ protected void setBigDecimal(BigDecimal bigDecimal) {
*/
protected abstract void syncFromDecimal();
+ /**
+ * Sets all the fields of BigNumber as per the value of BigInteger field
+ */
+ protected abstract void syncFromInteger();
+
+ //================================ INHERITED METHODS ===============================
+
+ @Override
+ public int compareTo(BigNumber number) {
+ int result = 0;
+
+ if(!this.isFractional() && !number.isFractional()) {
+ //Both are non-fractional
+ result = this.getBigInteger().compareTo(number.getBigInteger());
+ } else {
+ this.makeFractional();
+ number.makeFractional();
+ result = this.getBigDecimal().compareTo(number.getBigDecimal());
+ this.consolidate();
+ number.consolidate();
+ }
+ return result;
+ }
+
+ @Override
+ public int hashCode() {
+ final int PRIME = 13;
+ int hash = 0;
+ List val = this.getValue();
+ for(int digit : val) {
+ digit -= GlobalConstants.ASCII_ZERO;
+ if(digit>=0 && digit<=9) {
+ hash = hash * PRIME + digit;
+ }
+ }
+ return hash;
+ }
+
+ @Override
+ public boolean equals(Object number) {
+ if(number instanceof BigNumber && this.compareTo((BigNumber) number) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return StringUtils.combine(this.getValue(), GlobalConstants.BLANK);
+ }
+
+
+ //==================================================================================
+
+ /*==================================================================================
+ * GETTERS/SETTERS
+ *==================================================================================
+ */
+
+ /**
+ * @return true if the number is negative else false
+ * @since v0.1.0
+ */
+ public boolean isNegative() {
+ return isNegative;
+ }
+
+ protected void setNegative(boolean isNegative) {
+ this.isNegative = isNegative;
+ }
+
+ /**
+ * @return true if the number is a fraction else false
+ * @since v0.1.0
+ */
+ public boolean isFractional() {
+ return isFractional;
+ }
+
+ protected void setFractional(boolean isFraction) {
+ this.isFractional = isFraction;
+ if(!isFraction) {
+ this.setLocationOfDecimal(-1);
+ }
+ }
+
+ /**
+ * @return position of decimal point in the fractional number,
+ * -1 if it's not a fractional number
+ * @since v0.1.0
+ */
+ public int locationOfDecimal() {
+ if(!this.isFractional())
+ return -1;
+ return locationOfDecimal;
+ }
+
+ protected void setLocationOfDecimal(Integer locationOfDecimal) {
+ this.locationOfDecimal = locationOfDecimal;
+ }
+
+ protected BigInteger getBigInteger() {
+ if(bigInteger == null) {
+ this.setBigInteger(new BigInteger(GlobalConstants.ZERO_STR));
+ }
+ return bigInteger;
+ }
+
+ protected void setBigInteger(BigInteger bigInteger) {
+ this.bigInteger = bigInteger;
+ this.syncFromInteger();
+ }
+
+ /**
+ * @return true if the number has value = 0 or "0" else false
+ * @since v0.1.0
+ */
+ public boolean isZero() {
+ return isZero;
+ }
+
+ protected void setZero(boolean isZero) {
+ this.isZero = isZero;
+ }
+
}
diff --git a/src/main/java/org/bigNumber/models/BigNumberPool.java b/src/main/java/org/bigNumber/models/BigNumberPool.java
index 01d7f80..dfdb8cc 100644
--- a/src/main/java/org/bigNumber/models/BigNumberPool.java
+++ b/src/main/java/org/bigNumber/models/BigNumberPool.java
@@ -18,6 +18,7 @@
import java.util.TreeMap;
import org.bigNumber.common.interfaces.BigNumberFactory;
+import org.nishi.helper.Validate;
/**
* A BigNumberPool which keeps BigNumbers and handles allocation
@@ -40,20 +41,17 @@ public class BigNumberPool implements BigNumberFactory {
private static int MAX_CAP_INCREASE_MULTIPLIER = 10;
private static int MAX_CAP_TOLERANCE_MULTIPLIER = 5;
- private Integer key;
- private Integer limit;
- private Integer capacity;
- private Integer loadFactor;
-
- private List free;
- private List allotted;
+ private Integer key;
+ private Integer limit;
+ private Integer capacity;
+ private Integer loadFactor;
+ private Integer noOfAllottedBigNumbers = 0;
private TreeMap hold;
/**
* Constructs a pool with DEFAULT_CAPACITY and DEFAULT_LOAD_FACOTR
* However, this pool can intelligently modify its capacity and loadFactor in runtime
- * @author Nishi Inc.
* @since v1.0.0
*/
public BigNumberPool(){}
@@ -74,17 +72,9 @@ public BigNumberPool(int capacity, int loadFactor) {
@Override
public BigNumber getBigNumber() {
- if(this.getFree().isEmpty()) {
- this.getFree().add(new BigNumber());
- return getBigNumber();
- }
- for(BigNumber bignum : this.getFree()) {
- this.getFree().remove(bignum);
- this.getAllotted().add(bignum);
- this.updateIndex();
- return bignum;
- }
- return null;
+ this.setNoOfAllottedBigNumbers(this.getNoOfAllottedBigNumbers() + 1);
+ this.updateIndex();
+ return new BigNumber();
}
@Override
@@ -92,7 +82,7 @@ public BigNumber getBigNumber(Integer key) {
if(this.getHold().containsKey(key)) {
BigNumber result = this.getHold().get(key);
this.getHold().remove(key);
- this.getAllotted().add(result);
+ this.setNoOfAllottedBigNumbers(this.getNoOfAllottedBigNumbers() + 1);
return result;
}
return this.getBigNumber();
@@ -113,26 +103,19 @@ public void destroy(BigNumber... bignums) {
* @param bignum BigNumber to get rid of
*/
private void destroy(BigNumber bignum) {
- boolean removedFromAllotted = this.getAllotted().remove(bignum);
- boolean presentInHold;
- if(removedFromAllotted) {
- this.getFree().add(bignum);
- this.managePool();
- return;
- }
- presentInHold = this.getHold().containsValue(bignum);
- if(presentInHold) {
+ if(this.getHold().containsValue(bignum)) {
Integer key = this.getKeyFromHold(bignum);
this.getHold().remove(key);
- this.getFree().add(bignum);
- this.managePool();
+ } else {
+ this.setNoOfAllottedBigNumbers(this.getNoOfAllottedBigNumbers()-1);
}
+ this.managePool();
}
@Override
public Integer hold(BigNumber bignum) {
Integer key = nextKey();
- this.getAllotted().remove(bignum);
+ this.setNoOfAllottedBigNumbers(this.getNoOfAllottedBigNumbers()-1);
this.getHold().put(key, bignum);
return key;
}
@@ -143,21 +126,12 @@ public Integer hold(BigNumber bignum) {
*/
private void managePool() {
this.updateIndex();
- for(;this.getFree().size() > this.getCapacity();) {
- for(BigNumber bignum : this.getFree()) {
- this.getFree().remove(bignum);
- break;
- }
- }
-
- int sizeOfAllotted = this.getAllotted().size() + this.getHold().size();
- for(;(this.getFree().size()+sizeOfAllotted > this.getLimit()) && (!this.getFree().isEmpty());) {
- for(BigNumber bignum : this.getFree()) {
- this.getFree().remove(bignum);
- break;
- }
+
+ /*int sizeOfAllotted = this.getNoOfAllottedBigNumbers() + this.getHold().size();
+ for(;(this.getFree()+sizeOfAllotted > this.getLimit());) {
+
}
- this.updateIndex();
+ this.updateIndex(); */
}
/**
@@ -167,8 +141,8 @@ private void managePool() {
* Frequent calls to this method ensure proper value of index variable
*/
private void updateIndex() {
- int freeSize = this.getFree().size();
- int allottedSize = this.getAllotted().size() + this.getHold().size();
+ int freeSize = this.getFree();
+ int allottedSize = this.getNoOfAllottedBigNumbers() + this.getHold().size();
if(allottedSize > this.getCapacity()) {
if((freeSize+allottedSize)*BigNumberPool.MAX_CAP_TOLERANCE_MULTIPLIER > MAX_CAP) {
@@ -211,21 +185,8 @@ private Integer getKeyFromHold(BigNumber bignum) {
return null;
}
- private List getFree() {
- if(free == null) {
- free = new LinkedList();
- }
- return free;
- }
-
- private List getAllotted() {
- if(allotted == null) {
- allotted = new LinkedList();
- }
- return allotted;
- }
- /**
+ /**
* loadFactor cannot be equal to 0
* @return loadFactor of BigNumber pool as an int (as percentage: a number between 1 to 100)
*/
@@ -289,5 +250,16 @@ private TreeMap getHold() {
private Integer nextKey() {
return ++key;
}
-
+
+ private Integer getFree() {
+ return this.getCapacity() - (this.getNoOfAllottedBigNumbers() + this.getHold().size()) ;
+ }
+
+ private Integer getNoOfAllottedBigNumbers() {
+ return this.noOfAllottedBigNumbers;
+ }
+
+ private void setNoOfAllottedBigNumbers(Integer noOfAllottedBigNumbers) {
+ this.noOfAllottedBigNumbers = noOfAllottedBigNumbers;
+ }
}
diff --git a/src/test/java/BigTest.java b/src/test/java/BigTest.java
index 836b00a..6abc465 100644
--- a/src/test/java/BigTest.java
+++ b/src/test/java/BigTest.java
@@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.List;
+import org.bigNumber.common.services.BigNumberUtils;
import org.nishi.helper.GlobalConstants;
import junit.framework.TestCase;
@@ -42,8 +43,9 @@ private BigNumberPool getBigPool() {
public void testFactorialOf() {
BigNumber no = this.getBigPool().getBigNumber();
no.setValue("1");
- BigNumber correctResult = new BigNumber("1");
- BigNumber result = BigNumber.factorialOf(no);
+ BigNumber correctResult = this.getBigPool().getBigNumber();
+ correctResult.setValue("1");
+ BigNumber result = BigNumberUtils.factorialOf(no);
assertTrue(result.equals(correctResult));
}
@@ -65,7 +67,7 @@ public void testRoundOff() {
no.setValue("-34.2896");
BigNumberMathContext mathContext = new BigNumberMathContext();
mathContext.setNumberOfDigitsAfterDecimal(3);
- no.roundOff(mathContext);
+ no = no.roundOff(mathContext);
assertTrue(no.toString().contentEquals("-34.290"));
this.getBigPool().destroy(no);
}
@@ -87,7 +89,7 @@ public void testPow() {
BigNumber num = this.getBigPool().getBigNumber();
BigNumber result = this.getBigPool().getBigNumber();
num.setValue("34.1");
- result = BigNumber.pow(num, 2);
+ result = BigNumberUtils.pow(num, 2);
//num.pow(2);
assertTrue(result.toString().contentEquals("1162.81"));
this.getBigPool().destroy(num,result);
@@ -108,8 +110,8 @@ public void testAbsolute() {
BigNumber num2 = this.getBigPool().getBigNumber();
num1.setValue("1111");
num2.setValue("-2.0");
- num1.absolute();
- num2.absolute();
+ num1 = num1.absolute();
+ num2 = num2.absolute();
assertTrue(num1.toString().contentEquals("1111"));
assertTrue(num2.toString().contentEquals("2.0"));
this.getBigPool().destroy(num1, num2);
@@ -120,7 +122,7 @@ public void testModulus() {
BigNumber num2 = this.getBigPool().getBigNumber();
num1.setValue("-34");
num2.setValue("3");
- num1.modulus(num2);
+ num1 = num1.modulus(num2);
assertTrue(num1.toString().contentEquals("1"));
this.getBigPool().destroy(num1, num2);
}
@@ -137,7 +139,7 @@ public void testSum() {
num[3].setValue("3");
num[4].setValue("3");
num[2].setValue("3");
- result = BigNumber.sum(num);
+ result = BigNumberUtils.sum(num);
assertTrue(result.toString().contentEquals("8.0123"));
this.getBigPool().destroy(num);
}
@@ -154,7 +156,7 @@ public void testMultiplyStatic() {
num[3].setValue("3");
num[4].setValue("3");
num[2].setValue("3");
- result = BigNumber.multiply(num);
+ result = BigNumberUtils.multiply(num);
assertTrue(result.toString().contentEquals("-30486.49461"));
this.getBigPool().destroy(num);
}
@@ -166,7 +168,7 @@ public void testDivideStatic() {
num1.setValue("33.1123");
num2.setValue("4");
- result = BigNumber.divide(num1, num2);
+ result = BigNumberUtils.divide(num1, num2);
assertTrue(result.toString().contentEquals("8.278075"));
this.getBigPool().destroy(num1, num2);
}
@@ -178,7 +180,7 @@ public void testSub() {
num1.setValue("33.1123");
num2.setValue("4");
- result = BigNumber.sub(num1, num2);
+ result = BigNumberUtils.sub(num1, num2);
assertTrue(result.toString().contentEquals("29.1123"));
this.getBigPool().destroy(num1, num2);
}
@@ -206,7 +208,7 @@ public void testModify() {
BigNumber num1 = this.getBigPool().getBigNumber();
num1.setValue("33263547245296341");
- num1.modify(1, '.');
+ num1 = num1.modify(1, '.');
assertTrue(num1.toString().contentEquals("3.263547245296341"));
this.getBigPool().destroy(num1);
}
@@ -216,7 +218,7 @@ public void testAdd() {
BigNumber num2 = this.getBigPool().getBigNumber();
num1.setValue("11111111111111111");
num2.setValue("222222222222.22222");
- num1.add(num2);
+ num1 = num1.add(num2);
assertTrue(num1.toString().contentEquals("11111333333333333.22222"));
this.getBigPool().destroy(num1,num2);
}
@@ -230,7 +232,7 @@ public void testMultiply() {
} catch (Exception e) {
assertTrue(false);
}
- num1.multiply(num2);
+ num1 = num1.multiply(num2);
assertTrue(num1.toString().contentEquals("1000000000000000000000000000000000000000000000000"));
this.getBigPool().destroy(num1,num2);
}
@@ -238,13 +240,9 @@ public void testMultiply() {
public void testDivide() {
BigNumber num1 = this.getBigPool().getBigNumber();
BigNumber num2 = this.getBigPool().getBigNumber();
- try {
- num1.setValue("1000000000000000000000000");
- num2.setValue("1000000000000000000000000");
- } catch (Exception e) {
- assertTrue(false);
- }
- num1.divide(num2);
+ num1.setValue("1000000000000000000000000");
+ num2.setValue("1000000000000000000000000");
+ num1 = num1.divide(num2);
assertTrue(num1.toString().contentEquals("1"));
this.getBigPool().destroy(num1,num2);
}
@@ -291,7 +289,7 @@ public void testAppend() {
num1.setValue("-102");
num2.setValue("137");
- num1.append(num2);
+ num1 = num1.append(num2);
assertTrue(num1.toString().contentEquals("-102137"));
this.getBigPool().destroy(num1);
}