Question

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());
        }
    }

`

Was it helpful?

Solution 2

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.

OTHER TIPS

Looks like hitDocuments or totalDocuments (or both) is a Double, and that hitDocuments is 0.0. anything / 0.0 = Double.Infinity (or NaN if totalDocuments is 0.0). Can't take the log of either.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top