2.1.0 • Published 5 years ago

wrapping v2.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago