Skip to content

Commit 05d2380

Browse files
mreinsteintoji
authored andcommitted
use symmetric half up rounding in all vector modules. fixes #447
1 parent abb59a1 commit 05d2380

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

spec/gl-matrix/vec2-spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ describe("vec2", function() {
259259
it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3]); });
260260
it("should return vecA", function() { expect(result).toBe(vecA); });
261261
});
262+
263+
describe("symmetry", function() {
264+
it("should round negative values torwards negative infinity", function() { expect(vec2.round([], [-1.5, -1.5])).toBeEqualish([-2, -2]); });
265+
});
262266
});
263267

264268
describe("scale", function() {

spec/gl-matrix/vec3-spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ describe("vec3", function() {
400400
it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 1]); });
401401
it("should return vecA", function() { expect(result).toBe(vecA); });
402402
});
403+
404+
describe("symmetry", function() {
405+
it("should round negative values torwards negative infinity", function() { expect(vec3.round([], [-1.5, -1.5, -2.5])).toBeEqualish([-2, -2, -3]); });
406+
});
403407
});
404408

405409
describe("scale", function() {

spec/gl-matrix/vec4-spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ describe("vec4", function() {
260260
it("should place values into vecA", function() { expect(vecA).toBeEqualish([3, 3, 1, 1]); });
261261
it("should return vecA", function() { expect(result).toBe(vecA); });
262262
});
263+
264+
describe("symmetry", function() {
265+
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]); });
266+
});
263267
});
264268

265269
describe("scale", function() {

src/common.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ export let ARRAY_TYPE =
1010
export let RANDOM = Math.random;
1111
export let ANGLE_ORDER = "zyx";
1212

13+
/**
14+
* Symmetric round
15+
* see https://www.npmjs.com/package/round-half-up-symmetric#user-content-detailed-background
16+
*
17+
* @param {Number} a value to round
18+
*/
19+
export function round(a) {
20+
if (a >= 0)
21+
return Math.round(a);
22+
23+
return (a % 0.5 === 0) ? Math.floor(a) : Math.round(a);
24+
}
25+
1326
/**
1427
* Sets the type of array used when creating new vectors and matrices
1528
*

src/vec2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ export function max(out, a, b) {
184184
}
185185

186186
/**
187-
* Math.round the components of a vec2
187+
* symmetric round the components of a vec2
188188
*
189189
* @param {vec2} out the receiving vector
190190
* @param {ReadonlyVec2} a vector to round
191191
* @returns {vec2} out
192192
*/
193193
export function round(out, a) {
194-
out[0] = Math.round(a[0]);
195-
out[1] = Math.round(a[1]);
194+
out[0] = glMatrix.round(a[0]);
195+
out[1] = glMatrix.round(a[1]);
196196
return out;
197197
}
198198

src/vec3.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,16 @@ export function max(out, a, b) {
212212
}
213213

214214
/**
215-
* Math.round the components of a vec3
215+
* symmetric round the components of a vec3
216216
*
217217
* @param {vec3} out the receiving vector
218218
* @param {ReadonlyVec3} a vector to round
219219
* @returns {vec3} out
220220
*/
221221
export function round(out, a) {
222-
out[0] = Math.round(a[0]);
223-
out[1] = Math.round(a[1]);
224-
out[2] = Math.round(a[2]);
222+
out[0] = glMatrix.round(a[0]);
223+
out[1] = glMatrix.round(a[1]);
224+
out[2] = glMatrix.round(a[2]);
225225
return out;
226226
}
227227

src/vec4.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,17 @@ export function max(out, a, b) {
214214
}
215215

216216
/**
217-
* Math.round the components of a vec4
217+
* symmetric round the components of a vec4
218218
*
219219
* @param {vec4} out the receiving vector
220220
* @param {ReadonlyVec4} a vector to round
221221
* @returns {vec4} out
222222
*/
223223
export function round(out, a) {
224-
out[0] = Math.round(a[0]);
225-
out[1] = Math.round(a[1]);
226-
out[2] = Math.round(a[2]);
227-
out[3] = Math.round(a[3]);
224+
out[0] = glMatrix.round(a[0]);
225+
out[1] = glMatrix.round(a[1]);
226+
out[2] = glMatrix.round(a[2]);
227+
out[3] = glMatrix.round(a[3]);
228228
return out;
229229
}
230230

0 commit comments

Comments
 (0)