Giter VIP home page Giter VIP logo

bigdenary's Introduction

BigDenary

Arbitrary-length decimal implementation using JavaScript's native BigInt with no dependencies.

  • Supported on Node >= 10.4 and Deno.
  • Check caniuse.com for browser support.

Features

  • Deno module first. Soon to be available as ES Module (ESM) and CommonJS (Node) module.

  • Compute methods are largely implemented through native BigInt, without much string manipulation required.

  • Standalone & lightweight. Zero dependencies.

  • Intuitive data structure โ€“ base amount and decimal places, similar to that of cryptocurrency esp. Bitcoin.

  • API is similar with the popular BigNumber libraries such as bignumber.js, big.js, decimal.js. Not all methods are supported, yet.

Usage

import { BigDenary } from "https://deno.land/x/bigdenary/mod.ts";

const bd = new BigDenary("123.4512");
const sum = bd.add(56.1e2);

console.log(sum.toString()); // 5733.4512
console.log(sum); // BigDenary { base: 57334512n, _decimals: 4 }

API is largely inspired by and attempts to be compatible with decimal.js-light.

Available API

Core

  • constructor(): supports type BDNumberInput = number | string | bigint | BigDenary | BigDenaryRaw.
  • toString(): Returns string representation
  • valueOf(): Returns number approximation
  • toFixed(digits?): Returns string representation to the number of digits to appear decimal point.

Operations

  • plus() or add()
  • minus() or sub()
  • multipliedBy() or mul()
  • dividedBy() or div()
  • negated() or neg()
  • absoluteValue() or abs()

Comparisons

  • comparedTo() or cmp()
  • equals() or eq()
  • greaterThan() or gt()
  • greaterThanOrEqualTo() or gte()
  • lessThan() or lt()
  • lessThanOrEqualTo() or lte()

Develop and running of tests

  1. Install Deno

  2. Run unit tests

deno test

Notes

JavaScript native decimal support is currently being proposed (Stage 1) to ECMA.

License

MIT ยท U-Zyn Chua

Contributions are welcomed.

bigdenary's People

Contributors

uzyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

77it

bigdenary's Issues

(100 / 3) * 3 != 100 and (100 / 3) + (100 / 3) + (100 / 3) != 100

Hi,
I found an issue I want to highlight, curious about the position of the maintainer of this library on it.

I found that for bigdenary (100 / 3) * 3 != 100 and (100 / 3) + (100 / 3) + (100 / 3) != 100

the failing test are the following

Deno.test("BigDenary: (100 / 3) + (100 / 3) + (100 / 3) == 100", () => {
	const b_d100 = new BigDenary(100);
	const b_d33 = b_d100.div(3);
	const b_d100sum = new BigDenary(0).add(b_d33).add(b_d33).add(b_d33);
	assert(b_d100.eq(b_d100sum));
});

Deno.test("BigDenary: (100 / 3) * 3 == 100", () => {
	const b_d100 = new BigDenary(100);
	const b_d100mul = new BigDenary(100).div(3).mul(3);
	assert(b_d100.eq(b_d100mul));
});

The tests succeed when a BigDenary is converted to number with valueOf(), because valueOf() internally convert to string and then uses parseFloat() for the conversion to number, and because the converted string has more than 14 decimals, the returned number is rounded.

I decided then to test also decimal.js and number object, and I found that: number process them correctly (failing on others) while decimal.js fails at them as this library.

Test file attached.

For completeness, I also tested C #, and using the decimal type the problem is not present:

	decimal d100 = 100;
	decimal d33 = d100/ 3;
	decimal d100sum = d33 + d33 + d33;
	decimal d100mul = d33 * 3;
	Console.WriteLine(d33);
	Console.WriteLine(d100 == d100sum);
	Console.WriteLine(d100sum);
	Console.WriteLine(d100 == d100mul);
	Console.WriteLine(d100mul);

test.txt

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.