如何在 Java 中计算对数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14066186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:56:53  来源:igfitidea点击:

How to compute logarithms in Java?

javamath

提问by Lemon Juice

Following is the formula

以下是公式

Percentage of Fat = 495 / (1.0324 - 0.19077 x (LOG10(waist - neck)) + 0.15456 x (LOG10(height))) - 450

脂肪百分比 = 495 / (1.0324 - 0.19077 x (LOG10(腰围 - 颈部)) + 0.15456 x (LOG10(height))) - 450

Following is my code

以下是我的代码

import java.math.*;

public class Position
{
    static double waist=66,neck=30,height=150;

    public static void main(String[]args)
    {
        double fat = 495 / ( (1.0324 - 0.19077)* (Math.log(waist - neck)/Math.log(10)) + (0.15456) *  (Math.log(height)/Math.log(10))) - 450;

        System.out.println(fat);
    }
}

The answer I get is incorrect. It should be 11.8% (Use the following http://lowcarbdiets.about.com/library/blbodyfatcalculator.htm)

我得到的答案是不正确的。它应该是 11.8%(使用以下http://lowcarbdiets.about.com/library/blbodyfatcalculator.htm

I believe I have done something incorrectly in logarithms. Please help me to get the correct answer.

我相信我在对数方面做错了一些事情。请帮助我得到正确的答案。

回答by eis

You have written it incorrectly to code. Try with:

您将其错误地编写为代码。尝试:

import java.math.*;

public class Position
{
    static double waist=66,neck=30,height=150;

    public static void main(String[]args)
    {
        double fat = 495 / ( 1.0324
            - (0.19077 * (Math.log(waist - neck)/Math.log(10)))
            + (0.15456) * (Math.log(height)/Math.log(10))
            ) - 450;

        System.out.println(fat);
    }
}

The difference is that this doesn't have 1.0324 - 0.19077- the original formula didn't have it either, so you had misplaced parenthesis.

不同之处在于它没有1.0324 - 0.19077- 原始公式也没有它,所以你放错了括号。

As noted by @a_horse_with_no_name, Math.log() will use e-based logarithm, and not 10-based, but in the scope of this code the result is the same. To use 10-based, you'd use Math.log10().

正如@a_horse_with_no_name 所指出的,Math.log() 将使用基于 e 的对数,而不是基于 10 的对数,但在此代码的范围内,结果是相同的。要使用基于 10 的,您将使用 Math.log10()。

回答by Henry

The log calculation is correct but you have misplaced some parenthesis.

对数计算是正确的,但您放错了一些括号。

double fat = 495 / ( 1.0324 - 0.19077* (Math.log(waist - neck)/Math.log(10)) + (0.15456) *  (Math.log(height)/Math.log(10))) - 450

回答by Nikolay Kuznetsov

495 / (1.0324 - 0.19077 x

and this

还有这个

495 / ( (1.0324 - 0.19077)*

does not match

不匹配