1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-26 16:45:21 +00:00

Format code.

This commit is contained in:
2017-07-30 21:11:32 -07:00
parent 122874b97a
commit 3ce74303ed
39 changed files with 695 additions and 561 deletions

View File

@@ -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);
}