{"record":{"id":"19b7a570373e130b","repo":"TheAlgorithms/Go","slug":"negative-exponent-provided","errorCode":null,"errorMessage":"negative Exponent provided","messagePattern":"negative Exponent provided","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/modular/exponentiation.go","lineNumber":21,"sourceCode":"// details:\n// A simple implementation of Modular Exponentiation - [Modular Exponenetation wiki](https://en.wikipedia.org/wiki/Modular_exponentiation)\n// time complexity: O(log(n)) where n is the exponent\n// space complexity: O(1)\n// author(s) [Taj](https://github.com/tjgurwara99)\n// see exponentiation_test.go\n\npackage modular\n\nimport (\n\t\"errors\"\n\t\"math\"\n)\n\n// ErrorIntOverflow For asserting that the values do not overflow in Int64\nvar ErrorIntOverflow = errors.New(\"integer overflow\")\n\n// ErrorNegativeExponent for asserting that the exponent we receive is positive\nvar ErrorNegativeExponent = errors.New(\"negative Exponent provided\")\n\n// Exponentiation returns base^exponent % mod\nfunc Exponentiation(base, exponent, mod int64) (int64, error) {\n\tif mod == 1 {\n\t\treturn 0, nil\n\t}\n\n\tif exponent < 0 {\n\t\treturn -1, ErrorNegativeExponent\n\t}\n\t_, err := Multiply64BitInt(mod-1, mod-1)\n\n\tif err != nil {\n\t\treturn -1, err\n\t}\n\n\tvar result int64 = 1\n","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/modular/exponentiation.go#L3-L39","documentation":"ErrorNegativeExponent is returned by modular.Exponentiation when the exponent argument is negative (math/modular/exponentiation.go:29-31). The library only implements fast modular exponentiation for non-negative exponents; a negative exponent would require a modular multiplicative inverse, which this function does not compute, so it refuses the input with -1 and this sentinel error (declared at line 21).","triggerScenarios":"Calling modular.Exponentiation(base, exponent, mod) with exponent < 0, e.g. Exponentiation(2, -3, 5). Any code path that passes a user-supplied or computed int64 exponent that can go negative (subtraction, unvalidated input).","commonSituations":"Developers assuming the function handles modular inverses for negative exponents like math/big's Exp does not either; parsing exponents from config/CLI as signed integers where a '-' slips in; computing exponent as a difference (a-b) that turns out negative.","solutions":["Check the exponent before calling: if exponent < 0, either reject the input or compute base^(-exponent) mod m via modular.Exponentiation followed by modular.Inverse of the result mod m","Validate/sanitize the exponent at the input boundary (e.g., parse as unsigned or clamp negatives to an error in your own code)","If a negative exponent is legitimate for your use case, use math/big's ModInverse together with a positive power instead"],"exampleFix":"// before\nresult, err := modular.Exponentiation(base, exp, mod) // panics-free but errors when exp < 0\n// after\nif exp < 0 {\n    return 0, fmt.Errorf(\"exponent must be non-negative, got %d\", exp)\n}\nresult, err := modular.Exponentiation(base, exp, mod)","handlingStrategy":"validation","validationCode":"func validExponent(exp int64) bool { return exp >= 0 }\nif !validExponent(exp) { return 0, fmt.Errorf(\"exponent must be >= 0, got %d\", exp) }","typeGuard":"func isNonNegative(n int64) bool { return n >= 0 }","tryCatchPattern":"result, err := modular.Exponentiation(base, exp, mod)\nif err != nil {\n    if errors.Is(err, modular.ErrorNegativeExponent) {\n        // handle negative exponent: invert via modular.Inverse or reject input\n    }\n    return 0, err\n}","preventionTips":["Validate exponent >= 0 at the API boundary before doing math","Parse user-supplied exponents as unsigned integers where possible","Compute exponent differences with explicit checks that the result is non-negative","Add a unit test covering a negative-exponent input"],"tags":["go","math","modular-arithmetic","invalid-input"],"backgroundTag":"negative-exponent-argument","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}