JierunChen's picture
Add files using upload-large-folder tool
dbae350 verified
|
Raw History Blame Contribute Delete
2.88 kB

Missing Math Filters from jq's Standard Library

According to jq’s manual, a wide set of math functions should be available as filters that operate on numbers. Currently, jaq lacks most of these, making it impossible to perform common mathematical operations.

Observed behavior
Attempting to use any of the missing math filters results in an error (e.g., jaq: error: … unknown function …). For example:

$ echo '0' | jaq 'sin | .*.*.'
error: unknown function sin

Expected: 0 should be returned.

Expected behavior
The following filters from jq’s standard library should be available:

  • Zero‑argument filters (operate on the current value .):

    • sqrt, cbrt, abs
    • Trigonometric: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh
    • Exponential/logarithmic: exp, exp10, exp2, expm1, log, log10, log2, log1p
    • Rounding/classification: fabs, floor, ceil, trunc, round, nearbyint, rint, lrint, llrint, signbit, ilogb, logb, significand
    • Other: erf, erfc, j0, j1, y0, y1, lgamma, tgamma, modf, frexp
  • Two‑argument filters (ignore .):

    • pow, atan2, hypot, nextafter, nexttoward, fdim, fmax, fmin, fmod, copysign, scalbn, scalbln, drem, remainder, jn, ldexp, yn
  • Three‑argument filters (ignore .):

    • fma (fused multiply‑add)
    • remquo (remainder and quotient)
  • Additional filters defined in jq but not directly provided by the underlying math library:

    • infinite, isinfinite, isnan, isnormal, pow10, scalb, scalbln, significand, logb, drem, from (these should be implemented as well).

Acceptance criteria

  1. When applied to numbers, the filters return mathematically correct results. Examples:

    • 0 | sin | .*.*. → 0
    • 4 | sqrt → 2
    • pow(2; 10) → 1024
    • drem(3.5, -4; 6, 1.75, 2) → [-2.5, 0.0, -0.5, 2.0, -0.5, -0.0]
  2. Filters that cannot be implemented (e.g., because the underlying platform lacks support) must still be defined but raise an error when called, matching jq’s own behavior for unavailable functions.

  3. Numeric filters that accept integers must enforce integer arguments where appropriate (e.g., jn and yn take an integer as first argument; ldexp and scalbln take an integer as second argument).

Note
The filters should be discovered and usable without any special import; they are part of the core runtime.

Important: Solve this task using only the repository, files, documentation, tests, and tools available in the provided task environment. Do not use the internet or any external search service to look up this issue, related pull requests, patches, commits, discussions, or ready-made solutions. Derive the solution independently from the local evidence.