Skip to content

Commit

Permalink
Update hfround to throw TypeError when value is bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Sep 14, 2021
1 parent 8257ac0 commit 3096f45
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/hfround.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/hfround.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-env mocha */
/* eslint-env mocha, es2020 */
/* global assert hfround */

describe("hfround()", () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 3096f45

Please sign in to comment.