Java 使用bigdecimal的数学结果为0

Java 使用bigdecimal的数学结果为0,java,precision,Java,Precision,我想用BigDecimal解决一个问题。我的代码: BigDecimal tweetcount = new BigDecimal(3344048); BigDecimal emotionCountBig = new BigDecimal(855937); BigDecimal emotionCountSentenceBig = new BigDecimal(84988); MathContext mc = new MathContext(64); PMI[cnt] = (emotionCou

我想用BigDecimal解决一个问题。我的代码:

BigDecimal tweetcount = new BigDecimal(3344048);
BigDecimal emotionCountBig = new BigDecimal(855937);
BigDecimal emotionCountSentenceBig = new BigDecimal(84988); 

MathContext mc = new MathContext(64);
PMI[cnt] = (emotionCountSentenceBig.divide((tweetcount.multiply(emotionCountBig,mc)),RoundingMode.HALF_UP));
我想做的是:
emotionCountSentenceBig/(emotionCountBig*tweetcount)

(值可以更大)


如果我尝试这个,我得到一个零,这是不可能的。有什么帮助吗?

您还需要为分区指定MathContext:

emotionCountSentenceBig.divide(tweetcount.multiply(emotionCountBig, mc), mc);
这就产生了预期的结果:

2.969226352632111794036880818610913852084810652372969382467557947E-8

现在,正如@PeterLawrey正确评论的那样,您可以使用双打:

public static void main(String[] args) throws Exception {
    double tweetcount = 3344048;
    double emotionCount = 855937;
    double emotionCountSentence = 84988;

    double result = emotionCountSentence / (tweetcount * emotionCount);

    System.out.println("result = " + result);
}
其中打印:

结果=2.969226352632117E-8

请注意,如果您使用:

double result = 84988 / (3344048 * 855937);
实际上,您正在对整数执行运算(*和/),它将返回0。例如,您可以通过显式使用double来防止它(注意
d
):


您还需要为分区指定MathContext:

emotionCountSentenceBig.divide(tweetcount.multiply(emotionCountBig, mc), mc);
这就产生了预期的结果:

2.969226352632111794036880818610913852084810652372969382467557947E-8

现在,正如@PeterLawrey正确评论的那样,您可以使用双打:

public static void main(String[] args) throws Exception {
    double tweetcount = 3344048;
    double emotionCount = 855937;
    double emotionCountSentence = 84988;

    double result = emotionCountSentence / (tweetcount * emotionCount);

    System.out.println("result = " + result);
}
其中打印:

结果=2.969226352632117E-8

请注意,如果您使用:

double result = 84988 / (3344048 * 855937);
实际上,您正在对整数执行运算(*和/),它将返回0。例如,您可以通过显式使用double来防止它(注意
d
):


我会使用
double

int tweetcount = 3344048;
int emotionCountBig = 855937;
int emotionCountSentenceBig = 84988;

double pmi = emotionCountSentenceBig/((double) tweetcount * emotionCountBig);
System.out.println(pmi);
印刷品

2.9692263526321117E-8
这与使用BigDecimal的答案很接近

2.969226352632111794036880818610913852084810652372969382467557947E-8

我会使用
double

int tweetcount = 3344048;
int emotionCountBig = 855937;
int emotionCountSentenceBig = 84988;

double pmi = emotionCountSentenceBig/((double) tweetcount * emotionCountBig);
System.out.println(pmi);
印刷品

2.9692263526321117E-8
这与使用BigDecimal的答案很接近

2.969226352632111794036880818610913852084810652372969382467557947E-8


我怎样才能得到那个值?因为如果我调试它,我看到的唯一值是intCompact,这是不正确的。什么是intCompact?我刚刚复制了你的代码,修改了我的建议,并在
main
方法中打印了结果,然后运行了程序。当数字在变量中时,如何添加d?请注意乘法中的整数溢出。您只需将其中一个分母设为一个
double
如何获得该值?因为如果我调试它,我看到的唯一值是intCompact,这是不正确的。什么是intCompact?我刚刚复制了你的代码,修改了我的建议,并在
main
方法中打印了结果,然后运行了程序。当数字在变量中时,如何添加d?请注意乘法中的整数溢出。您只需要将其中一个分母设为双分母,我怀疑您所建模的分母不精确到小数点后几位,更不用说16位了。我建议改用
double
类型。可能吧,但是乘法得到的数字对于double来说太大了。有办法吗?@Ojtwist我怀疑-你可能在做整数乘法。@PeterLawrey说得很好。我已经相应地进行了编辑。double的最大值为
179769313486231570814527427317043567980705675258444996598917476803157260780028537605858586327668781715404589535143824642343213268946418227684675467035516986049910576551282076245490038932894407586858555133942458390322298180855933212334879787826204747231687177180919988125002461848568
你确定你有使用
double
而不是
int
?我怀疑您所建模的内容不准确到小数点后几位,更不用说16位了。我建议改用
double
类型。可能吧,但是乘法得到的数字对于double来说太大了。有办法吗?@Ojtwist我怀疑-你可能在做整数乘法。@PeterLawrey说得很好。我已经相应地进行了编辑。double的最大值为
179769313486231570814527427317043567980705675258444996598917476803157260780028537605858586327668781715404589535143824642343213268946418227684675467035516986049910576551282076245490038932894407586858555133942458390322298180855933212334879787826204747231687177180919988125002461848568
你确定你有使用
double
而不是
int