我正试图用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)
(值可以更大)
如果我尝试这个,我得到零,这是不可能的。有什么帮助吗?
答案 0 :(得分:4)
您还需要为分部指定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);
}
打印:
result = 2.9692263526321117E-8
请注意,如果您使用:
double result = 84988 / (3344048 * 855937);
你实际上正在对整数进行操作(*和/),它将返回0.你可以通过显式使用double来阻止它(例如d
):
double result = 84988d / (3344048d * 855937);
答案 1 :(得分:2)
我会使用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