如何用Java计算对数?

时间:2012-12-28 07:30:30

标签: java math

以下是公式

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

以下是我的代码

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);
    }
}

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

我相信我在对数方面做错了什么。请帮助我得到正确的答案。

3 个答案:

答案 0 :(得分:6)

您已将其错误地写入代码。试试:

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);
    }
}

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

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

495 / (1.0324 - 0.19077 x

和这个

495 / ( (1.0324 - 0.19077)*

不符合