【问题标题】:How to implement arctan function in Java?如何在 Java 中实现 arctan 函数?
【发布时间】:2023-01-23 00:48:03
【问题描述】:

要实现的功能

代码

public class arctan {
    public static double arctan(double x) {
        double sum = 0;
        int k = 0;

        double arctan1 = (Math.pow(-1, k) * (Math.pow(x, 2 * k + 1) / (2 * k + 1)));
        for (int i = k; i < 100; i++) {
            sum =+ arctan1;
        }
        return (double) arctan1;
    }
}

问题

我的程序只是返回我的 x 作为输出。我没有看到我正在做的错误。

【问题讨论】:

  • 您认为 arctan1 的价值在您的 for (int i = k; i &lt; 100; i++) 外观中是什么?
  • +sum =+ arctan1;中是多余的。您可能想要 sum += arctan1,但由于更改 k 不会更改,所以仍然无法正常工作已计算值持有arctan1。您需要在循环中每次重新计算。

标签: java


【解决方案1】:

您还必须将 double arctan1 = (Math.pow(-1, k) * (Math.pow(x, 2 * k + 1) / (2 * k + 1))); 放入循环中,因为这就是 Σ 在公式中所做的。

在这种情况下,您也不需要在 for 循环中使用新变量 i。像公式一样使用k就可以了。

所以它会像这样:

public class arctan {
    public static double arctan(double x) {
        double sum = 0;

        for (int k = 0; k < 100; i++) {
            sum += (Math.pow(-1, k) * (Math.pow(x, 2 * k + 1) / (2 * k + 1)));
        }
        return sum;
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 2011-01-19
    • 2014-10-26
    • 2014-05-28
    • 1970-01-01
    • 2015-01-29
    • 2013-10-13
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多