开发者

double and Integer conversion

开发者 https://www.devze.com 2023-02-16 09:21 出处:网络
I am trying to figure this out: double chiSquare = ((double)(hashtable.get(key).intValue()/noWords))/* * Math.log10((NO_DOCUMENTS/all.get(key)))*/;

I am trying to figure this out:

double chiSquare = ((double)(hashtable.get(key).intValue()/noWords))/* * Math.log10((NO_DOCUMENTS/all.get(key)))*/;
if (开发者_Go百科key.equals("love")){
    System.out.println(hashtable.get(key));
    System.out.println(all.get(key));                
    System.out.println(noWords);    
    System.out.println(chiSquare);
    System.out.println((double)1/841);
    System.exit(0);
}

Why is it printing chiSquare, prints a zero, while printing 1/841 gives the double value? The hashtable is of <String, Integer>


As @GregS indicated in his comment, an int divided by an int is an int. Casting one of the numbers to a double will produce the output your desire.

double chiSquare = ((double)hashtable.get(key).intValue())/noWords;

Or, use the convenience method on Integer to convert it to a double:

double chiSquare = hashtable.get(key).doubleValue()/noWords;


Because the parentheses are incorrect.

double chiSquare = ( (double) hashtable.get(key).intValue() )/noWords;
0

精彩评论

暂无评论...
验证码 换一张
取 消