Socket
Socket
Sign inDemoInstall

@petamoriken/float16

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@petamoriken/float16 - npm Package Compare versions

Comparing version 3.4.9 to 3.4.10

43

browser/float16.js

@@ -1,2 +0,2 @@

/*! @petamoriken/float16 v3.4.9 | MIT License - https://git.io/float16 */
/*! @petamoriken/float16 v3.4.10 | MIT License - https://git.io/float16 */

@@ -149,2 +149,3 @@ const float16 = (function (exports) {

const wm = new WeakMap();
return (self) => {

@@ -209,3 +210,3 @@ const storage = wm.get(self);

// Inspired by util.types implementation of Node.js
const getTypedArrayPrototypeSybolToStringTag = Reflect.getOwnPropertyDescriptor(Reflect.getPrototypeOf(Uint8Array).prototype, Symbol.toStringTag).get;
const getTypedArrayPrototypeSymbolToStringTag = Reflect.getOwnPropertyDescriptor(Reflect.getPrototypeOf(Uint8Array).prototype, Symbol.toStringTag).get;

@@ -217,3 +218,3 @@ /**

function isTypedArray(value) {
return getTypedArrayPrototypeSybolToStringTag.call(value) !== undefined;
return getTypedArrayPrototypeSymbolToStringTag.call(value) !== undefined;
}

@@ -226,3 +227,3 @@

function isUint16Array(value) {
return getTypedArrayPrototypeSybolToStringTag.call(value) === "Uint16Array";
return getTypedArrayPrototypeSymbolToStringTag.call(value) === "Uint16Array";
}

@@ -543,2 +544,4 @@

const defaultFloat16ArrayMethods = new WeakSet();
/**

@@ -851,6 +854,5 @@ * @param {unknown} target

const length = this.length;
const thisArg = opts[0];
const length = this.length;
const Constructor = SpeciesConstructor(this, Float16Array);

@@ -885,6 +887,7 @@

const length = this.length;
const thisArg = opts[0];
const kept = [];
for (let i = 0, l = this.length; i < l; ++i) {
for (let i = 0; i < length; ++i) {
const val = convertToNumber(this[i]);

@@ -956,5 +959,6 @@ if (callback.call(thisArg, val, i, _(this).proxy)) {

const length = this.length;
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
for (let i = 0; i < length; ++i) {
callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy);

@@ -968,5 +972,6 @@ }

const length = this.length;
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
for (let i = 0; i < length; ++i) {
const value = convertToNumber(this[i]);

@@ -983,5 +988,6 @@ if (callback.call(thisArg, value, i, _(this).proxy)) {

const length = this.length;
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
for (let i = 0; i < length; ++i) {
const value = convertToNumber(this[i]);

@@ -1000,5 +1006,6 @@ if (callback.call(thisArg, value, i, _(this).proxy)) {

const length = this.length;
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
for (let i = length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);

@@ -1015,5 +1022,6 @@ if (callback.call(thisArg, value, i, _(this).proxy)) {

const length = this.length;
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
for (let i = length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);

@@ -1032,5 +1040,6 @@ if (callback.call(thisArg, value, i, _(this).proxy)) {

const length = this.length;
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
for (let i = 0; i < length; ++i) {
if (!callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy)) {

@@ -1048,5 +1057,6 @@ return false;

const length = this.length;
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
for (let i = 0; i < length; ++i) {
if (callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy)) {

@@ -1211,3 +1221,3 @@ return true;

for (let i = from, l = length; i < l; ++i) {
for (let i = from; i < length; ++i) {
if (hasOwn(this, i) && convertToNumber(this[i]) === element) {

@@ -1266,3 +1276,3 @@ return i;

const isNaN = Number.isNaN(element);
for (let i = from, l = length; i < l; ++i) {
for (let i = from; i < length; ++i) {
const value = convertToNumber(this[i]);

@@ -1323,3 +1333,2 @@

const defaultFloat16ArrayMethods = new WeakSet();
for (const key of Reflect.ownKeys(Float16ArrayPrototype)) {

@@ -1326,0 +1335,0 @@ // constructor is not callable

{
"name": "@petamoriken/float16",
"version": "3.4.10",
"description": "half precision floating point for JavaScript",
"version": "3.4.9",
"main": "./lib/index.cjs",
"module": "./src/index.mjs",
"keywords": [
"float16",
"binary16",
"FP16",
"half-precision",
"ieee754",
"Float16Array",
"TypedArray",
"DataView",
"getFloat16",
"setFloat16",
"hfround",
"ponyfill",
"shim"
],
"homepage": "https://git.io/float16",
"bugs": {
"url": "https://github.com/petamoriken/float16/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/petamoriken/float16.git"
},
"license": "MIT",
"author": "Kenta Moriuchi <moriken@kimamass.com> (https://moriken.dev)",
"sideEffects": false,
"exports": {
".": {
"import": "./src/index.mjs",
"require": "./lib/index.cjs"
"require": "./lib/index.cjs",
"import": "./src/index.mjs"
},
"./inspect": {
"import": "./inspect/node.mjs",
"require": "./inspect/node.cjs"
"require": "./inspect/node.cjs",
"import": "./inspect/node.mjs"
}
},
"main": "./lib/index.cjs",
"module": "./src/index.mjs",
"types": "index.d.ts",
"sideEffects": false,
"author": "Kenta Moriuchi <moriken@kimamass.com> (https://moriken.dev)",
"bugs": {
"url": "https://github.com/petamoriken/float16/issues"
},
"homepage": "https://git.io/float16",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/petamoriken/float16.git"
},
"files": [

@@ -37,21 +52,8 @@ "src",

],
"keywords": [
"float16",
"binary16",
"FP16",
"half-precision",
"ieee754",
"Float16Array",
"TypedArray",
"DataView",
"getFloat16",
"setFloat16",
"hfround",
"ponyfill",
"shim"
],
"scripts": {
"build": "yarn run build:lib; yarn run build:browser",
"build:browser": "rollup -c",
"build:lib": "babel src -d lib --out-file-extension .cjs",
"build:browser": "rollup -c",
"clean": "rm -rf lib browser docs coverage .nyc_output",
"coverage": "nyc --reporter=lcov mocha test/*.js",
"docs": "mkdir -p docs && cp README.md docs/index.md; yarn run docs:test",

@@ -62,10 +64,8 @@ "docs:test": "mkdir -p docs/test && cp browser/float16.js docs/test/float16.js; yarn run docs:test:assets; yarn run docs:test:dependencies",

"lint": "eslint *.js src/**/*.mjs test/**/*.js test/**/*.mjs",
"prepublishOnly": "yarn run lint && yarn test",
"refresh": "yarn run clean && yarn run build && yarn run docs",
"setup-test-browser": "http-server docs/test -p 8000 > /dev/null 2>&1 &",
"test": "mocha test/*.js",
"test-browser": "nightwatch -e chrome,chrome_old,firefox,firefox_old,firefox_esr,edge,edge_old,safari,safari_old",
"setup-test-browser": "http-server docs/test -p 8000 > /dev/null 2>&1 &",
"coverage": "nyc --reporter=lcov mocha test/*.js",
"clean": "rm -rf lib browser docs coverage .nyc_output",
"refresh": "yarn run clean && yarn run build && yarn run docs",
"version": "NPM_VERSION_SCRIPT=1 yarn run refresh",
"prepublishOnly": "yarn run lint && yarn test"
"version": "NPM_VERSION_SCRIPT=1 yarn run refresh"
},

@@ -81,3 +81,3 @@ "devDependencies": {

"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsdoc": "^36.1.0",
"eslint-plugin-jsdoc": "^36.1.1",
"espower-cli": "^1.1.0",

@@ -84,0 +84,0 @@ "espower-loader": "^1.2.2",

@@ -69,3 +69,3 @@ # <a href="https://git.io/float16">@petamoriken/float16</a>

You can get modules from [deno.land/x](https://deno.land/x/float16) hosting service.
You can get modules from the [deno.land/x](https://deno.land/x/float16) hosting service.

@@ -107,51 +107,55 @@ ```ts

Or use [jsDelivr](https://cdn.jsdelivr.net/npm/@petamoriken/float16/) CDN.
<details>
<summary>Or, you can use CDN services.</summary>
```html
<!-- Module Scripts -->
<script type="module">
import {
Float16Array, isFloat16Array,
getFloat16, setFloat16,
hfround,
} from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm";
</script>
```
#### [jsDelivr](https://cdn.jsdelivr.net/npm/@petamoriken/float16/) CDN
```html
<!-- Classic Scripts -->
<script src="https://cdn.jsdelivr.net/npm/@petamoriken/float16/browser/float16.min.js"></script>
<script>
const {
Float16Array, isFloat16Array,
getFloat16, setFloat16,
hfround,
} = float16;
</script>
```
```html
<!-- Module Scripts -->
<script type="module">
import {
Float16Array, isFloat16Array,
getFloat16, setFloat16,
hfround,
} from "https://cdn.jsdelivr.net/npm/@petamoriken/float16/+esm";
</script>
```
ES modules are also available on the [Skypack](https://www.skypack.dev/view/@petamoriken/float16) CDN.
```html
<!-- Classic Scripts -->
<script src="https://cdn.jsdelivr.net/npm/@petamoriken/float16/browser/float16.min.js"></script>
<script>
const {
Float16Array, isFloat16Array,
getFloat16, setFloat16,
hfround,
} = float16;
</script>
```
```html
<!-- Module Scripts -->
<script type="module">
import {
Float16Array, isFloat16Array,
getFloat16, setFloat16,
hfround,
} from "https://cdn.skypack.dev/@petamoriken/float16?min";
</script>
```
#### [Skypack](https://www.skypack.dev/view/@petamoriken/float16) CDN
## Support
```html
<!-- Module Scripts -->
<script type="module">
import {
Float16Array, isFloat16Array,
getFloat16, setFloat16,
hfround,
} from "https://cdn.skypack.dev/@petamoriken/float16?min";
</script>
```
</details>
**This package only uses up to the ES2015 features**, so you should be able to use it without any problems.
## Support engines
`Float16Array` implemented by the `Proxy` object, so IE11 is never supported.
**This package only uses up to ES2015 features** and does not use environment-dependent features (except for `inspect/`), so you can use it without any problems. It works fine with [the current officially supported versions of Node.js](https://github.com/nodejs/Release).
`Float16Array` implemented by the `Proxy`, so IE11 is never supported.
### Pre-transpiled JavaScript files (CommonJS, IIFE)
`lib/` and `browser/` directories in the npm package have JavaScript files already transpiled, and they have been tested automatically in the following environments
`lib/` and `browser/` directories in the npm package have JavaScript files already transpiled, and they have been tested automatically in the following environments:
* Node.js Active LTS
* Node.js: Active LTS
* Firefox: last 2 versions and ESR

@@ -233,156 +237,169 @@ * Chrome: last 2 versions

### The `instanceof` Operator
<details>
<summary><code>Float16Array</code> has some limitations, because it is impossible to completely reproduce the behavior of <code>TypedArray</code>. Be careful when checking which <code>TypedArray</code> it is, and when using Web standards such as <code>structuredClone</code> and WebGL.</summary>
Since `Float16Array` is made by inheriting from `Uint16Array`, so you can't use the `instanceof` operator to check if it is a `Uint16Array` or not.
### The `instanceof` Operator
```js
new Uint16Array(10) instanceof Uint16Array; // true
new Float16Array(10) instanceof Uint16Array; // true
```
Since `Float16Array` is made by inheriting from `Uint16Array`, so you can't use the `instanceof` operator to check if it is a `Uint16Array` or not.
Actually, I could use `Proxy`'s `getPrototypeOf` handler to trap it, but that would be too complex and have some limitations.
```js
new Uint16Array(10) instanceof Uint16Array; // true
new Float16Array(10) instanceof Uint16Array; // true
```
In addition, it is a bad idea to use `instanceof` to detect the type of `TypedArray`, because it can't be used to detect the type of objects from other Realms, such as iframe and vm. It is recommended to use `Object#toString` or `@@toStringTag` for this purpose.
Actually, I could use `Proxy`'s `getPrototypeOf` handler to trap it, but that would be too complex and have some limitations.
```js
function isUint16Array(target) {
if (target === null || typeof target !== "object") {
return false;
In addition, it is a bad idea to use `instanceof` to detect the type of `TypedArray`, because it can't be used to detect the type of objects from other Realms, such as iframe and vm. It is recommended to use `Object#toString` or `@@toStringTag` for this purpose.
```js
function isUint16Array(target) {
if (target === null || typeof target !== "object") {
return false;
}
return target[Symbol.toStringTag] === "Uint16Array";
}
return Object.prototype.toString.call(target) === "[object Uint16Array]";
}
```
```
For Node.js, you can use `util.types` ([document](https://nodejs.org/api/util.html#util_util_types)) instead. Want to do a more solid `TypedArray` check for other environments? Then you can use [this code](https://gist.github.com/petamoriken/6982e7469994a8880bcbef6198203042) 😉
For Node.js, you can use `util.types` ([document](https://nodejs.org/api/util.html#util_util_types)) instead. Want to do a more solid `TypedArray` check for other environments? Then you can use [this code](https://gist.github.com/petamoriken/6982e7469994a8880bcbef6198203042).
### Built-in Functions
### Built-in Functions
Built-in `TypedArray` objects use "internal slots" for built-in methods. Some limitations exist because the `Proxy` object can't trap internal slots ([explanation](https://javascript.info/proxy#built-in-objects-internal-slots)).
Built-in `TypedArray` objects use "internal slots" for built-in methods. Some limitations exist because the `Proxy` object can't trap internal slots ([explanation](https://javascript.info/proxy#built-in-objects-internal-slots)).
This package isn't polyfill, in other words, it doesn't change native global functions and static/prototype methods.
This package isn't polyfill, in other words, it doesn't change native global functions and static/prototype methods.
E.g. `ArrayBuffer.isView` is the butlt-in method that checks if it has the `[[ViewedArrayBuffer]]` internal slot. It returns `false` for `Proxy` object such as `Float16Array` instance.
E.g. `ArrayBuffer.isView` is the butlt-in method that checks if it has the `[[ViewedArrayBuffer]]` internal slot. It returns `false` for `Proxy` object such as `Float16Array` instance.
```js
ArrayBuffer.isView(new Float32Array(10)); // true
ArrayBuffer.isView(new Float16Array(10)); // false
```
```js
ArrayBuffer.isView(new Float32Array(10)); // true
ArrayBuffer.isView(new Float16Array(10)); // false
```
### The structured clone algorithm (Web Workers, IndexedDB, etc)
### The structured clone algorithm (Web Workers, IndexedDB, etc)
The structured clone algorithm copies complex JavaScript objects. It is used internally when invoking `structuredClone()`, to transfer data between Web Workers via `postMessage()`, storing objects with IndexedDB, or copying objects for other APIs ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)).
The structured clone algorithm copies complex JavaScript objects. It is used internally when invoking `structuredClone()`, to transfer data between Web Workers via `postMessage()`, storing objects with IndexedDB, or copying objects for other APIs ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)).
It can't clone `Proxy` object such as `Float16Array` instance, you need to convert it to `Uint16Array` or deal with `ArrayBuffer` directly.
It can't clone `Proxy` object such as `Float16Array` instance, you need to convert it to `Uint16Array` or deal with `ArrayBuffer` directly.
```js
const array = new Float16Array([1.0, 1.1, 1.2]);
const cloned = structuredClone({ buffer: array.buffer });
```
```js
const array = new Float16Array([1.0, 1.1, 1.2]);
const cloned = structuredClone({ buffer: array.buffer });
```
### WebGL
### WebGL
WebGL requires `Uint16Array` for buffer or texture data whose types are `gl.HALF_FLOAT` (WebGL 2) or `ext.HALF_FLOAT_OES` (WebGL 1 extension). Do not apply the `Float16Array` object directly to `gl.bufferData` or `gl.texImage2D` etc.
WebGL requires `Uint16Array` for buffer or texture data whose types are `gl.HALF_FLOAT` (WebGL 2) or `ext.HALF_FLOAT_OES` (WebGL 1 extension). Do not apply the `Float16Array` object directly to `gl.bufferData` or `gl.texImage2D` etc.
```js
// WebGL 2 example
const vertices = new Float16Array([
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0,
]);
```js
// WebGL 2 example
const vertices = new Float16Array([
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0,
]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
// wrap in Uint16Array
gl.bufferData(gl.ARRAY_BUFFER, new Uint16Array(vertices.buffer), gl.STATIC_DRAW);
gl.vertexAttribPointer(location, 3, gl.HALF_FLOAT, false, 0, 0);
// wrap in Uint16Array
gl.bufferData(gl.ARRAY_BUFFER, new Uint16Array(vertices.buffer), gl.STATIC_DRAW);
gl.vertexAttribPointer(location, 3, gl.HALF_FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.enableVertexAttribArray(location);
```
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.enableVertexAttribArray(location);
```
### Others
### Others
See JSDoc comments in `src/Float16Array.mjs` for details. If you don't write hacky code, you shouldn't have any problems.
See JSDoc comments in `src/Float16Array.mjs` for details. If you don't write hacky code, you shouldn't have any problems.
</details>
## `Float16Array` custom inspection
## `Float16Array` Custom inspection
<details>
<summary>Provides custom inspection for Node.js and Deno, which makes the results of <code>console.log</code> more readable.
</summary>
Provides custom inspection for Node.js and Deno, which makes the results of `console.log` more readable.
### Node.js
### Node.js
```js
// ES Modules
import { Float16Array } from "@petamoriken/float16";
import { customInspect } from "@petamoriken/float16/inspect";
```js
// ES Modules
import { Float16Array } from "@petamoriken/float16";
import { customInspect } from "@petamoriken/float16/inspect";
Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect;
```
Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect;
```
```js
// CommonJS
const { Float16Array } = require("@petamoriken/float16");
const { customInspect } = require("@petamoriken/float16/inspect");
```js
// CommonJS
const { Float16Array } = require("@petamoriken/float16");
const { customInspect } = require("@petamoriken/float16/inspect");
Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect;
```
Float16Array.prototype[Symbol.for("nodejs.util.inspect.custom")] = customInspect;
```
### Deno
### Deno
```ts
import { Float16Array } from "https://deno.land/x/float16/mod.ts";
import { customInspect } from "https://deno.land/x/float16/inspect.ts";
```ts
import { Float16Array } from "https://deno.land/x/float16/mod.ts";
import { customInspect } from "https://deno.land/x/float16/inspect.ts";
// deno-lint-ignore no-explicit-any
(Float16Array.prototype as any)[Symbol.for("Deno.customInspect")] = customInspect;
```
</details>
// deno-lint-ignore no-explicit-any
(Float16Array.prototype as any)[Symbol.for("Deno.customInspect")] = customInspect;
```
## Development
## Build
<details>
<summary>Manual build and test</summary>
First, download devDependencies.
### Manual Build
```console
yarn
```
First, download devDependencies.
Build `lib/`, `browser/` files.
```console
yarn
```
```console
yarn run build
```
Build `lib/`, `browser/` files.
Build `docs/` files (for browser test).
```console
yarn run build
```
```console
yarn run docs
```
Build `docs/` files (for browser test).
## Test
```console
yarn run docs
```
First, download devDependencies.
### Test
```console
yarn
```
First, download devDependencies.
### Node.js Test
```console
yarn
```
```console
yarn build:lib
yarn test
```
#### Node.js Test
### Browser Test
```console
NODE_ENV=test yarn build:lib
yarn test
```
```console
yarn build:browser
yarn docs
```
#### Browser Test
Access `docs/test/index.html` with browsers.
```console
NODE_ENV=test yarn build:browser
yarn docs
```
You can access current [test page](https://petamoriken.github.io/float16/test) ([power-assert version](https://petamoriken.github.io/float16/test/power)) in `master` branch.
Access `docs/test/index.html` with browsers.
You can access current [test page](https://petamoriken.github.io/float16/test) ([power-assert version](https://petamoriken.github.io/float16/test/power)) in `master` branch.
</details>
## License

@@ -392,2 +409,2 @@

This software contains productions that are distributed under [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). Specifically, `index.d.ts` is modified from the original [TypeScript lib files](https://github.com/microsoft/TypeScript/tree/main/src/lib).
This software contains productions that are distributed under [the Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). Specifically, `index.d.ts` is modified from the original [TypeScript lib files](https://github.com/microsoft/TypeScript/tree/main/src/lib).

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc