diff --git a/src/hfround.mjs b/src/hfround.mjs index 9276114b..8f975962 100644 --- a/src/hfround.mjs +++ b/src/hfround.mjs @@ -6,6 +6,10 @@ import { convertToNumber, roundToFloat16Bits } from "./helper/converter.mjs"; * @returns {number} */ export function hfround(num) { + if (typeof num === "bigint") { + throw TypeError("Cannot convert a BigInt value to a number"); + } + num = Number(num); // for optimization diff --git a/test/hfround.js b/test/hfround.js index 3394818a..4355a439 100644 --- a/test/hfround.js +++ b/test/hfround.js @@ -1,4 +1,4 @@ -/* eslint-env mocha */ +/* eslint-env mocha, es2020 */ /* global assert hfround */ describe("hfround()", () => { @@ -64,4 +64,13 @@ describe("hfround()", () => { it("return 1.3369140625 when value is 1.337", () => { assert( hfround(1.337) === 1.3369140625 ); }); + + it("throw TypeError when value is bigint", function () { + // Safari 13 doesn't have BigInt + if (typeof BigInt === "undefined") { + this.skip(); + } + + assert.throws(() => { hfround(BigInt(0)); }, TypeError); + }); });