Skip to content

Commit f1eda5c

Browse files
psnettoji
authored andcommitted
for common.glMatrix equals method added optional param tolerance by default equal to EPSILON
1 parent 1ebfe2b commit f1eda5c

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ When adding new functionality, please explain why and when the functionality is
66

77
Please provide unit tests for new functionality. (See [TESTING.md](./TESTING.md) for details)
88

9-
Also, avoid commiting [rebuilt docs](https://github.com/toji/gl-matrix/tree/master/dist) and [files in the dist folder](https://github.com/toji/gl-matrix/tree/master/dist)
9+
Also, avoid commiting [rebuilt docs](https://github.com/toji/gl-matrix/tree/master/docs) and [files in the dist folder](https://github.com/toji/gl-matrix/tree/master/dist)
1010
to prevent merge conflicts.

spec/gl-matrix/common-spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ describe("common", function(){
1414
});
1515

1616
describe("equals", function() {
17-
let r0, r1, r2;
17+
let r0, r1, r2, r3, r4;
1818
beforeEach(function() {
1919
r0 = glMatrix.equals(1.0, 0.0);
2020
r1 = glMatrix.equals(1.0, 1.0);
2121
r2 = glMatrix.equals(1.0+glMatrix.EPSILON/2, 1.0);
22+
r3 = glMatrix.equals(1.0+glMatrix.EPSILON/2, 1.0, 0.001);
23+
r4 = glMatrix.equals(100.5, 100.7, 0.2);
2224
});
2325
it("should return false for different numbers", function() { expect(r0).toBe(false); });
2426
it("should return true for the same number", function() { expect(r1).toBe(true); });
2527
it("should return true for numbers that are close", function() { expect(r2).toBe(true); });
28+
it("should return false for numbers that are close but tolerance is set to smaller value", function() { expect(r3).toBe(false); });
29+
it("should return true for numbers that are close with tolerance is set to bigger value", function() { expect(r4).toBe(true); });
2630
});
2731

2832
});

src/common.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ export function toDegree(a) {
5959
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less
6060
* than or equal to 1.0, and a relative tolerance is used for larger values)
6161
*
62-
* @param {Number} a The first number to test.
63-
* @param {Number} b The second number to test.
62+
* @param {Number} a The first number to test.
63+
* @param {Number} b The second number to test.
64+
* @param {Number} tolerance Absolute or relative tolerance (default glMatrix.EPSILON)
6465
* @returns {Boolean} True if the numbers are approximately equal, false otherwise.
6566
*/
66-
export function equals(a, b) {
67-
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
67+
export function equals(a, b, tolerance = EPSILON) {
68+
return Math.abs(a - b) <= tolerance * Math.max(1, Math.abs(a), Math.abs(b));
6869
}

0 commit comments

Comments
 (0)