languagetool-org/languagetool · error · RuntimeException

Probability must be >= 0:

Error message

Probability must be >= 0: 

What it means

Probability is the value object wrapping an n-gram match probability. Its constructor validates that prob is non-negative and throws if a negative probability is passed, since probabilities below 0 are mathematically invalid and indicate an upstream computation error.

Source

Thrown at languagetool-core/src/main/java/org/languagetool/rules/ngrams/Probability.java:33

 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.rules.ngrams;

/**
 * Probability, but doesn't check for a range of 0 to 1.
 * @since 3.2
 */
public class Probability {

  private final double prob;
  private final float coverage;
  private final long occurrences;

  public Probability(double prob, float coverage, long occurrences) {
    if (prob < 0.0) {
      throw new RuntimeException("Probability must be >= 0: " + prob);
    }
    //if (prob > 1.0) {
    //  throw new RuntimeException("Probability must be 0.0 to 1.0: " + prob);
    //}
    this.prob = prob;
    this.coverage = coverage;
    this.occurrences = occurrences;
  }

  public Probability(double prob, float coverage) {
    this(prob, coverage, -1);
  }

  /**
   * A probability-like value, but can be filled with any float &gt;= 0.
   */
  public double getProb() {
    return prob;

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Clamp the computed probability with Math.max(0.0, prob) before constructing Probability
  2. Inspect the language-model lookup that produced the negative value (log-to-linear conversion, interpolation weights)
  3. Update to a fixed LanguageModelUtils/lucene-index version if the negative value comes from library code
  4. Fix test data if the negative probability originates from a mock or fixture

Example fix

// before
return new Probability(prob, coverage, occurrences);
// after
return new Probability(Math.max(0.0, prob), coverage, occurrences);
Defensive patterns

Strategy: try-catch

Validate before calling

if (prob < 0.0) {
  throw new IllegalArgumentException("prob must be >= 0");
}

Type guard

boolean isValidProbability(double p) {
  return !Double.isNaN(p) && p >= 0.0;
}

Try / catch

try {
  return new Probability(prob, coverage, occurrences);
} catch (RuntimeException e) {
  if (e.getMessage().startsWith("Probability must be >= 0")) {
    return new Probability(0.0, coverage, occurrences);
  }
  throw e;
}

Prevention

When it happens

Trigger: Constructing new Probability(...) with a negative prob value — typically a double returned by LanguageModelUtils n-gram lookups (e.g. from log-space conversions or a misbehaving language model) that came back negative.

Common situations: Custom language model implementations returning negative values, interpolation/smoothing bugs producing negative interpolated probabilities, or unit tests feeding invalid data.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/bf115547b9d9aba7. Report an issue: GitHub.