mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-13 10:25:20 +00:00
Format code.
This commit is contained in:
@@ -5,26 +5,6 @@ package org.nwapw.abacus.number;
|
||||
*/
|
||||
public class NaiveNumber implements NumberInterface {
|
||||
|
||||
/**
|
||||
* The value of this number.
|
||||
*/
|
||||
private double value;
|
||||
|
||||
/**
|
||||
* Creates a new NaiveNumber with the given string.
|
||||
* @param value the value, which will be parsed as a double.
|
||||
*/
|
||||
public NaiveNumber(String value) {
|
||||
this(Double.parseDouble(value));
|
||||
}
|
||||
/**
|
||||
* Creates a new NaiveNumber with the given value.
|
||||
* @param value the value to use.
|
||||
*/
|
||||
public NaiveNumber(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The number zero.
|
||||
*/
|
||||
@@ -33,6 +13,27 @@ public class NaiveNumber implements NumberInterface {
|
||||
* The number one.
|
||||
*/
|
||||
public static final NaiveNumber ONE = new NaiveNumber(1);
|
||||
/**
|
||||
* The value of this number.
|
||||
*/
|
||||
private double value;
|
||||
|
||||
/**
|
||||
* Creates a new NaiveNumber with the given string.
|
||||
*
|
||||
* @param value the value, which will be parsed as a double.
|
||||
*/
|
||||
public NaiveNumber(String value) {
|
||||
this(Double.parseDouble(value));
|
||||
}
|
||||
/**
|
||||
* Creates a new NaiveNumber with the given value.
|
||||
*
|
||||
* @param value the value to use.
|
||||
*/
|
||||
public NaiveNumber(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxPrecision() {
|
||||
@@ -41,22 +42,22 @@ public class NaiveNumber implements NumberInterface {
|
||||
|
||||
@Override
|
||||
public NumberInterface multiply(NumberInterface multiplier) {
|
||||
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
||||
return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface divide(NumberInterface divisor) {
|
||||
return new NaiveNumber(value / ((NaiveNumber)divisor).value);
|
||||
return new NaiveNumber(value / ((NaiveNumber) divisor).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface add(NumberInterface summand) {
|
||||
return new NaiveNumber(value + ((NaiveNumber)summand).value);
|
||||
return new NaiveNumber(value + ((NaiveNumber) summand).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
|
||||
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,16 +67,16 @@ public class NaiveNumber implements NumberInterface {
|
||||
|
||||
@Override
|
||||
public NumberInterface intPow(int exponent) {
|
||||
if(exponent == 0){
|
||||
if (exponent == 0) {
|
||||
return NaiveNumber.ONE;
|
||||
}
|
||||
boolean takeReciprocal = exponent < 0;
|
||||
exponent = Math.abs(exponent);
|
||||
NumberInterface power = this;
|
||||
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
||||
for (int currentExponent = 1; currentExponent < exponent; currentExponent++) {
|
||||
power = power.multiply(this);
|
||||
}
|
||||
if(takeReciprocal){
|
||||
if (takeReciprocal) {
|
||||
power = NaiveNumber.ONE.divide(power);
|
||||
}
|
||||
return power;
|
||||
@@ -94,14 +95,14 @@ public class NaiveNumber implements NumberInterface {
|
||||
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()) return this;
|
||||
else if(toClass == PreciseNumber.class){
|
||||
if (toClass == this.getClass()) return this;
|
||||
else if (toClass == PreciseNumber.class) {
|
||||
return new PreciseNumber(Double.toString(value));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
public String toString() {
|
||||
double shiftBy = Math.pow(10, 10);
|
||||
return Double.toString(Math.round(value * shiftBy) / shiftBy);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ public interface NumberInterface {
|
||||
|
||||
/**
|
||||
* The maximum precision to which this number operates.
|
||||
*
|
||||
* @return the precision.
|
||||
*/
|
||||
int getMaxPrecision();
|
||||
@@ -14,27 +15,34 @@ public interface NumberInterface {
|
||||
/**
|
||||
* Multiplies this number by another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param multiplier the multiplier
|
||||
* @return the result of the multiplication.
|
||||
*/
|
||||
NumberInterface multiply(NumberInterface multiplier);
|
||||
|
||||
/**
|
||||
* Divides this number by another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param divisor the divisor
|
||||
* @return the result of the division.
|
||||
*/
|
||||
NumberInterface divide(NumberInterface divisor);
|
||||
|
||||
/**
|
||||
* Adds this number to another, returning
|
||||
* a new number instance.
|
||||
*
|
||||
* @param summand the summand
|
||||
* @return the result of the summation.
|
||||
*/
|
||||
NumberInterface add(NumberInterface summand);
|
||||
|
||||
/**
|
||||
* Subtracts another number from this number,
|
||||
* a new number instance.
|
||||
*
|
||||
* @param subtrahend the subtrahend.
|
||||
* @return the result of the subtraction.
|
||||
*/
|
||||
@@ -43,12 +51,14 @@ public interface NumberInterface {
|
||||
/**
|
||||
* Returns a new instance of this number with
|
||||
* the sign flipped.
|
||||
*
|
||||
* @return the new instance.
|
||||
*/
|
||||
NumberInterface negate();
|
||||
|
||||
/**
|
||||
* Raises this number to an integer power.
|
||||
*
|
||||
* @param exponent the exponent to which to take the number.
|
||||
* @return the resulting value.
|
||||
*/
|
||||
@@ -56,6 +66,7 @@ public interface NumberInterface {
|
||||
|
||||
/**
|
||||
* Compares this number to another.
|
||||
*
|
||||
* @param number the number to compare to.
|
||||
* @return same as Integer.compare();
|
||||
*/
|
||||
@@ -63,12 +74,14 @@ public interface NumberInterface {
|
||||
|
||||
/**
|
||||
* Same as Math.signum().
|
||||
*
|
||||
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
|
||||
*/
|
||||
int signum();
|
||||
|
||||
/**
|
||||
* Promotes this class to another number class.
|
||||
*
|
||||
* @param toClass the class to promote to.
|
||||
* @return the resulting new instance.
|
||||
*/
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.nwapw.abacus.number;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class PreciseNumber implements NumberInterface{
|
||||
public class PreciseNumber implements NumberInterface {
|
||||
|
||||
/**
|
||||
* The number one.
|
||||
@@ -25,18 +25,20 @@ public class PreciseNumber implements NumberInterface{
|
||||
|
||||
/**
|
||||
* Constructs a precise number from the given string.
|
||||
*
|
||||
* @param string a string representation of the number meeting the same conditions
|
||||
* as the BidDecimal(String) constructor.
|
||||
*/
|
||||
public PreciseNumber(String string){
|
||||
public PreciseNumber(String string) {
|
||||
value = new BigDecimal(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a precise number from the given BigDecimal.
|
||||
*
|
||||
* @param value a BigDecimal object representing the value of the number.
|
||||
*/
|
||||
public PreciseNumber(BigDecimal value){
|
||||
public PreciseNumber(BigDecimal value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@@ -67,16 +69,16 @@ public class PreciseNumber implements NumberInterface{
|
||||
|
||||
@Override
|
||||
public NumberInterface intPow(int exponent) {
|
||||
if(exponent == 0){
|
||||
if (exponent == 0) {
|
||||
return PreciseNumber.ONE;
|
||||
}
|
||||
boolean takeReciprocal = exponent < 0;
|
||||
exponent = Math.abs(exponent);
|
||||
NumberInterface power = this;
|
||||
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
||||
for (int currentExponent = 1; currentExponent < exponent; currentExponent++) {
|
||||
power = power.multiply(this);
|
||||
}
|
||||
if(takeReciprocal){
|
||||
if (takeReciprocal) {
|
||||
power = PreciseNumber.ONE.divide(power);
|
||||
}
|
||||
return power;
|
||||
@@ -93,13 +95,13 @@ public class PreciseNumber implements NumberInterface{
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface negate(){
|
||||
public NumberInterface negate() {
|
||||
return new PreciseNumber(value.negate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()){
|
||||
if (toClass == this.getClass()) {
|
||||
return this;
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user