Skip to content

Commit

Permalink
revised cast.js cast_test.js and utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mei1127 committed Dec 19, 2023
1 parent 1d3ff60 commit 648dbc3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ export function cast(input, type) {
let outputArray;
switch (type) {
case 'int8':
outputArray = new Int8Array(input.data);
outputArray = new Int8Array(Array.from(input.data, (num) => (Math.round(num))));
break;
case 'uint8':
outputArray = new Uint8Array(input.data);
outputArray = new Uint8Array(Array.from(input.data, (num) => (Math.round(num))));
break;
case 'int32':
outputArray = new Int32Array(input.data);
outputArray = new Int32Array(Array.from(input.data, (num) => (Math.round(num))));
break;
case 'uint32':
outputArray = new Uint32Array(input.data);
outputArray = new Uint32Array(Array.from(input.data, (num) => (Math.round(num))));
break;
case 'int64':
outputArray = new BigInt64Array(Array.from(input.data, (num) => BigInt(Math.round(num))));
Expand Down
20 changes: 10 additions & 10 deletions test/cast_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ describe('test cast', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
-0.25, 0.25, 3.75, 14, -14,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, -46, 46,
0, 0, 4, 14, -14,
],
};
testCast(input, 'int8', expected);
Expand All @@ -46,13 +46,13 @@ describe('test cast', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
0.25, 0.75, 3.75, 14, 15,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, 210, 46,
0, 1, 4, 14, 15,
],
};
testCast(input, 'uint8', expected);
Expand All @@ -78,13 +78,13 @@ describe('test cast', function() {
const input = {
shape: [5],
data: [
-0.25, 0.25, 3.21, 1234, -1234,
0.75, 0.25, 3.21, 14, 15,
],
};
const expected = {
shape: [5],
data: [
0, 0, 3, 1234, 4294966062,
1, 0, 3, 14, 15,
],
};
testCast(input, 'uint32', expected);
Expand Down Expand Up @@ -126,14 +126,14 @@ describe('test cast', function() {
const input = {
shape: [5],
data: [
0, 1, 2, 3, 3,
0, 1, -2, -3, 3,
],
type: 'int32',
};
const expected = {
shape: [5],
data: [
0, 1, 2, 3, 3,
0, 1, -2, -3, 3,
],
};
testCast(input, 'float32', expected);
Expand All @@ -160,14 +160,14 @@ describe('test cast', function() {
const input = {
shape: [5],
data: [
0, 0.1, 0.2, 3, 993,
0, 0.1, 0.2, -3, 993,
],
type: 'float32',
};
const expected = {
shape: [5],
data: [
0, 0.10000000149011612, 0.20000000298023224, 3, 993,
0, 0.10000000149011612, 0.20000000298023224, -3, 993,
],
};
testCast(input, 'float64', expected);
Expand Down
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ assert.isAlmostEqualUlp = function(a, b, nulp, message) {
distance = distance >= 0 ? distance : -distance;
return assert.isTrue(distance <= nulp, message);
} else {
let distance = a-b;
let distance = a - b;
distance = distance >= 0n ? distance : -distance;
return assert.isTrue(distance <= nulp, message);
}
Expand Down

0 comments on commit 648dbc3

Please sign in to comment.