Skip to content

Commit 1a891dd

Browse files
Merge pull request #115 from Exabyte-io/feature/SOF-7597
feature/SOF 7597
2 parents e34d1f0 + 83d087e commit 1a891dd

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

.github/workflows/cicd.yml

-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ jobs:
6666
strategy:
6767
matrix:
6868
node-version:
69-
- 14.x
7069
- 20.x
7170
- 22.x
7271

@@ -116,7 +115,6 @@ jobs:
116115
- name: Publish JS release
117116
uses: ./actions/js/publish
118117
with:
119-
node-version: 14.19.x
120118
npm-token: ${{ secrets.NPM_TOKEN }}
121119
github-token: ${{ secrets.BOT_GITHUB_TOKEN }}
122120

src/js/math.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ const EPSILON = 1e-8;
4343
* @param v2 Vector 2
4444
*/
4545
const product = (v1: number[], v2: number[]) => {
46-
return math.multiply(v1, math.transpose(v2));
46+
if (v1.length !== v2.length) {
47+
throw new Error("Vectors must be of the same length");
48+
}
49+
return Number(math.multiply(v1, math.transpose(v2)));
4750
};
4851

4952
/**
@@ -217,15 +220,15 @@ const roundValueToNDecimals = (value: number, decimals = 3) => {
217220
};
218221

219222
// See: https://en.wikipedia.org/wiki/Rounding
220-
export enum RoundingMethod {
223+
export enum RoundingMethodEnum {
221224
Bankers = "bankers",
222225
HalfAwayFromZero = "halfAwayFromZero",
223226
}
224227

225228
export const roundCustom = (
226229
value: number,
227230
decimals = 0,
228-
method = RoundingMethod.HalfAwayFromZero,
231+
method = RoundingMethodEnum.HalfAwayFromZero,
229232
) => {
230233
const factor = Math.pow(10, decimals);
231234
const scaledValue = value * factor;
@@ -235,10 +238,10 @@ export const roundCustom = (
235238
let roundedAbs: number;
236239

237240
switch (method) {
238-
case RoundingMethod.HalfAwayFromZero:
241+
case RoundingMethodEnum.HalfAwayFromZero:
239242
roundedAbs = Math.round(absValue);
240243
break;
241-
case RoundingMethod.Bankers:
244+
case RoundingMethodEnum.Bankers:
242245
const floorValue = Math.floor(absValue);
243246
const fractional = absValue - floorValue;
244247

@@ -321,5 +324,5 @@ export const math = {
321324
roundValueToNDecimals,
322325
numberToPrecision,
323326
roundCustom,
324-
RoundingMethod,
327+
RoundingMethod: RoundingMethodEnum,
325328
};

tests/js/math.ts

+48-23
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,55 @@
11
import { expect } from "chai";
22

3-
import { math } from "../../src/js/math";
3+
import { math, RoundingMethodEnum } from "../../src/js/math";
44

55
describe("Math Tests", () => {
6-
it("should round according to specified method", () => {
7-
const [value1, value2, value3, value4] = [0.5, 1.5, -0.5, -1.5];
8-
const [expectedBankers1, expectedBankers2, expectedBankers3, expectedBankers4] = [
9-
0, 2, 0, -2,
10-
];
11-
const [expectedAway1, expectedAway2, expectedAway3, expectedAway4] = [1, 2, -1, -2];
6+
it("should roundCustom according to specified method", () => {
7+
const values = [0.5, 1.5, -0.5, -1.5];
8+
const expectedBankers = [0, 2, 0, -2];
9+
const expectedHalfAwayFromZero = [1, 2, -1, -2];
1210
const n = 0;
13-
expect(math.roundCustom(value1, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers1);
14-
expect(math.roundCustom(value2, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers2);
15-
expect(math.roundCustom(value3, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers3);
16-
expect(math.roundCustom(value4, n, math.RoundingMethod.Bankers)).to.equal(expectedBankers4);
17-
expect(math.roundCustom(value1, n, math.RoundingMethod.HalfAwayFromZero)).to.equal(
18-
expectedAway1,
19-
);
20-
expect(math.roundCustom(value2, n, math.RoundingMethod.HalfAwayFromZero)).to.equal(
21-
expectedAway2,
22-
);
23-
expect(math.roundCustom(value3, n, math.RoundingMethod.HalfAwayFromZero)).to.equal(
24-
expectedAway3,
25-
);
26-
expect(math.roundCustom(value4, n, math.RoundingMethod.HalfAwayFromZero)).to.equal(
27-
expectedAway4,
28-
);
11+
12+
expectedBankers.forEach((expected, i) => {
13+
const result = math.roundCustom(values[i], n, RoundingMethodEnum.Bankers);
14+
expect(result).to.equal(expected);
15+
});
16+
expectedHalfAwayFromZero.forEach((expected, i) => {
17+
const result = math.roundCustom(values[i], n, RoundingMethodEnum.HalfAwayFromZero);
18+
expect(result).to.equal(expected);
19+
});
20+
});
21+
22+
it("should calculate vector length", () => {
23+
const vector = [3, 4, 0];
24+
const expectedLength = 5;
25+
expect(math.vlen(vector)).to.equal(expectedLength);
26+
});
27+
28+
it("should calculate angle between vectors", () => {
29+
const vectorA = [1, 0, 0];
30+
const vectorB = [0, 1, 0];
31+
const expectedAngle = 90;
32+
expect(math.angle(vectorA, vectorB, "deg")).to.equal(expectedAngle);
33+
});
34+
35+
it("should calculate distance between vectors", () => {
36+
const vectorA = [1, 2, 3];
37+
const vectorB = [4, 5, 6];
38+
const expectedDistance = 5.196152422706632;
39+
expect(math.vDist(vectorA, vectorB)).to.equal(expectedDistance);
40+
});
41+
42+
it("should check if vectors are equal within tolerance", () => {
43+
const vectorA = [1, 2, 3];
44+
const vectorB = [1.0001, 2.0001, 3.0001];
45+
const tolerance = 0.001;
46+
expect(math.vEqualWithTolerance(vectorA, vectorB, tolerance)).to.equal(true);
47+
});
48+
49+
it("should check if vectors are not equal within tolerance", () => {
50+
const vectorA = [1, 2, 3];
51+
const vectorB = [1.1, 2.1, 3.1];
52+
const tolerance = 0.001;
53+
expect(math.vEqualWithTolerance(vectorA, vectorB, tolerance)).to.equal(false);
2954
});
3055
});

0 commit comments

Comments
 (0)