-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix round-off errors in
mod()
(#3011)
* changes made to the following files: - mod.js - gcd.js * updated BigNumber implementation and added validating tests * added validating test cases * updated test cases * formatted code * Made updates according to requirement used mathjs floor in mod.js imported mod in gcd.js made mod work for negative divisors wrote and updated tests to validate new behavior * updated mod in arithmetic.js * added tests for modNumber function --------- Co-authored-by: Jos de Jong <[email protected]>
- Loading branch information
1 parent
c35a801
commit 1465ac7
Showing
7 changed files
with
128 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,6 +230,7 @@ Michael Greminger <[email protected]> | |
Kiku <[email protected]> | ||
MaybePixem <[email protected]> | ||
Aly Khaled <[email protected]> | ||
Praise Nnamonu <[email protected]> | ||
BuildTools <[email protected]> | ||
Anik Patel <[email protected]> | ||
Vrushaket Chaudhari <[email protected]> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// test modNumber function in src\plain\number\arithmetic.js | ||
import assert from 'assert' | ||
import { modNumber as mod } from '../../../../lib/cjs/plain/number/aristhmetic' | ||
import approx from '../../../../tools/approx.js' | ||
|
||
const math = require('../../../../lib/cjs/defaultInstance').default | ||
const bignumber = math.bignumber | ||
|
||
describe('mod', function () { | ||
it('should calculate the modulus of booleans correctly', function () { | ||
assert.strictEqual(mod(true, true), 0) | ||
assert.strictEqual(mod(false, true), 0) | ||
assert.strictEqual(mod(true, false), 1) | ||
assert.strictEqual(mod(false, false), 0) | ||
}) | ||
|
||
it('should calculate the modulus of two numbers', function () { | ||
assert.strictEqual(mod(1, 1), 0) | ||
assert.strictEqual(mod(0, 1), 0) | ||
assert.strictEqual(mod(1, 0), 1) | ||
assert.strictEqual(mod(0, 0), 0) | ||
assert.strictEqual(mod(7, 0), 7) | ||
|
||
approx.equal(mod(7, 2), 1) | ||
approx.equal(mod(9, 3), 0) | ||
approx.equal(mod(10, 4), 2) | ||
approx.equal(mod(-10, 4), 2) | ||
approx.equal(mod(8.2, 3), 2.2) | ||
approx.equal(mod(4, 1.5), 1) | ||
approx.equal(mod(0, 3), 0) | ||
approx.equal(mod(-10, 4), 2) | ||
approx.equal(mod(-5, 3), 1) | ||
}) | ||
|
||
it('should handle precise approximation of float approximation', function () { | ||
approx.equal(mod(0.1, 0.01), 0) | ||
approx.equal(mod(0.15, 0.05), 0) | ||
approx.equal(mod(1.23456789, 0.00000000001), 0) | ||
}) | ||
|
||
it('should calculate mod for negative divisor', function () { | ||
assert.strictEqual(mod(10, -4), -2) | ||
}) | ||
|
||
it('should throw an error if used with wrong number of arguments', function () { | ||
assert.throws(function () { mod(1) }, /TypeError: Too few arguments/) | ||
assert.throws(function () { mod(1, 2, 3) }, /TypeError: Too many arguments/) | ||
}) | ||
|
||
it('should throw an error if used with wrong type of arguments', function () { | ||
assert.throws(function () { mod(1, new Date()) }, /TypeError: Unexpected type of argument/) | ||
assert.throws(function () { mod(1, null) }, /TypeError: Unexpected type of argument/) | ||
assert.throws(function () { mod(new Date(), bignumber(2)) }, /TypeError: Unexpected type of argument/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters