【问题标题】:DecimalFormat not returning the right numberDecimalFormat 未返回正确的数字
【发布时间】:2018-06-04 03:33:25
【问题描述】:

这里是这个特定分配的提示:让 x 是任何实数。编写一个程序,使用二分法 (https://docs.python.org/3/library/functions.html#format) 计算 x 的平方根。请注意,您应该将此问题简化为对 a 和 b 进行自然选择的二分法(即,用户不需要提供 a 和 b)。注意Math.powMath.sqrt是不允许的。

问题是当我输入整数,如 100(其根是“10.0”但控制台返回“9.999997615814209”)或 36(“6.0”但控制台返回“5.999997138977051”)。我认为这可能与我正在对数字进行四舍五入有关,或者只是因为我一般使用doubles,但我不太了解编程中的数学。谢谢你帮助我。

import java.text.DecimalFormat;
import java.util.Scanner;

public class BisectionMethod {

    public double a = 0;
    public double b;
    public double userInput;
    public int iteration = 0;
    public double midpoint;

    public static void main(String[] args) {
        BisectionMethod testClass = new BisectionMethod();
        System.out.println("Answer: " + testClass.FindAnswer(testClass.FindInput(), testClass.a, testClass.FindInterval()));
    }

    public double FindInput() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number to find the root for: ");
        userInput = sc.nextDouble();
        while (userInput < 0) {
            System.out.println("Please enter a number bigger than 0: ");
            userInput = sc.nextDouble();
        }
        sc.close();
        return userInput;
    }

    public double FindInterval() {
        while (b * b < userInput) {
            b = b + 1;
        }
        return b;
    }

    public double RoundNumber(double number) {
        //Rounds number to 4 decimal places.
        String pattern = "#.####";
        DecimalFormat formatNumber = new DecimalFormat(pattern);
        double rounded = Double.valueOf(formatNumber.format(number));
        return rounded;
    }

    public double FindAnswer(double number, double interval1, double interval2) {

        midpoint = (interval1 + interval2) / 2;
        double difference = (midpoint * midpoint) - number;

        while (RoundNumber(difference) != 0) {
            midpoint = (interval1 + interval2) / 2;
            difference = (midpoint * midpoint) - number;
            if (midpoint * midpoint < number) {
                interval1 = midpoint;
            } else {
                interval2 = midpoint;
            }
            iteration = iteration + 1;
        }
        System.out.println("Iterations: " + iteration);
        return midpoint;
    }
}

【问题讨论】:

  • 由于计算四舍五入到小数点后 4 位,请尝试将结果四舍五入到小数点后 4 位。
  • @Andreas 哇,谢谢它的工作!但我真的不明白为什么大声笑
  • 您的代码正在缩小解决方案的范围,每次迭代越来越接近。一旦difference 小于0.00005 (四舍五入== 0),您就让代码停止尝试,那么您为什么对结果只精确到小数点后4 位感到困惑?

标签: java double decimalformat


【解决方案1】:

努力理解编程中的数学是值得的。查看这篇文章,因为它可能会解释为什么你会得到一些意想不到的值 - 可能比你意识到的要复杂一些。编程问题只需要坚韧不拔地解决它们,只要坚持下去。

Double vs. BigDecimal?

【讨论】:

    【解决方案2】:

    改变

    while (RoundNumber(difference) != 0) {
    

    收件人:

    while (difference != 0) {
    

    或者与一个小ε比较:

    while (Math.abs(difference) > 0.0000001) {
    

    【讨论】:

    • 我想使用RoundNumber 函数,这样我也可以处理根不是整数的数字。例如 5 的根是 2.23606... 但如果我没有 RoundNumber 函数,控制台将不会返回任何内容。
    • 使用 epsilon 值。例如:while (Math.abs(difference) &gt; 0.0000001) {你可以把它变小。
    • @jspcal 那就是RoundNumber的效果。从RoundNumber 更改为epsilon 不会有太大的不同(除了它更快)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2019-01-27
    相关资源
    最近更新 更多