mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-13 10:25:20 +00:00
Move the source files into a new default directory.
This commit is contained in:
110
src/main/java/org/nwapw/abacus/number/NaiveNumber.java
Executable file
110
src/main/java/org/nwapw/abacus/number/NaiveNumber.java
Executable file
@@ -0,0 +1,110 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
/**
|
||||
* An implementation of NumberInterface using a double.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public static final NaiveNumber ZERO = new NaiveNumber(0);
|
||||
/**
|
||||
* The number one.
|
||||
*/
|
||||
public static final NaiveNumber ONE = new NaiveNumber(1);
|
||||
|
||||
@Override
|
||||
public int getMaxPrecision() {
|
||||
return 18;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface multiply(NumberInterface multiplier) {
|
||||
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface divide(NumberInterface divisor) {
|
||||
return new NaiveNumber(value / ((NaiveNumber)divisor).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface add(NumberInterface summand) {
|
||||
return new NaiveNumber(value + ((NaiveNumber)summand).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface negate() {
|
||||
return new NaiveNumber(-value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface intPow(int exponent) {
|
||||
if(exponent == 0){
|
||||
return NaiveNumber.ONE;
|
||||
}
|
||||
boolean takeReciprocal = exponent < 0;
|
||||
exponent = Math.abs(exponent);
|
||||
NumberInterface power = this;
|
||||
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
||||
power = power.multiply(this);
|
||||
}
|
||||
if(takeReciprocal){
|
||||
power = NaiveNumber.ONE.divide(power);
|
||||
}
|
||||
return power;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(NumberInterface number) {
|
||||
NaiveNumber num = (NaiveNumber) number;
|
||||
return Double.compare(value, num.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int signum() {
|
||||
return this.compareTo(ZERO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()) return this;
|
||||
else if(toClass == PreciseNumber.class){
|
||||
return new PreciseNumber(Double.toString(value));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
double shiftBy = Math.pow(10, 10);
|
||||
return Double.toString(Math.round(value * shiftBy) / shiftBy);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
77
src/main/java/org/nwapw/abacus/number/NumberInterface.java
Executable file
77
src/main/java/org/nwapw/abacus/number/NumberInterface.java
Executable file
@@ -0,0 +1,77 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
/**
|
||||
* An interface used to represent a number.
|
||||
*/
|
||||
public interface NumberInterface {
|
||||
|
||||
/**
|
||||
* The maximum precision to which this number operates.
|
||||
* @return the precision.
|
||||
*/
|
||||
int getMaxPrecision();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
NumberInterface subtract(NumberInterface subtrahend);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
NumberInterface intPow(int exponent);
|
||||
|
||||
/**
|
||||
* Compares this number to another.
|
||||
* @param number the number to compare to.
|
||||
* @return same as Integer.compare();
|
||||
*/
|
||||
int compareTo(NumberInterface number);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
NumberInterface promoteTo(Class<? extends NumberInterface> toClass);
|
||||
|
||||
}
|
||||
113
src/main/java/org/nwapw/abacus/number/PreciseNumber.java
Executable file
113
src/main/java/org/nwapw/abacus/number/PreciseNumber.java
Executable file
@@ -0,0 +1,113 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class PreciseNumber implements NumberInterface{
|
||||
|
||||
/**
|
||||
* The number one.
|
||||
*/
|
||||
static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
||||
/**
|
||||
* The number zero.
|
||||
*/
|
||||
static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
||||
/**
|
||||
* The number ten.
|
||||
*/
|
||||
static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
||||
|
||||
/**
|
||||
* The value of the PreciseNumber.
|
||||
*/
|
||||
BigDecimal value;
|
||||
|
||||
/**
|
||||
* 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){
|
||||
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){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxPrecision() {
|
||||
return 54;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface multiply(NumberInterface multiplier) {
|
||||
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface divide(NumberInterface divisor) {
|
||||
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface add(NumberInterface summand) {
|
||||
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface intPow(int exponent) {
|
||||
if(exponent == 0){
|
||||
return PreciseNumber.ONE;
|
||||
}
|
||||
boolean takeReciprocal = exponent < 0;
|
||||
exponent = Math.abs(exponent);
|
||||
NumberInterface power = this;
|
||||
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
||||
power = power.multiply(this);
|
||||
}
|
||||
if(takeReciprocal){
|
||||
power = PreciseNumber.ONE.divide(power);
|
||||
}
|
||||
return power;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(NumberInterface number) {
|
||||
return value.compareTo(((PreciseNumber) number).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int signum() {
|
||||
return value.signum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface negate(){
|
||||
return new PreciseNumber(value.negate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()){
|
||||
return this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
BigDecimal rounded = value.setScale(getMaxPrecision() - 4, RoundingMode.HALF_UP);
|
||||
return rounded.stripTrailingZeros().toPlainString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user