@rgsoft/math
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -170,2 +170,4 @@ declare class Complex { | ||
export { Complex, Line, Segment, Vector }; | ||
declare function mod(n: number, m: number): number; | ||
export { Complex, Line, Segment, Vector, mod }; |
@@ -18,3 +18,3 @@ "use strict"; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2); | ||
@@ -27,3 +27,4 @@ // src/index.ts | ||
Segment: () => Segment, | ||
Vector: () => Vector | ||
Vector: () => Vector, | ||
mod: () => mod | ||
}); | ||
@@ -404,2 +405,19 @@ module.exports = __toCommonJS(src_exports); | ||
}; | ||
// src/modulo.ts | ||
function mod(n, m) { | ||
if (!Number.isInteger(n)) { | ||
throw new Error(`Non integer received: ${n}`); | ||
} | ||
if (!Number.isInteger(m)) { | ||
throw new Error(`Non integer received: ${m}`); | ||
} | ||
if (m <= 0) { | ||
throw new Error(`Modulo must be positive: ${m}`); | ||
} | ||
while (n < 0) { | ||
n += m; | ||
} | ||
return n % m; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -410,4 +428,5 @@ 0 && (module.exports = { | ||
Segment, | ||
Vector | ||
Vector, | ||
mod | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@rgsoft/math", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Yet another JS math library", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -229,1 +229,22 @@ # math-js | ||
``` | ||
## Modulo | ||
The `mod` function calculates the modular remainder of a number `n` modulo `m`. | ||
The main diference between this function and the remainder operator `%` is that | ||
the `mod` function applies the modular arithmetic strictly. For example | ||
```javascript | ||
console.log(-1 % 4); | ||
``` | ||
will output `-1`. Whereas using the `mod`: | ||
```javascript | ||
console.log(mod(-1, 4)); | ||
``` | ||
we should get `3`. | ||
The `mod` function will fail if it receives a negative modulo or any non-integer | ||
number. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
67388
980
249