diff --git a/src/main/java/de/tilman_neumann/jml/roots/RootsReal.java b/src/main/java/de/tilman_neumann/jml/roots/RootsReal.java index 4cae2c01..a43c0529 100644 --- a/src/main/java/de/tilman_neumann/jml/roots/RootsReal.java +++ b/src/main/java/de/tilman_neumann/jml/roots/RootsReal.java @@ -139,4 +139,4 @@ public static BigDecimal ithRoot(BigDecimal x, int i, BigDecimal guess, Scale re throw new IllegalArgumentException("x = " + x + ", but i.th root(x) is defined for x>=0 only!"); } -} \ No newline at end of file +} diff --git a/src/main/java/de/tilman_neumann/jml/roots/SqrtReal.java b/src/main/java/de/tilman_neumann/jml/roots/SqrtReal.java index d06cc05f..b7136e2a 100644 --- a/src/main/java/de/tilman_neumann/jml/roots/SqrtReal.java +++ b/src/main/java/de/tilman_neumann/jml/roots/SqrtReal.java @@ -120,32 +120,4 @@ public static BigDecimal sqrt(BigDecimal x, BigDecimal guess, Scale resultScale) throw new IllegalArgumentException("x = " + x + ", but sqrt(x) is defined for x>=0 only!"); } - - /** - * Test. - * @param argv command line arguments - */ - public static void main(String[] argv) { - ConfigUtil.initProject(); - - if (argv.length != 2) { - // wrong number of arguments ! - LOG.error("Usage: Sqrt !!"); - return; - } - - // get argument for the sqrt function (decimal input required): - BigDecimal x = new BigDecimal(argv[0]); - - // get desired maximal precision - Scale maxScale = Scale.valueOf(Integer.parseInt(argv[1])); - long t0, t1; - - t0 = System.currentTimeMillis(); - for (Scale scale=Scale.valueOf(2); scale.compareTo(maxScale)<=0; scale = scale.add(1)) { - LOG.debug("sqrt(" + x + ", " + scale + ")=" + sqrt(x, scale)); - } - t1 = System.currentTimeMillis(); - LOG.debug("Time of sqrt computation: " + TimeUtil.timeDiffStr(t0,t1)); - } -} \ No newline at end of file +} diff --git a/src/test/java/de/tilman_neumann/jml/roots/RootsRealRunner.java b/src/test/java/de/tilman_neumann/jml/roots/RootsRealRunner.java index e50a2738..86d70a3e 100644 --- a/src/test/java/de/tilman_neumann/jml/roots/RootsRealRunner.java +++ b/src/test/java/de/tilman_neumann/jml/roots/RootsRealRunner.java @@ -53,7 +53,7 @@ public static void main(String[] argv) { t0 = System.currentTimeMillis(); for (int i=2; i<10; i++) { for (Scale scale = Scale.valueOf(2); scale.compareTo(maxScale)<=0; scale = scale.add(1)) { - LOG.debug(i + ".th root(" + x + ", " + scale + ")=" + RootsReal.ithRoot(x, i, scale)); + LOG.debug(i + ".th root(" + x + ", " + scale + ") = " + RootsReal.ithRoot(x, i, scale)); } } t1 = System.currentTimeMillis(); diff --git a/src/test/java/de/tilman_neumann/jml/roots/SqrtRealRunner.java b/src/test/java/de/tilman_neumann/jml/roots/SqrtRealRunner.java new file mode 100644 index 00000000..1cb7c3fc --- /dev/null +++ b/src/test/java/de/tilman_neumann/jml/roots/SqrtRealRunner.java @@ -0,0 +1,65 @@ +/* + * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project. + * Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de + * + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; + * if not, see . + */ +package de.tilman_neumann.jml.roots; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.math.BigDecimal; + +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.LogManager; + +import de.tilman_neumann.jml.precision.Scale; +import de.tilman_neumann.util.ConfigUtil; +import de.tilman_neumann.util.TimeUtil; + +/** + * Test the computation of square roots of floating point numbers by user input. + * + * @author Tilman Neumann + */ +public class SqrtRealRunner { + private static final Logger LOG = LogManager.getLogger(SqrtRealRunner.class); + + /** + * Test. + * @param argv command line arguments + */ + public static void main(String[] argv) { + ConfigUtil.initProject(); + + while (true) { + try { + LOG.info("\nPlease insert :"); + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String line = in.readLine(); + String[] splitted = line.split("\\s+"); + BigDecimal x = new BigDecimal(splitted[0]); + Integer maxScaleInput = Integer.parseInt(splitted[1]); + Scale maxScale = Scale.valueOf(maxScaleInput); + long t0, t1; + + t0 = System.currentTimeMillis(); + t0 = System.currentTimeMillis(); + for (Scale scale=Scale.valueOf(2); scale.compareTo(maxScale)<=0; scale = scale.add(1)) { + LOG.debug("sqrt(" + x + ", " + scale + ") = " + SqrtReal.sqrt(x, scale)); + } + t1 = System.currentTimeMillis(); + LOG.debug("Time of sqrt computations: " + TimeUtil.timeDiffStr(t0,t1)); + } catch (Exception ex) { + LOG.error("Error " + ex, ex); + } + } + } +}