Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/gl-matrix/vec2-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ describe("vec2", function() {
it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3]); });
it("should return vecA", function() { expect(result).toBe(vecA); });
});

describe("symmetry", function() {
it("should round negative values torwards negative infinity", function() { expect(vec2.round([], [-1.5, -1.5])).toBeEqualish([-2, -2]); });
});
});

describe("scale", function() {
Expand Down
4 changes: 4 additions & 0 deletions spec/gl-matrix/vec3-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ describe("vec3", function() {
it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 1]); });
it("should return vecA", function() { expect(result).toBe(vecA); });
});

describe("symmetry", function() {
it("should round negative values torwards negative infinity", function() { expect(vec3.round([], [-1.5, -1.5, -2.5])).toBeEqualish([-2, -2, -3]); });
});
});

describe("scale", function() {
Expand Down
4 changes: 4 additions & 0 deletions spec/gl-matrix/vec4-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ describe("vec4", function() {
it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 1, 1]); });
it("should return vecA", function() { expect(result).toBe(vecA); });
});

describe("symmetry", function() {
it("should round negative values torwards negative infinity", function() { expect(vec4.round([], [-1.5, -1.5, -2.5, -0.5 ])).toBeEqualish([-2, -2, -3, -1]); });
});
});

describe("scale", function() {
Expand Down
13 changes: 13 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ export let ARRAY_TYPE =
export let RANDOM = Math.random;
export let ANGLE_ORDER = "zyx";

/**
* Symmetric round
* see https://www.npmjs.com/package/round-half-up-symmetric#user-content-detailed-background
*
* @param {Number} a value to round
*/
export function round(a) {
if (a >= 0)
return Math.round(a);

return (a % 0.5 === 0) ? Math.floor(a) : Math.round(a);
}

/**
* Sets the type of array used when creating new vectors and matrices
*
Expand Down
6 changes: 3 additions & 3 deletions src/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ export function max(out, a, b) {
}

/**
* Math.round the components of a vec2
* symmetric round the components of a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to round
* @returns {vec2} out
*/
export function round(out, a) {
out[0] = Math.round(a[0]);
out[1] = Math.round(a[1]);
out[0] = glMatrix.round(a[0]);
out[1] = glMatrix.round(a[1]);
return out;
}

Expand Down
8 changes: 4 additions & 4 deletions src/vec3.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ export function max(out, a, b) {
}

/**
* Math.round the components of a vec3
* symmetric round the components of a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to round
* @returns {vec3} out
*/
export function round(out, a) {
out[0] = Math.round(a[0]);
out[1] = Math.round(a[1]);
out[2] = Math.round(a[2]);
out[0] = glMatrix.round(a[0]);
out[1] = glMatrix.round(a[1]);
out[2] = glMatrix.round(a[2]);
return out;
}

Expand Down
10 changes: 5 additions & 5 deletions src/vec4.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@ export function max(out, a, b) {
}

/**
* Math.round the components of a vec4
* symmetric round the components of a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to round
* @returns {vec4} out
*/
export function round(out, a) {
out[0] = Math.round(a[0]);
out[1] = Math.round(a[1]);
out[2] = Math.round(a[2]);
out[3] = Math.round(a[3]);
out[0] = glMatrix.round(a[0]);
out[1] = glMatrix.round(a[1]);
out[2] = glMatrix.round(a[2]);
out[3] = glMatrix.round(a[3]);
return out;
}

Expand Down