Skip to content

Commit

Permalink
Rename hfround to f16round (treat hfround as alias) (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken authored Mar 26, 2023
1 parent b3dfe64 commit e362f84
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 87 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bun add @petamoriken/float16
import {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} from "@petamoriken/float16";
```

Expand All @@ -72,7 +72,7 @@ import {
const {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} = require("@petamoriken/float16");
```

Expand All @@ -85,7 +85,7 @@ service.
import {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} from "https://deno.land/x/float16/mod.ts";
```

Expand All @@ -100,7 +100,7 @@ from your Web server with the JavaScript `Content-Type` HTTP header.
import {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} from "DEST/TO/float16.mjs";
</script>
```
Expand All @@ -112,7 +112,7 @@ from your Web server with the JavaScript `Content-Type` HTTP header.
const {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} = float16;
</script>
```
Expand All @@ -126,7 +126,7 @@ from your Web server with the JavaScript `Content-Type` HTTP header.
import {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm";
</script>
```
Expand All @@ -138,7 +138,7 @@ from your Web server with the JavaScript `Content-Type` HTTP header.
const {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} = float16;
</script>
```
Expand Down Expand Up @@ -250,19 +250,19 @@ view.setFloat16(0, Math.PI, true);
view.getFloat16(0, true); // 3.140625
```

### `hfround`
### `f16round` (`hfround`)

`hfround` is similar to `Math.fround`
`f16round` is similar to `Math.fround`
([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround)).
This function returns nearest half-precision float representation of a number.

```ts
declare function hfround(x: number): number;
declare function f16round(x: number): number;
```

```js
Math.fround(1.337); // 1.3370000123977661
hfround(1.337); // 1.3369140625
f16round(1.337); // 1.3369140625
```

## `Float16Array` limitations (edge cases)
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,11 @@ export declare function setFloat16(
* Returns the nearest half-precision float representation of a number.
* @param x A numeric expression.
*/
export declare function f16round(x: number): number;

/**
* Returns the nearest half-precision float representation of a number.
* @alias f16round
* @param x A numeric expression.
*/
export declare function hfround(x: number): number;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"DataView",
"getFloat16",
"setFloat16",
"hfround",
"f16round",
"ponyfill",
"shim"
],
Expand Down
2 changes: 1 addition & 1 deletion src/hfround.mjs → src/f16round.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NumberIsFinite } from "./_util/primordials.mjs";
* @param {number} x
* @returns {number}
*/
export function hfround(x) {
export function f16round(x) {
const number = +x;

// for optimization
Expand Down
2 changes: 1 addition & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
export { Float16Array, isFloat16Array } from "./Float16Array.mjs";
export { isTypedArray } from "./isTypedArray.mjs";
export { getFloat16, setFloat16 } from "./DataView.mjs";
export { hfround } from "./hfround.mjs";
export { f16round, f16round as hfround } from "./f16round.mjs";
2 changes: 1 addition & 1 deletion test/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<script src="Float16Array.js"></script>
<script src="DataView.js"></script>
<script src="hfround.js"></script>
<script src="f16round.js"></script>
<script src="isTypedArray.js"></script>

<script class="mocha-exec">
Expand Down
2 changes: 1 addition & 1 deletion test/browser/power.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<script src="Float16Array.power.js"></script>
<script src="DataView.power.js"></script>
<script src="hfround.power.js"></script>
<script src="f16round.power.js"></script>
<script src="isTypedArray.power.js"></script>

<script class="mocha-exec">
Expand Down
69 changes: 69 additions & 0 deletions test/f16round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
describe("f16round()", () => {
const maxFloat16 = 65504;
const minFloat16 = 2 ** -24;

it("property `name` is 'f16round'", () => {
assert(f16round.name === "f16round");
});

it("property `length` is 1", () => {
assert(f16round.length === 1);
});

it("return NaN when value is empty or undefined or NaN", () => {
assert(Number.isNaN(f16round()));
assert(Number.isNaN(f16round(undefined)));
assert(Number.isNaN(f16round(NaN)));
});

it("return 0 when value is 0 or null", () => {
assert(Object.is(f16round(0), 0));
assert(Object.is(f16round(null), 0));
});

it("return -0 when value is -0", () => {
assert(Object.is(f16round(-0), -0));
});

it("return ±Infinity when value is ±Infinity", () => {
assert(f16round(Infinity) === Infinity);
assert(f16round(-Infinity) === -Infinity);
});

it("return ±Infinity when value is ±Number.MAX_VALUE", () => {
assert(f16round(Number.MAX_VALUE) === Infinity);
assert(f16round(-Number.MAX_VALUE) === -Infinity);
});

it("return ±0 when value is ±Number.MIN_VALUE", () => {
assert(Object.is(f16round(Number.MIN_VALUE), 0));
assert(Object.is(f16round(-Number.MIN_VALUE), -0));
});

it("return same value when value is ±float16 max/min value", () => {
assert(f16round(maxFloat16) === maxFloat16);
assert(f16round(-maxFloat16) === -maxFloat16);
assert(f16round(minFloat16) === minFloat16);
assert(f16round(-minFloat16) === -minFloat16);
});

it("return 0 when value is ±float16 min value / 2", () => {
assert(Object.is(f16round(minFloat16 / 2), 0));
assert(Object.is(f16round(-minFloat16 / 2), -0));
});

it("return ±float16 min value when value is ±float16 min value / 2 ± a bit number", () => {
assert(f16round(minFloat16 / 2 + 2 ** -25) === minFloat16);
assert(f16round(-minFloat16 / 2 - 2 ** -25) === -minFloat16);
});

it("return 1.3369140625 when value is 1.337", () => {
assert(f16round(1.337) === 1.3369140625);
});

it("throw TypeError when value is bigint", function () {
assert.throws(() => {
f16round(BigInt(0));
}, TypeError);
});
});
69 changes: 0 additions & 69 deletions test/hfround.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,
hfround,
f16round,
} from "../../src/index.mjs";

const float16 = new Float16Array([1.0, 1.1, 1.2]);
Expand All @@ -21,7 +21,7 @@ const view = new DataView(buffer);
setFloat16(view, 0, Math.PI, true);
assertEqualsTrue(getFloat16(view, 0, true) === 3.140625);

assertEqualsTrue(hfround(1.337) === 1.3369140625);
assertEqualsTrue(f16round(1.337) === 1.3369140625);

function assertEqualsTrue(target) {
if (target !== true) {
Expand Down

0 comments on commit e362f84

Please sign in to comment.