JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 4
  • Score
    100M100P100Q49601F
  • License MIT

Library for wrapping arithmetic

Package Exports

  • wrapping

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (wrapping) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Wrapping

npm version Coverage Status TypeScript

Library for wrapping arithmetic

Install

NPM: npm install --save-dev wrapping

Yarn: yarn add wrapping

TypeScript declaration file is included.

Why

If you want to emulate numeric data types like a uint8 (unsigned 8-bit integer) and perform arithmetic with wrapping semantics.

Example

// Create a new context to operate on unsigned 8-bit numbers
const wrapper = new Wrapping(0, 256 /* 2 ** 8 */);
console.log(wrapper.add(1, 255)); // 0
console.log(wrapper.subtract(0, 1)); // 255
console.log(wrapper.multiply(5, 52)); // 4

API

class Wrapping

constructor(min: number, max: number)

min and max must be a finite, safe integer.

Methods:

  • add(first: number, second: number): number

  • subtract(first: number, second: number): number

  • multiply(first: number, second: number): number

  • divide(first: number, second: number): number

    This is redundant, as division between two numbers will never result in overflow. Implemented only for completeness sakes.

  • wrapNumber(n: number): number

    Wraps a number according to the min and max values set in the constructor. This can be calculated using the formula n - (max - min) - Math.floor((n - min) / (max - min)).

Motivation

This project was inspired by Rust's Wrapping struct and the wrapping_add/wrapping_sub/wrapping_mul/wrapping_div functions.

License

MIT