karatelabs/karate · error · JsErrorException

value must be >= 1

Error message

value must be >= 1

What it means

Range validation in a JsMath method that requires a lower bound of 1 for its numeric argument (input too small would be mathematically invalid or out of domain). Fires when the argument is below 1; pass a value >= 1.

Solutions

  1. Clamp the input: `Math.acosh(Math.max(1, x))`
  2. Guard the call: `x >= 1 ? Math.acosh(x) : NaN` (matching the IEEE NaN result)
  3. Re-examine the formula — if values below 1 are expected, you likely want Math.log(x + Math.sqrt(x*x + 1)) i.e. Math.asinh, or Math.acosh(1 + x)
  4. Fix upstream calculations that should produce values >= 1

Example fix

// before
const r = Math.acosh(x); // RangeError when x < 1
// after
const r = x >= 1 ? Math.acosh(x) : NaN; // IEEE-consistent fallback
Defensive patterns

Strategy: validation

Validate before calling

if (!(x >= 1)) { throw new Error('Math.acosh requires x >= 1, got ' + x); }

Type guard

function acoshSafe(x) { return (typeof x === 'number' && x >= 1) ? Math.acosh(x) : NaN; }

Try / catch

try {
  r = Math.acosh(x);
} catch (e) {
  if (String(e.message).includes('value must be >= 1')) {
    r = NaN; // IEEE NaN semantics
  } else throw e;
}

Prevention

When it happens

Trigger: `Math.acosh(0)`, `Math.acosh(0.5)`, `Math.acosh(-3)`; arguments computed from other expressions that dip below 1; NaN-producing input pipelines feeding acosh with sub-1 values.

Common situations: Inverse-hyperbolic math on physical quantities that can be 0 or negative (distances, temperatures as deltas); interpolation/normalization code producing values in (0,1); unit conversion mistakes.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/01ac52a6a14d4b3d. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsMath.java:67

    private static final byte METHOD_ATTRS = WRITABLE | CONFIGURABLE | PropertySlot.INTRINSIC;

    JsMath() {
        // Constants — ES §21.3.1.
        defineOwn("E", Math.E, CONSTANT_ATTRS);
        defineOwn("LN10", Math.log(10), CONSTANT_ATTRS);
        defineOwn("LN2", Math.log(2), CONSTANT_ATTRS);
        defineOwn("LOG2E", 1 / Math.log(2), CONSTANT_ATTRS);
        defineOwn("LOG10E", 1 / Math.log(10), CONSTANT_ATTRS);
        defineOwn("PI", Math.PI, CONSTANT_ATTRS);
        defineOwn("SQRT1_2", Math.sqrt(0.5), CONSTANT_ATTRS);
        defineOwn("SQRT2", Math.sqrt(2), CONSTANT_ATTRS);

        // Methods — ES §21.3.2.
        installMethod("abs", 1, math(Math::abs));
        installMethod("acos", 1, math(Math::acos));
        installMethod("acosh", 1, math(x -> {
            if (x < 1) {
                throw JsErrorException.rangeError("value must be >= 1");
            }
            return Math.log(x + Math.sqrt(x * x - 1));
        }));
        installMethod("asin", 1, math(Math::asin));
        installMethod("asinh", 1, math(x -> {
            // Spec §21.3.2.5: ±0 / ±Inf / NaN return the argument unchanged.
            // The naive `log(x + sqrt(x*x + 1))` form yields NaN for -Inf
            // (Inf + (-Inf)) and loses the sign of zero.
            if (Double.isNaN(x) || x == 0 || Double.isInfinite(x)) return x;
            return Math.log(x + Math.sqrt(x * x + 1));
        }));
        installMethod("atan", 1, math(Math::atan));
        installMethod("atan2", 2, math(Math::atan2));
        installMethod("atanh", 1, math(x -> {
            // Spec §21.3.2.7: ±1 → ±Infinity; |x| > 1 → NaN; ±0 preserved.
            if (Double.isNaN(x)) return Double.NaN;
            if (x > 1 || x < -1) return Double.NaN;
            if (x == 1) return Double.POSITIVE_INFINITY;

View on GitHub (pinned to a22eb90246)