mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 16:15:19 +00:00
Compare commits
19 Commits
remove-cac
...
sig-fig
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8b3559cec | ||
|
|
4cf4ba98a8 | ||
|
|
68fbcd2d7c | ||
|
|
ed92b382f0 | ||
|
|
1cd544e712 | ||
|
|
a8c70a6bbe | ||
|
|
8a9df051cf | ||
|
|
4eda15b3fb | ||
| c9e0d4f8d3 | |||
| 213d7af10b | |||
| 585a3839c1 | |||
| 205b73f62c | |||
| 1cd332b97d | |||
| 314552f95a | |||
| c5ec521996 | |||
| 4712bbfded | |||
| 7ae7f6d9a5 | |||
|
|
d0ccb8b625 | ||
|
|
7d5efa1fe6 |
@@ -30,6 +30,10 @@ public class Configuration {
|
|||||||
*/
|
*/
|
||||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The computation delay for which the thread can run without interruption.
|
||||||
|
*/
|
||||||
|
private double computationDelay = 0;
|
||||||
/**
|
/**
|
||||||
* The implementation of the number that should be used.
|
* The implementation of the number that should be used.
|
||||||
*/
|
*/
|
||||||
@@ -51,10 +55,12 @@ public class Configuration {
|
|||||||
/**
|
/**
|
||||||
* Creates a new configuration with the given values.
|
* Creates a new configuration with the given values.
|
||||||
*
|
*
|
||||||
|
* @param computationDelay the delay before the computation gets killed.
|
||||||
* @param numberImplementation the number implementation, like "naive" or "precise"
|
* @param numberImplementation the number implementation, like "naive" or "precise"
|
||||||
* @param disabledPlugins the list of disabled plugins.
|
* @param disabledPlugins the list of disabled plugins.
|
||||||
*/
|
*/
|
||||||
public Configuration(String numberImplementation, String[] disabledPlugins) {
|
public Configuration(double computationDelay, String numberImplementation, String[] disabledPlugins) {
|
||||||
|
this.computationDelay = computationDelay;
|
||||||
this.numberImplementation = numberImplementation;
|
this.numberImplementation = numberImplementation;
|
||||||
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
|
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
|
||||||
}
|
}
|
||||||
@@ -75,6 +81,7 @@ public class Configuration {
|
|||||||
* @param otherConfiguration the configuration to copy from.
|
* @param otherConfiguration the configuration to copy from.
|
||||||
*/
|
*/
|
||||||
public void copyFrom(Configuration otherConfiguration) {
|
public void copyFrom(Configuration otherConfiguration) {
|
||||||
|
this.computationDelay = otherConfiguration.computationDelay;
|
||||||
this.numberImplementation = otherConfiguration.numberImplementation;
|
this.numberImplementation = otherConfiguration.numberImplementation;
|
||||||
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
|
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
|
||||||
}
|
}
|
||||||
@@ -130,4 +137,23 @@ public class Configuration {
|
|||||||
return disabledPlugins;
|
return disabledPlugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the computation delay specified in the configuration.
|
||||||
|
*
|
||||||
|
* @return the computaton delay.
|
||||||
|
*/
|
||||||
|
public double getComputationDelay() {
|
||||||
|
return computationDelay;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the computation delay.
|
||||||
|
*
|
||||||
|
* @param computationDelay the new computation delay.
|
||||||
|
*/
|
||||||
|
public void setComputationDelay(double computationDelay) {
|
||||||
|
this.computationDelay = computationDelay;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,13 +12,25 @@ import javafx.stage.Stage;
|
|||||||
*/
|
*/
|
||||||
public class AbacusApplication extends Application {
|
public class AbacusApplication extends Application {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The controller currently managing the application.
|
||||||
|
*/
|
||||||
|
private AbacusController controller;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage primaryStage) throws Exception {
|
public void start(Stage primaryStage) throws Exception {
|
||||||
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
|
FXMLLoader loader = new FXMLLoader(getClass().getResource("/abacus.fxml"));
|
||||||
|
Parent parent = loader.load();
|
||||||
|
controller = loader.getController();
|
||||||
Scene mainScene = new Scene(parent, 320, 480);
|
Scene mainScene = new Scene(parent, 320, 480);
|
||||||
primaryStage.setScene(mainScene);
|
primaryStage.setScene(mainScene);
|
||||||
primaryStage.setTitle("Abacus");
|
primaryStage.setTitle("Abacus");
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() throws Exception {
|
||||||
|
super.stop();
|
||||||
|
controller.performStop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package org.nwapw.abacus.fx;
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
import javafx.beans.value.ChangeListener;
|
||||||
|
import javafx.beans.value.ObservableValue;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
@@ -89,6 +91,8 @@ public class AbacusController implements PluginListener {
|
|||||||
private ComboBox<String> numberImplementationBox;
|
private ComboBox<String> numberImplementationBox;
|
||||||
@FXML
|
@FXML
|
||||||
private ListView<ToggleablePlugin> enabledPluginView;
|
private ListView<ToggleablePlugin> enabledPluginView;
|
||||||
|
@FXML
|
||||||
|
private TextField computationLimitField;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of history entries, created by the users.
|
* The list of history entries, created by the users.
|
||||||
@@ -124,6 +128,17 @@ public class AbacusController implements PluginListener {
|
|||||||
* The alert shown when a press to "apply" is needed.
|
* The alert shown when a press to "apply" is needed.
|
||||||
*/
|
*/
|
||||||
private Alert reloadAlert;
|
private Alert reloadAlert;
|
||||||
|
/**
|
||||||
|
* The runnable that takes care of killing computations that take too long.
|
||||||
|
*/
|
||||||
|
private final Runnable TIMER_RUNNABLE = () -> {
|
||||||
|
try {
|
||||||
|
Configuration abacusConfig = abacus.getConfiguration();
|
||||||
|
if(abacusConfig.getComputationDelay() == 0) return;
|
||||||
|
Thread.sleep((long) (abacusConfig.getComputationDelay() * 1000));
|
||||||
|
performStop();
|
||||||
|
} catch (InterruptedException e) { }
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* The runnable used to perform the calculation.
|
* The runnable used to perform the calculation.
|
||||||
*/
|
*/
|
||||||
@@ -161,6 +176,10 @@ public class AbacusController implements PluginListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* The thread that is waiting to pause the calculation.
|
||||||
|
*/
|
||||||
|
private Thread computationLimitThread;
|
||||||
/**
|
/**
|
||||||
* The thread in which the computation runs.
|
* The thread in which the computation runs.
|
||||||
*/
|
*/
|
||||||
@@ -224,6 +243,15 @@ public class AbacusController implements PluginListener {
|
|||||||
}
|
}
|
||||||
abacusPluginManager.reload();
|
abacusPluginManager.reload();
|
||||||
|
|
||||||
|
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
|
||||||
|
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
|
if(!newValue.matches("(\\d+(\\.\\d*)?)?")) {
|
||||||
|
computationLimitField.setText(oldValue);
|
||||||
|
} else {
|
||||||
|
changesMade = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
changesMade = false;
|
changesMade = false;
|
||||||
reloadAlertShown = false;
|
reloadAlertShown = false;
|
||||||
|
|
||||||
@@ -234,21 +262,29 @@ public class AbacusController implements PluginListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void performCalculation() {
|
public void performCalculation() {
|
||||||
inputButton.setDisable(true);
|
inputButton.setDisable(true);
|
||||||
stopButton.setDisable(false);
|
stopButton.setDisable(false);
|
||||||
calculationThread = new Thread(CALCULATION_RUNNABLE);
|
calculationThread = new Thread(CALCULATION_RUNNABLE);
|
||||||
calculationThread.start();
|
calculationThread.start();
|
||||||
|
computationLimitThread = new Thread(TIMER_RUNNABLE);
|
||||||
|
computationLimitThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void performStop(){
|
public void performStop(){
|
||||||
if(calculationThread != null)
|
if(calculationThread != null) {
|
||||||
calculationThread.interrupt();
|
calculationThread.interrupt();
|
||||||
|
calculationThread = null;
|
||||||
|
}
|
||||||
|
if(computationLimitThread != null){
|
||||||
|
computationLimitThread.interrupt();
|
||||||
|
computationLimitThread = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void performSaveAndReload() {
|
public void performSaveAndReload() {
|
||||||
performSave();
|
performSave();
|
||||||
performReload();
|
performReload();
|
||||||
changesMade = false;
|
changesMade = false;
|
||||||
@@ -256,13 +292,13 @@ public class AbacusController implements PluginListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void performReload() {
|
public void performReload() {
|
||||||
alertIfApplyNeeded(true);
|
alertIfApplyNeeded(true);
|
||||||
abacus.getPluginManager().reload();
|
abacus.getPluginManager().reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void performSave() {
|
public void performSave() {
|
||||||
Configuration configuration = abacus.getConfiguration();
|
Configuration configuration = abacus.getConfiguration();
|
||||||
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
|
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
|
||||||
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
||||||
@@ -270,6 +306,8 @@ public class AbacusController implements PluginListener {
|
|||||||
for (ToggleablePlugin pluginEntry : enabledPlugins) {
|
for (ToggleablePlugin pluginEntry : enabledPlugins) {
|
||||||
if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
||||||
}
|
}
|
||||||
|
if(computationLimitField.getText().matches("\\d*(\\.\\d+)?") && computationLimitField.getText().length() != 0)
|
||||||
|
configuration.setComputationDelay(Double.parseDouble(computationLimitField.getText()));
|
||||||
configuration.saveTo(CONFIG_FILE);
|
configuration.saveTo(CONFIG_FILE);
|
||||||
changesMade = false;
|
changesMade = false;
|
||||||
reloadAlertShown = false;
|
reloadAlertShown = false;
|
||||||
@@ -296,4 +334,5 @@ public class AbacusController implements PluginListener {
|
|||||||
enabledPlugins.clear();
|
enabledPlugins.clear();
|
||||||
numberImplementationOptions.clear();
|
numberImplementationOptions.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,5 +128,9 @@ public class NaiveNumber extends NumberInterface {
|
|||||||
return Double.toString(Math.round(value * shiftBy) / shiftBy);
|
return Double.toString(Math.round(value * shiftBy) / shiftBy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface getMaxError(){
|
||||||
|
return new NaiveNumber(Math.pow(10, -18));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ public abstract class NumberInterface {
|
|||||||
/**
|
/**
|
||||||
* Returns the least integer greater than or equal to the number.
|
* Returns the least integer greater than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the least integer >= the number, if int can hold the value.
|
* @return the least integer greater or equal to the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface ceilingInternal();
|
protected abstract NumberInterface ceilingInternal();
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ public abstract class NumberInterface {
|
|||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* Also, checks if the thread has been interrupted, and if so, throws
|
||||||
* an exception.
|
* an exception.
|
||||||
*
|
*
|
||||||
* @return the least integer bigger or equal to the number, if int can hold the value.
|
* @return the least integer bigger or equal to the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface ceiling(){
|
public final NumberInterface ceiling(){
|
||||||
checkInterrupted();
|
checkInterrupted();
|
||||||
@@ -192,7 +192,7 @@ public abstract class NumberInterface {
|
|||||||
/**
|
/**
|
||||||
* Return the greatest integer less than or equal to the number.
|
* Return the greatest integer less than or equal to the number.
|
||||||
*
|
*
|
||||||
* @return the greatest integer smaller or equal the number, if int can hold the value.
|
* @return the greatest integer smaller or equal the number.
|
||||||
*/
|
*/
|
||||||
protected abstract NumberInterface floorInternal();
|
protected abstract NumberInterface floorInternal();
|
||||||
|
|
||||||
@@ -201,7 +201,7 @@ public abstract class NumberInterface {
|
|||||||
* Also, checks if the thread has been interrupted, and if so, throws
|
* Also, checks if the thread has been interrupted, and if so, throws
|
||||||
* an exception.
|
* an exception.
|
||||||
*
|
*
|
||||||
* @return the greatest int >= the number, if int can hold the value.
|
* @return the greatest int smaller than or equal to the number.
|
||||||
*/
|
*/
|
||||||
public final NumberInterface floor(){
|
public final NumberInterface floor(){
|
||||||
checkInterrupted();
|
checkInterrupted();
|
||||||
@@ -216,7 +216,7 @@ public abstract class NumberInterface {
|
|||||||
protected abstract NumberInterface fractionalPartInternal();
|
protected abstract NumberInterface fractionalPartInternal();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the fractional part of the number.
|
* Returns the fractional part of the number, specifically x - floor(x).
|
||||||
* Also, checks if the thread has been interrupted,
|
* Also, checks if the thread has been interrupted,
|
||||||
* and if so, throws an exception.
|
* and if so, throws an exception.
|
||||||
* @return the fractional part of the number.
|
* @return the fractional part of the number.
|
||||||
@@ -255,4 +255,11 @@ public abstract class NumberInterface {
|
|||||||
return promoteToInternal(toClass);
|
return promoteToInternal(toClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the smallest error this instance can tolerate depending
|
||||||
|
* on its precision and value.
|
||||||
|
* @return the smallest error that should be permitted in calculations.
|
||||||
|
*/
|
||||||
|
public abstract NumberInterface getMaxError();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.nwapw.abacus.number;
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.MathContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A number that uses a BigDecimal to store its value,
|
* A number that uses a BigDecimal to store its value,
|
||||||
@@ -22,6 +22,21 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
*/
|
*/
|
||||||
public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of extra significant figures kept in calculations before rounding for output.
|
||||||
|
*/
|
||||||
|
private static int numExtraInternalSigFigs = 15;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MathContext that is used when rounding a number prior to output.
|
||||||
|
*/
|
||||||
|
private static MathContext outputContext = new MathContext(50);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MathContext that is actually used in calculations.
|
||||||
|
*/
|
||||||
|
private static MathContext internalContext = new MathContext(outputContext.getPrecision()+numExtraInternalSigFigs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The value of the PreciseNumber.
|
* The value of the PreciseNumber.
|
||||||
*/
|
*/
|
||||||
@@ -48,7 +63,7 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getMaxPrecision() {
|
public int getMaxPrecision() {
|
||||||
return 65;
|
return internalContext.getPrecision();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -58,7 +73,7 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface divideInternal(NumberInterface divisor) {
|
public NumberInterface divideInternal(NumberInterface divisor) {
|
||||||
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP));
|
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, internalContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -113,19 +128,18 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
String str = value.toPlainString();
|
String str = value.toPlainString();
|
||||||
int decimalIndex = str.indexOf('.');
|
int decimalIndex = str.indexOf('.');
|
||||||
if (decimalIndex != -1) {
|
if (decimalIndex != -1) {
|
||||||
return new PreciseNumber(str.substring(0, decimalIndex));
|
NumberInterface floor = new PreciseNumber(str.substring(0, decimalIndex));
|
||||||
|
if(signum() == -1){
|
||||||
|
floor = floor.subtract(ONE);
|
||||||
|
}
|
||||||
|
return floor;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface fractionalPartInternal() {
|
public NumberInterface fractionalPartInternal() {
|
||||||
String str = value.toPlainString();
|
return this.subtractInternal(floorInternal());
|
||||||
int decimalIndex = str.indexOf('.');
|
|
||||||
if (decimalIndex != -1) {
|
|
||||||
return new PreciseNumber(str.substring(decimalIndex + 1));
|
|
||||||
}
|
|
||||||
return ZERO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -148,7 +162,11 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
BigDecimal rounded = value.setScale(getMaxPrecision() - 15, RoundingMode.HALF_UP);
|
return value.round(outputContext).toString();
|
||||||
return rounded.stripTrailingZeros().toPlainString();
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface getMaxError(){
|
||||||
|
return new PreciseNumber(value.ulp()).multiplyInternal(TEN.intPowInternal(value.precision()-internalContext.getPrecision()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ import java.util.function.BiFunction;
|
|||||||
*/
|
*/
|
||||||
public class StandardPlugin extends Plugin {
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores objects of NumberInterface with integer values for reuse.
|
||||||
|
*/
|
||||||
|
private final static HashMap<Class<? extends NumberInterface>, HashMap<Integer, NumberInterface>> integerValues = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The addition operator, +
|
* The addition operator, +
|
||||||
*/
|
*/
|
||||||
@@ -112,7 +117,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
if (params[0].signum() == 0) {
|
if (params[0].signum() == 0) {
|
||||||
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
return fromInt(params[0].getClass(), 1);
|
||||||
}
|
}
|
||||||
NumberInterface factorial = params[0];
|
NumberInterface factorial = params[0];
|
||||||
NumberInterface multiplier = params[0];
|
NumberInterface multiplier = params[0];
|
||||||
@@ -157,14 +162,14 @@ public class StandardPlugin extends Plugin {
|
|||||||
int powersOf2 = 0;
|
int powersOf2 = 0;
|
||||||
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
|
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
||||||
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
param = param.divide(fromInt(param.getClass(), 2));
|
||||||
powersOf2++;
|
powersOf2++;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||||
break;
|
break;
|
||||||
//No infinite loop for you.
|
//No infinite loop for you.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
|
param = param.multiply(fromInt(param.getClass(), 2));
|
||||||
powersOf2--;
|
powersOf2--;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
|
||||||
break;
|
break;
|
||||||
@@ -183,7 +188,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
private NumberInterface getLogPartialSum(NumberInterface x) {
|
private NumberInterface getLogPartialSum(NumberInterface x) {
|
||||||
|
|
||||||
NumberInterface maxError = getMaxError(x);
|
NumberInterface maxError = x.getMaxError();
|
||||||
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
||||||
NumberInterface currentNumerator = x, currentTerm = x, sum = x;
|
NumberInterface currentNumerator = x, currentTerm = x, sum = x;
|
||||||
int n = 1;
|
int n = 1;
|
||||||
@@ -202,18 +207,18 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return the value of log(2) with the appropriate precision.
|
* @return the value of log(2) with the appropriate precision.
|
||||||
*/
|
*/
|
||||||
private NumberInterface getLog2(NumberInterface number) {
|
private NumberInterface getLog2(NumberInterface number) {
|
||||||
NumberInterface maxError = getMaxError(number);
|
NumberInterface maxError = number.getMaxError();
|
||||||
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
//NumberInterface errorBound = fromInt(number.getClass(), 1);
|
||||||
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
|
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
|
||||||
//In the following, a=1/3^n, b=1/4^n, c = 1/n.
|
//In the following, a=1/3^n, b=1/4^n, c = 1/n.
|
||||||
//a is also an error bound.
|
//a is also an error bound.
|
||||||
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
NumberInterface a = fromInt(number.getClass(), 1), b = a, c = a;
|
||||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while (a.compareTo(maxError) >= 1) {
|
while (a.compareTo(maxError) >= 1) {
|
||||||
n++;
|
n++;
|
||||||
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
a = a.divide(fromInt(number.getClass(), 3));
|
||||||
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
b = b.divide(fromInt(number.getClass(), 4));
|
||||||
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
|
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
|
||||||
sum = sum.add(a.add(b).multiply(c));
|
sum = sum.add(a.add(b).multiply(c));
|
||||||
}
|
}
|
||||||
@@ -296,22 +301,17 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface maxError = getMaxError(params[0]);
|
NumberInterface maxError = params[0].getMaxError();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
if (params[0].signum() <= 0) {
|
if (params[0].signum() < 0) {
|
||||||
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
NumberInterface[] negatedParams = {params[0].negate()};
|
||||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
return fromInt(params[0].getClass(), 1).divide(applyInternal(negatedParams));
|
||||||
n++;
|
|
||||||
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
|
||||||
sum = sum.add(currentTerm);
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
} else {
|
} else {
|
||||||
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
||||||
//right and left refer to lhs and rhs in the above inequality.
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||||
NumberInterface nextNumerator = params[0];
|
NumberInterface nextNumerator = params[0];
|
||||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
|
NumberInterface left = params[0].multiply(fromInt(params[0].getClass(), 3).intPow(params[0].ceiling().intValue())), right = maxError;
|
||||||
do {
|
do {
|
||||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
|
||||||
n++;
|
n++;
|
||||||
@@ -335,7 +335,8 @@ public class StandardPlugin extends Plugin {
|
|||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2
|
return params.length == 2
|
||||||
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
||||||
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0);
|
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0)
|
||||||
|
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -344,6 +345,13 @@ public class StandardPlugin extends Plugin {
|
|||||||
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
||||||
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
||||||
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
||||||
|
//Detect integer bases:
|
||||||
|
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
|
||||||
|
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
|
||||||
|
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0){
|
||||||
|
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
|
||||||
|
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
|
||||||
|
}
|
||||||
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
|
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -359,12 +367,12 @@ public class StandardPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface pi = piFor(params[0].getClass());
|
NumberInterface pi = piFor(params[0].getClass());
|
||||||
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2));
|
||||||
NumberInterface theta = getSmallAngle(params[0], pi);
|
NumberInterface theta = getSmallAngle(params[0], pi);
|
||||||
//System.out.println(theta);
|
//System.out.println(theta);
|
||||||
if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) {
|
if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) {
|
||||||
theta = theta.subtract(twoPi);
|
theta = theta.subtract(twoPi);
|
||||||
} else if (theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0) {
|
} else if (theta.compareTo(pi.divide(fromInt(pi.getClass(), 2))) > 0) {
|
||||||
theta = pi.subtract(theta);
|
theta = pi.subtract(theta);
|
||||||
}
|
}
|
||||||
//System.out.println(theta);
|
//System.out.println(theta);
|
||||||
@@ -382,7 +390,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return functionSin.apply(piFor(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
|
return functionSin.apply(piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2))
|
||||||
.subtract(params[0]));
|
.subtract(params[0]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -463,16 +471,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the maximum error based on the precision of the class of number.
|
|
||||||
*
|
|
||||||
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
|
||||||
* @return the maximum error.
|
|
||||||
*/
|
|
||||||
private static NumberInterface getMaxError(NumberInterface number) {
|
|
||||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factorial function that uses memoization for each number class; it efficiently
|
* A factorial function that uses memoization for each number class; it efficiently
|
||||||
* computes factorials of non-negative integers.
|
* computes factorials of non-negative integers.
|
||||||
@@ -504,7 +502,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
private static NumberInterface sinTaylor(NumberInterface x) {
|
private static NumberInterface sinTaylor(NumberInterface x) {
|
||||||
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
||||||
NumberInterface maxError = getMaxError(x);
|
NumberInterface maxError = x.getMaxError();
|
||||||
int n = 1;
|
int n = 1;
|
||||||
do {
|
do {
|
||||||
n += 2;
|
n += 2;
|
||||||
@@ -531,6 +529,22 @@ public class StandardPlugin extends Plugin {
|
|||||||
return theta;
|
return theta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a number of class numType with value n.
|
||||||
|
* @param numType class of number to return.
|
||||||
|
* @param n value of returned number.
|
||||||
|
* @return numClass instance with value n.
|
||||||
|
*/
|
||||||
|
private static NumberInterface fromInt(Class<? extends NumberInterface> numType, int n){
|
||||||
|
if(!integerValues.containsKey(numType)){
|
||||||
|
integerValues.put(numType, new HashMap<>());
|
||||||
|
}
|
||||||
|
if(!integerValues.get(numType).containsKey(n)){
|
||||||
|
integerValues.get(numType).put(n, new NaiveNumber(n).promoteTo(numType));
|
||||||
|
}
|
||||||
|
return integerValues.get(numType).get(n);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
||||||
|
|||||||
@@ -50,7 +50,9 @@
|
|||||||
<ListView fx:id="enabledPluginView"
|
<ListView fx:id="enabledPluginView"
|
||||||
GridPane.rowIndex="1" GridPane.columnIndex="0"
|
GridPane.rowIndex="1" GridPane.columnIndex="0"
|
||||||
GridPane.columnSpan="2" maxHeight="100"/>
|
GridPane.columnSpan="2" maxHeight="100"/>
|
||||||
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10"
|
<Text GridPane.columnIndex="0" GridPane.rowIndex="2" text="Computation Limit"/>
|
||||||
|
<TextField fx:id="computationLimitField" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
|
||||||
|
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="3" hgap="10"
|
||||||
vgap="10">
|
vgap="10">
|
||||||
<Button text="Apply" onAction="#performSave"/>
|
<Button text="Apply" onAction="#performSave"/>
|
||||||
<Button text="Reload Plugins" onAction="#performReload"/>
|
<Button text="Reload Plugins" onAction="#performReload"/>
|
||||||
|
|||||||
9
src/test/java/org/nwapw/abacus/tests/CalculationTests.java
Normal file → Executable file
9
src/test/java/org/nwapw/abacus/tests/CalculationTests.java
Normal file → Executable file
@@ -11,7 +11,7 @@ import org.nwapw.abacus.tree.TreeNode;
|
|||||||
|
|
||||||
public class CalculationTests {
|
public class CalculationTests {
|
||||||
|
|
||||||
private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
|
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{}));
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void prepareTests(){
|
public static void prepareTests(){
|
||||||
@@ -88,8 +88,8 @@ public class CalculationTests {
|
|||||||
public void testExp(){
|
public void testExp(){
|
||||||
testOutput("exp0", "exp(0)", "1");
|
testOutput("exp0", "exp(0)", "1");
|
||||||
testOutput("exp1", "exp(1)", "2.718281828459045235360287471352662497757247");
|
testOutput("exp1", "exp(1)", "2.718281828459045235360287471352662497757247");
|
||||||
testOutput("exp300", "exp(300)", "19424263952412559365842088360176992193662086");
|
testOutput("exp300", "exp(300)", "1.9424263952412559365842088360176992193662086");
|
||||||
testOutput("exp300", "exp(300)", "19424263952412559365842088360176992193662086");
|
testOutput("exp(-500)", "exp((500)`)", "7.1245764067412855315491573771227552469277568");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -99,6 +99,9 @@ public class CalculationTests {
|
|||||||
testOutput("2^1", "(2^1)", "2");
|
testOutput("2^1", "(2^1)", "2");
|
||||||
testOutput("2^-1", "(2^(1)`)", "0.5");
|
testOutput("2^-1", "(2^(1)`)", "0.5");
|
||||||
testOutput("2^50", "(2^50)", "112589990684262");
|
testOutput("2^50", "(2^50)", "112589990684262");
|
||||||
|
testOutput("7^(-sqrt2*17)", "(7^((sqrt(2)*17))`)", "4.81354609155297814551845300063563");
|
||||||
|
testEvalError("0^0", "(0^0)");
|
||||||
|
testEvalError("(-13)^.9999", "((13)`^0.9999)");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class TokenizerTests {
|
public class TokenizerTests {
|
||||||
|
|
||||||
private static Abacus abacus = new Abacus(new Configuration("precise", new String[]{}));
|
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{}));
|
||||||
private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||||
private static Function subtractFunction = new Function() {
|
private static Function subtractFunction = new Function() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user