Double division behaving wrongly

后端 未结 4 1251
旧巷少年郎
旧巷少年郎 2020-11-30 15:51

I have a HashMap called List wordFreqMap whose size is 234

wordFreqMap = {radiology         


        
相关标签:
4条回答
  • 2020-11-30 16:20

    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;
    
    0 讨论(0)
  • 2020-11-30 16:22

    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.

    0 讨论(0)
  • 2020-11-30 16:23

    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 );
    
    0 讨论(0)
  • 2020-11-30 16:29

    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;
    
    0 讨论(0)
提交回复
热议问题