{"record":{"id":"c59c94a2d7f30b2a","repo":"TheAlgorithms/Python","slug":"log10-0-is-undefined","errorCode":null,"errorMessage":"log10(0) is undefined.","messagePattern":"log10\\(0\\) is undefined\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/word_frequency_functions.py","lineNumber":113,"sourceCode":"    smoothing, if True return the idf-smooth\n    @returns : log10(N/df) or 1+log10(N/1+df)\n    @examples :\n    >>> inverse_document_frequency(3, 0)\n    Traceback (most recent call last):\n     ...\n    ValueError: log10(0) is undefined.\n    >>> inverse_document_frequency(1, 3)\n    0.477\n    >>> inverse_document_frequency(0, 3)\n    Traceback (most recent call last):\n     ...\n    ZeroDivisionError: df must be > 0\n    >>> inverse_document_frequency(0, 3,True)\n    1.477\n    \"\"\"\n    if smoothing:\n        if n == 0:\n            raise ValueError(\"log10(0) is undefined.\")\n        return round(1 + log10(n / (1 + df)), 3)\n\n    if df == 0:\n        raise ZeroDivisionError(\"df must be > 0\")\n    elif n == 0:\n        raise ValueError(\"log10(0) is undefined.\")\n    return round(log10(n / df), 3)\n\n\ndef tf_idf(tf: int, idf: int) -> float:\n    \"\"\"\n    Combine the term frequency\n    and inverse document frequency functions to\n    calculate the originality of a term. This\n    'originality' is calculated by multiplying\n    the term frequency and the inverse document\n    frequency : tf-idf = TF * IDF\n    @params : tf, the term frequency, and idf, the inverse document","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/word_frequency_functions.py#L95-L131","documentation":"Raised by inverse_document_frequency in word_frequency_functions.py when smoothing=True and the total number of documents n is 0. With smoothing the formula is 1 + log10(n/(1+df)), and log10(0) is mathematically undefined, so the function refuses rather than returning -inf.","triggerScenarios":"Calling inverse_document_frequency(0, df, True) - i.e. n=0 with the smoothing flag set. Note df=0 with smoothing is fine because the denominator becomes 1+df.","commonSituations":"Computing IDF over an empty corpus during unit tests, before documents are loaded, or when a filtering step removes all documents from a shard.","solutions":["Ensure the corpus is non-empty before computing IDF: check n > 0 at the call site.","Load/index documents before running the TF-IDF pipeline.","If an empty corpus is legitimate in your flow, skip IDF computation for those terms rather than calling the function."],"exampleFix":"# before\nidf = inverse_document_frequency(n_docs, df, True)  # n_docs == 0\n\n# after\nidf = inverse_document_frequency(n_docs, df, True) if n_docs > 0 else None","handlingStrategy":"validation","validationCode":"if n_docs > 0:\n    idf = inverse_document_frequency(n_docs, df, True)\nelse:\n    idf = None  # empty corpus: no IDF defined","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check corpus size before running TF-IDF stages.","Skip empty shards/batches in distributed document processing."],"tags":["nlp","tf-idf","math-domain","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}