{"record":{"id":"55df5fdf17e969e1","repo":"gchq/CyberChef","slug":"modulus-cannot-be-zero","errorCode":null,"errorMessage":"Modulus cannot be zero","messagePattern":"Modulus cannot be zero","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/operations/MOD.mjs","lineNumber":51,"sourceCode":"            {\n                \"name\": \"Delimiter\",\n                \"type\": \"option\",\n                \"value\": ARITHMETIC_DELIM_OPTIONS,\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const modulus = new BigNumber(args[0]);\n        const delimiter = args[1];\n\n        if (modulus.isZero()) {\n            throw new Error(\"Modulus cannot be zero\");\n        }\n\n        const numbers = createNumArray(input, delimiter);\n        const results = numbers.map(num => num.mod(modulus));\n\n        return results.join(\" \");\n    }\n\n}\n\nexport default MOD;\n","sourceCodeStart":33,"sourceCodeEnd":63,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/MOD.mjs#L33-L63","documentation":"Thrown by the MOD operation when the 'Modulus' argument (args[0]) evaluates to zero. Division/modulo by zero is undefined, so after constructing a BigNumber from args[0] the operation checks modulus.isZero() and throws a plain Error (not OperationError) at MOD.mjs:51. The default modulus is 2. Note: because this is a plain Error, recipe execution may treat it differently from OperationError.","triggerScenarios":"Running MOD with the Modulus argument set to 0 (or a string BigNumber parses as zero, e.g. '0', '0.0', '-0'). Raised at MOD.mjs:50-52 before any numbers are read from the input.","commonSituations":"Recipe imported with modulus 0; user cleared the modulus field or typed 0 expecting 'no modulo'; a preceding operation produced 0 and it was wired into the modulus (uncommon, since modulus is an arg not the input).","solutions":["Set the Modulus argument to a non-zero number (the default 2 works for parity checks).","If you derived the modulus programmatically, guard against zero before invoking MOD.","Wrap the call in try/catch — note this op throws a plain Error, not OperationError."],"exampleFix":"// before\nmod.run('15 4 7', [0, 'Space']);   // modulus zero -> throws\n\n// after\nmod.run('15 4 7', [3, 'Space']);    // modulus 3 -> '0 1 1'","handlingStrategy":"validation","validationCode":"import BigNumber from 'bignumber.js';\nfunction nonZeroModulus(m) {\n  const mod = new BigNumber(m);\n  if (mod.isZero() || !mod.isFinite()) {\n    throw new Error('Modulus must be a non-zero finite number');\n  }\n  return mod;\n}\nconst modulus = nonZeroModulus(args[0]);","typeGuard":"import BigNumber from 'bignumber.js';\nfunction isNonZeroModulus(v: unknown): v is string|number {\n  try {\n    const b = new BigNumber(v as any);\n    return b.isFinite() && !b.isZero();\n  } catch { return false; }\n}","tryCatchPattern":"try {\n  mod.run(input, [modulusArg, delim]);\n} catch (e) {\n  // MOD throws a plain Error, not OperationError\n  if (e instanceof Error && /Modulus cannot be zero/.test(e.message)) {\n    // fix the modulus and retry\n  } else throw e;\n}","preventionTips":["Default the modulus to 2 (or another non-zero value); never 0.","Validate modulus != 0 before running — note MOD throws a plain Error.","When deriving modulus from upstream data, guard against a zero result."],"tags":["mod","arithmetic","validation","divide-by-zero","number-format"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}