From 648dbc32d36cbf5661557bf5384517132765d348 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Tue, 19 Dec 2023 09:33:25 +0800 Subject: [PATCH] revised cast.js cast_test.js and utils.js --- src/cast.js | 8 ++++---- test/cast_test.js | 20 ++++++++++---------- test/utils.js | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cast.js b/src/cast.js index 6d59228..d29a048 100644 --- a/src/cast.js +++ b/src/cast.js @@ -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)))); diff --git a/test/cast_test.js b/test/cast_test.js index 85a2ddf..b6046ee 100644 --- a/test/cast_test.js +++ b/test/cast_test.js @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/test/utils.js b/test/utils.js index b4fed49..f8c22cc 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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); }