Giter Site home page Giter Site logo

mikemcl / decimal.js-light Goto Github PK

View Code? Open in Web Editor NEW
355.0 355.0 48.0 665 KB

The light version of decimal.js, an arbitrary-precision Decimal type for JavaScript.

Home Page: http://mikemcl.github.io/decimal.js-light

License: MIT License

JavaScript 99.78% HTML 0.22%
arbitrary-precision bigdecimal bignumber javascript significant-digits

decimal.js-light's People

Contributors

haydnhkim avatar iicdii avatar mikemcl avatar mugeso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

decimal.js-light's Issues

Thoughts about even smaller bundle

I had a couple of ideas to shrink the bundle even more, that are implemented in parzh/decimal.js-tiny. However, the only module system it definitely supports is CommonJS (i.e., Node.js), not sure about browsers and ES modules, haven't tried them.

Perhaps, you have thoughts about shrinking the bundle further down? If not, I can continue supporting decimal.js-tiny at my own risk.

How compute toFraction?

So decimal.js-light has all the API's and I am okey with not including infinity nan and null.

However i do need to use toFraction in the app. Is there a way i can extend it or how can I make a function that does the same as toFraction in decimal.js.

I tried to read and replicate the same thing that the function, but having a really hard time reading and reimplementing this:

  P.toFraction = function (maxD) {
    var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r,
      x = this,
      xd = x.d,
      Ctor = x.constructor;

    if (!xd) return new Ctor(x);

    n1 = d0 = new Ctor(1);
    d1 = n0 = new Ctor(0);

    d = new Ctor(d1);
    e = d.e = getPrecision(xd) - x.e - 1;
    k = e % LOG_BASE;
    d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);

    if (maxD == null) {

      // d is 10**e, the minimum max-denominator needed.
      maxD = e > 0 ? d : n1;
    } else {
      n = new Ctor(maxD);
      if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
      maxD = n.gt(d) ? (e > 0 ? d : n1) : n;
    }

    external = false;
    n = new Ctor(digitsToString(xd));
    pr = Ctor.precision;
    Ctor.precision = e = xd.length * LOG_BASE * 2;

    for (;;)  {
      q = divide(n, d, 0, 1, 1);
      d2 = d0.plus(q.times(d1));
      if (d2.cmp(maxD) == 1) break;
      d0 = d1;
      d1 = d2;
      d2 = n1;
      n1 = n0.plus(q.times(d2));
      n0 = d2;
      d2 = d;
      d = n.minus(q.times(d2));
      n = d2;
    }

    d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
    n0 = n0.plus(d2.times(n1));
    d0 = d0.plus(d2.times(d1));
    n0.s = n1.s = x.s;

    // Determine which fraction is closer to x, n0/d0 or n1/d1?
    r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1
        ? [n1, d1] : [n0, d0];

    Ctor.precision = pr;
    external = true;

    return r;
  };

Typescript import

Hi there,
I have issue importing this library to angular-cli project.
Import like:

import { Decimal } from 'decimal.js-light';

code:

    let d = new Decimal(1.23);
    console.log(d.toJSON());

I get the error:

_WEBPACK_IMPORTED_MODULE_4_decimal_js_light_decimal__.Decimal is not a constructor
    at ClientsComponent.webpackHotUpdate.../../../../../src/app/ftech/clients/clients.component.ts.ClientsComponent.onRefresh (clients.component.ts:89)
    at Object.eval [as handleEvent] (ClientsComponent.ngfactory.js:38)
    at handleEvent (

Typescript 2.4.2, code completion works fine, i.e. recognizes decimal.d.ts file.

How to properly extend Decimal.js-light?

I want to add some helper methods into the prototype, so I write my own class and extend Decimal.js.
It works perfectly except when calling .add() and some other operators. the super class (Decimal) is returned by default. Any ideas to properly extend Decimal?

Slow toLocaleString

I realized that localizing decimal toLocaleString consumes around 3-4 ms which is huge performance problem if you need to render 100 elements which lead to 300ms scripting time and can cause rendering glitches.

Is there any workaround. Obviously this function does not feel light at all.

I am using version 2.5.0

Constructor fails to recognize valid input

We can make the constructor a lot more robust by removing the value instanceof Decimal type guard.

The problem with this guard is that you can get false negatives (esp if you end up with different versions of Decimal in your project, for whatever reason). This causes very difficult-to-trace issues where code will throw on valid inputs depending on where the source object was constructed.

The condition probably doesn't need to be quite so restrictive. What if it was replaced with duck-typing? (this could also help in dumping to/from json).

if (typeof value.s == 'number' && typeof value.e == 'number' ... etc )

Addition is sometimes not associative

const a = new Decimal('-13.77');
const b = new Decimal('1.5612555115940468637');
const c = new Decimal('5');

const result1 = a.add(b).add(c);
const result2 = a.add(b.add(c));

console.log(result1.toString()); // "-7.208744488405953136"
console.log(result2.toString()); // "-7.2087444884059531363"

Fiddle: https://jsfiddle.net/es9pun76/1/

I'm not sure what is special about these values or order of addition, but the values are different when they definitely shouldn't be! result2 is the mathematically correct result, if you were wondering ๐Ÿ˜†

Least significant digit is lost

Hi, I found a bug in my application and I could debug it down to what I'm breaking down below:

import Decimal from 'decimal.js-light';

let number = 1.01322716;
let result1 = new Decimal(number).mul(1).toNumber();  // => 1.0132271
let result2 = new Decimal(number).plus(0).toNumber(); // => 1.0132271

let a = number === result1;  // => false
let b = number === result2;  // => false
let c = result1 === result2; // => true

The least significant digit is lost. Do you know why this is happening and how to avoid it?

Thanks!

157.30 - 1 = 150?

I really don't know what I'm doing wrong:

var dAmount = new Decimal('157.30'), 
    dInterest = new Decimal('1.00');

var result = dAmount.minus(dInterest).toString(); // gives 150, instead of 156.30

What am I doing wrong? Configuration:

Decimal.config({
    precision: 2,
    rounding: Decimal.ROUND_HALF_EVEN
});

Error result of pow

Test case:

console.log(new Decimal(4).pow(0.5).toString()) // 1.9999999999999999999

Obviously, it is not expected, how to fix it?

Add a way to test that a string represents a valid decimal value

In the case I need to use user input, I must first check if it's a valid value before creating a new Decimal instance, or else an error is thrown.

I was expecting a static function allowing to test the validity of a string, e.g.:

Decimal.isValid(userInput)

Of course I could try/catch on an error, but an isValid static function is cleaner and simpler.

Exponent Confusion, and a questions

Playing with decimal.js-light for the first time. Ran into something and have a questions after.

  1. Confusion:

dec = new Decimal(1.99).pow(199);

dec.toExponential() seems correct at "2.9632084641717921185e+59"

but: dec.e = 8

I was expecting it would be 59. Any idea why I'm getting 8?

  1. Question:

Is there a way to format a decimal constrained to the nearest exponent of 3?

examples:
12345 = 12.345e+3 instead of 1.2345 e+4
123456 = 123.456 e+3 instead of 1.23456e+5

I'm ultimately wanting to convert rather large numbers into a metric-type lettering system where every 3 places is represented by a letter (ex 1.23k)

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.