问题
I have a HashMap called List<String, Intger> wordFreqMap whose size is 234
wordFreqMap = {radiology=1, shift=2, mummy=1, empirical=1, awful=1, geoff=1, .......}
I want to calculate the term frequency of each word.
term frequency = frequency of term / total number of terms
public static Map<String, Double> getTFMap (Map<String, Integer> wordFreqMap)
{
Map<String, Double> tfMap = new HashMap<String, Double>();
int noOfTerms = wordFreqMap.size();
Double tf;
for (Entry<String, Integer> word : wordFreqMap.entrySet() )
{
tf = (double) ( word.getValue() / noOfTerms );
tfMap.put(word.getKey(), tf );
}
return tfMap;
}
My problem is that, tfMap is returning {radiology=0.0, shift=0.0, mummy=0.0, empirical=0.0, awful=0.0, geoff=0.0, .....}
I don't understand why it returns 0.0 for every term. How do I fix it?
I should get something like {radiology=0.00427, shift=0.00854, ...}
回答1:
You're performing an integer division and then type casting:
tf = (double) ( word.getValue() / noOfTerms );
^-----integer division----^
Type cast one of the elements in the division to convert into a floating point division:
tf = ((double)word.getValue()) / noOfTerms;
回答2:
You are doing integer division and then casting that answer to a double. What you need to do is cast one of the two values to a double first, and then do division on it. That should get you the answer you desire.
回答3:
Integer/Integer is an Integer which is casted to a Double so ,it remains an Integer expanded to a Double.
Change it to
tf = ( (double)word.getValue() / noOfTerms );
回答4:
what you are doing is dividing an integer by another integer and then trying to cast that to a double. int/int is a int do though you cast that to a double you will not get the actual value with decimal points.
int/int -> int
What you should do is either cast word.getValue() or noOfterms to double then
int/double -> double
double/int -> double
double/double -> double
e.g.
tf = (double)word.getValue()/noOfTerms;
or
tf = word.getValue()/(double)noOfTerms;
来源:https://stackoverflow.com/questions/25748763/double-division-behaving-wrongly