Search code examples
javacosine-similarity

Exception in thread "main" java.lang.NumberFormatException: Infinite or NaN


Can you please help me on this one guys.. I am trying to get log of a big decimal (BigDecimal), but I get an exception error message below:

Exception in thread "main" java.lang.NumberFormatException: Infinite or NaN

This is what I have:

BigDecimal num = new BigDecimal(totalDocuments/hitDocuments);
BigDecimal idf = new BigDecimal(Math.log(num.doubleValue()));
BigDecimal termF = new BigDecimal(terms.get(j).getTermFreq());
BigDecimal tfIdf = new BigDecimal(termF.doubleValue() * idf.doubleValue());
terms.get(j).setTfIdf(tfIdf.doubleValue());

I get the exception in the second line; How do I get around this? Thank you so much for your kindness. Oh, and by the way I am trying to calculate the "tf-idf" of text files.

Here is the full code

File[] corpus = new File("files//").listFiles(); int totalDocuments = (corpus.length) - 1; //-1 for the suspect document.

    int hitDocuments = 1;
    for (int i = 0; i < corpus.length; i++) {
        ArrayList<String> corpusWords = getWords(corpus[i].getAbsolutePath());
        for (int j = 0; j < terms.size(); j++) {
            for (int k = 0; k < corpusWords.size(); k++) {
                if (terms.get(j).getTerm().equals(corpusWords.get(k))) {
                    hitDocuments++;
                }
            }
            //Update the tf-idf
            BigDecimal num = new BigDecimal(totalDocuments/hitDocuments);
            BigDecimal idf = new BigDecimal(Math.log(num.doubleValue()));
            BigDecimal termF = new BigDecimal(terms.get(j).getTermFreq());
            BigDecimal tfIdf = new BigDecimal(termF.doubleValue() * idf.doubleValue());
            terms.get(j).setTfIdf(tfIdf.doubleValue());
        }
    }

`


Solution

  • If num is 0 then Math.log() will return Infinite

    If the argument is positive zero or negative zero, then the result is negative infinity.