Git Product home page Git Product logo

decimal128.js's Introduction

decimal128.js—A userland approximation to IEEE 754 Decimal128 in JavaScript

Operations

  • addition (add)
  • subtraction (subtract)
  • multiplication (multiply)
  • division (divide)
  • truncate (trunctate)
  • floor (floor)
  • ceiling (ceiling)
  • absolute value (abs)
  • toString
  • toDecimalDigits

Implementation

This package is written in TypeScript. Unit tests are in (typed) Jest. There are no other external dependencies.

Data model

This package aims to reproduce the IEEE 754 Decimal128 decimal floating-point numbers in JavaScript. These decimal (not binary!) numbers take up 128 bits of information per number. This format allows for an exact representation of decimal numbers with 34 (decimal) significant digits and an exponent between -6143 and 6144. That's a vast amount of range and precision! Decimal128 is a fantastic standard. Let's implement it in JavaScript.

This package also supports minus zero, positive and negative infinity, and NaN.

This package aims to work with only normalized values in the Decimal128 universe. With this package, there is no way to represent, say, 1.2 and 1.20 as distinct values. Digit strings are normalized right away, so 1.20 becomes 1.2.

Differences with the official Decimal128

This package is not literally an implementation of Decimal128. In time, it may become one, but initially, this package is working with a subspace of Decimal128 that makes sense for the use cases we have in mind (mainly, finance).

Lack of support for specifying context

IEEE 754 Decimal128 allows one to globally specify configuration values (e.g., precision) that control all mathematical operations on Decimal128 values. This JavaScript package does not support that. Calculations are always done using all available digits.

Think of this package as providing, basically, arbitrary-precision decimal numbers limited to those that fit into 128 bits the way that Decimal128 does it. No need to specify context. Just imagine that you're working in an ideal arbitrary-precision world, do the operation, and enjoy the results. If you need to cut off a calculation after a certain point, just perform the operation (e.g., addition) and then use toDecimalDigits on the result.

Values always normalized

Decimal128 is a universe of unnormalized values. In the Decimal128 world, 1.2 and 1.20 are distinct values. There's good reason for adopting such an approach, and some benefits. But this package deliberately works in a world of normalized values. Given the string 1.20, this package will turn that into 1.2; that extra trailing zero will be lost. To recover the string 1.20, additional, out-of-band information needs to be supplied. For instance: if you're working with numbers as financial quantities, you know, out-of-band, how to interpret your numbers. Thus, if I tell you that the cost of something is 1.2 USD, you know that means, and you know that, if you need to present that data to someone, you'd add an extra digit there. This package provides a toDecimalDigits method that allows you to generate 1.20 from the underlying 1.2.

Missing operations

This package focuses on the bread and butter of arithmetic: addition, multiplication, subtraction, and division. To round things out from there (ha!), we have the absolute value function, trunction, floor/ceiling.

decimal128.js's People

Contributors

jessealama avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.