【问题标题】:Testing a quadratic equation检验二次方程
【发布时间】:2012-04-01 23:24:25
【问题描述】:

我正在对一个计算二次方程结果的程序进行代码测试

我需要有以下情况的测试数据,当 a 不为零且 d 为正时,下面的代码中有两种可能性,我需要在 Math.abs(b / a - 200.0)

                caption= "Two roots";
                if (Math.abs(b / a - 200.0) < 1.0e-4)
                {
                    System.out.println("first one");
                    x1 = (-100.0 * (1.0 + Math.sqrt(1.0 - 1.0 / (10000.0 * a))));
                    x2 = (-100.0 * (1.0 - Math.sqrt(1.0 - 1.0 / (10000.0 * a))));
                }
                else
                {
                    System.out.println("secrst one");

                    x1 = (-b - Math.sqrt(d)) / (2.0 * a);
                    x2 = (-b + Math.sqrt(d)) / (2.0 * a);
                }
            }
        }                            

【问题讨论】:

    标签: java math testing


    【解决方案1】:

    不确定您遇到了什么问题。我写道:

    public class Quad
    {
        public static void main(String[] args) {
            double a = Double.parseDouble(args[0]);
            double b = Double.parseDouble(args[1]);
    
            System.out.println(Math.abs(b/a - 200.0));
    
            if (Math.abs(b/a - 200.0) < 1.0e-4) {
                System.out.println("first one");
            }
            else {
                System.out.println("second one");
            }
        }
    }
    

    还有一些输出:

    animato:~/src/Java/SO$ java Quad 1 200
    0.0
    first one
    animato:~/src/Java/SO$ java Quad 2 400
    0.0
    first one
    animato:~/src/Java/SO$ java Quad -3 -600
    0.0
    first one
    

    【讨论】:

    • 是的,这就是我想要的,非常感谢。我想要一个执行语句的值示例(第一个)。
    猜你喜欢
    • 1970-01-01
    • 2016-08-06
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多