npm

Don't miss out on this year's npm Pride t-shirt designs!Order your favorites here »

@petamoriken/float16

1.1.0 • Public • Published

@petamoriken/float16

half precision floating point for JavaScript
see ES Discuss Float16Array topic

license GitHub issues npm npm downloads Travis Greenkeeper badge dependencies Status devDependencies Status

Sauce Labs browser matrix

Supports (at least)

This library's Float16Array uses Proxy object, so IE11 is never supported.

/lib and /browser directories in the npm package have JavaScript files already built, whose target are

  • Firefox: last 2 versions and ESR
  • Chrome: last 2 versions
  • Edge: last 2 versions
  • Safari: last 2 versions
  • Node.js: latest version

When you build by yourself using webpack or rollup.js, please transpile JavaScript files in /src directory.

Install

yarn add @petamoriken/float16

or

npm install @petamoriken/float16 --save

Require

npm, yarn package

// ES6 Modules
import { Float16Array, getFloat16, setFloat16, hfround } from "@petamoriken/float16";

or

// CommonJS
const { Float16Array, getFloat16, setFloat16, hfround } = require("@petamoriken/float16");

Browser

Copy the build file for browsers to your project directory.

<script src="DEST/TO/float16.js"></script>
<script>
    const { Float16Array, getFloat16, setFloat16, hfround } = float16;
</script> 

API

  • Float16Array

    This API is similar to TypedArray such as Float32Array.

    const float16 = new Float16Array([1.0, 1.1, 1.2]);
    for(const val of float16) {
        console.log(val); // => 1, 1.099609375, 1.19921875
    }
     
    float16.reduce((prev, current) => prev + current); // 3.298828125
  • getFloat16(view: DataView, byteOffset: number [, littleEndian: boolean])

  • setFloat16(view: DataView, byteOffset: number, value: number [, littleEndian: boolean])

    These APIs are similar to DataView methods such as DataView#getFloat32 and DataView#setFloat32.

    const buffer = new ArrayBuffer(10);
    const view = new DataView(buffer);
     
    view.setUint16(0, 0x1234);
    getFloat16(view, 0); // 0.0007572174072265625
     
    // You can append to DataView instance
    view.getFloat16 = (...args) => getFloat16(view, ...args);
    view.setFloat16 = (...args) => setFloat16(view, ...args);
     
    view.getFloat16(0); // 0.0007572174072265625
     
    view.setFloat16(0, Math.PI, true);
    view.getFloat16(0, true); // 3.140625
  • hfround(x: number)

    This API is similar to Math.fround (MDN). This function returns nearest half precision float representation of a number.

    Math.fround(1.337); // 1.3370000123977661
    hfround(1.337); // 1.3369140625

Build

First, download devDependencies.

yarn

Build lib, browser files.

yarn run build

Build docs files (for browser test).

yarn run docs

Bug

Some browsers have JavaScript bugs. Please see Float16Array test code and search this.skip().

Test

First, download devDependencies.

yarn

Node.js Test

yarn test

Browser Test

Access test page (power-assert version).

install

npm i @petamoriken/float16

Downloadsweekly downloads

107

version

1.1.0

license

MIT

homepage

git.io

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability