{"id":19391839,"url":"https://github.com/joeltg/fp16","last_synced_at":"2026-02-13T00:04:41.034Z","repository":{"id":57241206,"uuid":"405418636","full_name":"joeltg/fp16","owner":"joeltg","description":"Half-precision 16-bit floating point numbers","archived":false,"fork":false,"pushed_at":"2025-03-05T15:27:40.000Z","size":487,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-26T00:14:08.834Z","etag":null,"topics":["float16","floating-point","half-precision","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joeltg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-11T15:46:58.000Z","updated_at":"2025-03-05T15:27:20.000Z","dependencies_parsed_at":"2025-03-05T16:26:31.811Z","dependency_job_id":"ebc3327f-6f1e-4355-b1fc-179096ed720f","html_url":"https://github.com/joeltg/fp16","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeltg%2Ffp16","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeltg%2Ffp16/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeltg%2Ffp16/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeltg%2Ffp16/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joeltg","download_url":"https://codeload.github.com/joeltg/fp16/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252269136,"owners_count":21721248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["float16","floating-point","half-precision","typescript"],"created_at":"2024-11-10T10:29:22.809Z","updated_at":"2026-02-13T00:04:41.028Z","avatar_url":"https://github.com/joeltg.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fp16\n\n[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg)](https://github.com/RichardLitt/standard-readme) [![license](https://img.shields.io/github/license/joeltg/fp16)](https://opensource.org/licenses/MIT) [![NPM version](https://img.shields.io/npm/v/fp16)](https://www.npmjs.com/package/fp16) ![TypeScript types](https://img.shields.io/npm/types/fp16)\n\nHalf-precision 16-bit floating point numbers.\n\n[`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) has APIs for getting and setting [float64s](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) and [float32s](https://en.wikipedia.org/wiki/Single-precision_floating-point_format). This library provides the analogous methods for [float16s](https://en.wikipedia.org/wiki/Half-precision_floating-point_format), and utilities for testing how a given float64 value will convert to float32 and float16 values. Conversion implements the IEEE 754 default rounding behavior (\"Round-to-Nearest RoundTiesToEven\").\n\n`NaN` is always encoded as `0x7e00`, which extends the pattern of how browsers serialize `NaN` in 32 bits and is the recommendation [in the CBOR spec](https://www.rfc-editor.org/rfc/rfc8949.html#name-deterministically-encoded-c).\n\nThis library is TypeScript-native, ESM-only, and has zero dependencies. It works in Node, the browser, and Deno.\n\n## Table of Contents\n\n- [Install](#install)\n- [Usage](#usage)\n- [Testing](#testing)\n- [Credits](#credits)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Install\n\n```\nnpm i fp16\n```\n\n## Usage\n\n### Set a 16-bit float\n\n```typescript\ndeclare function setFloat16(\n  view: DataView,\n  offset: number,\n  value: number,\n  littleEndian?: boolean,\n): void\n```\n\n### Get a 16-bit float\n\n```typescript\ndeclare function getFloat16(\n  view: DataView,\n  offset: number,\n  littleEndian?: boolean,\n): number\n```\n\n### Precision\n\nIn addition to methods for getting and setting float16s, `fp16` exports two methods for testing how a given `number` value will convert to 32-bit and 16-bit values.\n\n```typescript\nexport const Precision = {\n\tExact: 0,\n\tInexact: 1,\n\tUnderflow: 2,\n\tOverflow: 3,\n} as const\n\nexport type Precision = typeof Precision[keyof typeof Precision]\n\ndeclare function getFloat32Precision(value: number): Precision\ndeclare function getFloat16Precision(value: number): Precision\n```\n\n- `Precision.Exact`: Conversion will not loose precision. The value is guaranteed to round-trip back to the same `number` value. Positive and negative zero, positive and negative infinity, and `NaN` all return `exact`. **Values that can be represented losslessly as a subnormal value in the target format will return `exact`.**\n- `Precision.Overflow`: the exponent of the given value is greater than the maximum exponent of the target size (`127` for float32 or `15` for float16). Conversion is guaranteed to overflow to +/- Infinity.\n- `Precision.Underflow`: the exponent of the given value is less than the minimum exponent _minus the number of fractional bits_ of the target size (`-126 - 23` for float32 or `-14 - 10` for float16). Conversion is guaranteed to underflow to +/- 0 **or** to the smallest signed subnormal value (`+/- 2^-24` for float16 or `+/- 2^-149` for float32).\n- `Precision.Inexact`: the exponent is within the target range, but precision bits will be lost during rounding. The value may round to +/- 0 but will never round to +/- Infinity.\n\nNote that the boundaries for overflow and underflow are not what you might necessarily expect; this is because values with exponents just under the minimum exponent for a format map to subnormal values.\n\nAlso note that `fp16` treats all NaN values as identical, ignoring sign and signalling bits when decoding, and encoding every `NaN` value as `0x7e00`. This means that not all 16-bit values will round-trip through `setFloat16` and `getFloat16`.\n\n## Testing\n\nTests use [AVA](https://github.com/avajs/ava) and live in the [test](./test/) directory.\n\n```\nnpm run test\n```\n\nTests cover decoding all 65536 possible 16-bit values, rounding behaviour, subnormal values, underflows, and overflows. More tests are always welcome.\n\n## Credits\n\n[This PDF](http://fox-toolkit.org/ftp/fasthalffloatconversion.pdf) was extremely helpful as a reference for understanding the float16 format, even though `fp16` doesn't use the table-based aproach it outlines.\n\nThe Golang [github.com/x448/float16](https://github.com/x448/float16) package was used as a reference for implementing rounding. The test suite in [tests/32to16.js](./test/32to16.js) was adapted from its test file [float16_test.go](https://github.com/x448/float16/blob/master/float16_test.go).\n\n## Contributing\n\nI don't expect to add any additional features to this library, or change any of the exported interfaces. If you encounter a bug or would like to add more tests, please open an issue to discuss it!\n\n## License\n\nMIT © 2021 Joel Gustafson\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoeltg%2Ffp16","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoeltg%2Ffp16","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoeltg%2Ffp16/lists"}