mikemcl / decimal.js-light Goto Github PK
View Code? Open in Web Editor NEWThe light version of decimal.js, an arbitrary-precision Decimal type for JavaScript.
Home Page: http://mikemcl.github.io/decimal.js-light
License: MIT License
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
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 ๐
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?
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.
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.
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!
Playing with decimal.js-light for the first time. Ran into something and have a questions after.
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?
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)
The link to the README in the documentation ("See the README on GitHub for a quick-start introduction.") points to https://github.com/MikeMcl/decimal.js instead of https://github.com/MikeMcl/decimal.js-light.
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.
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
});
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;
};
I don't want 12kb of extra javascript just for rounding decimal precision
tonum
is in the documentation but it's not defined in the code.
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 )
Test case:
console.log(new Decimal(4).pow(0.5).toString()) // 1.9999999999999999999
Obviously, it is not expected, how to fix it?
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
https://github.com/MikeMcl/decimal.js-light/blob/master/decimal.js#L1862
Change this line to
Object.defineProperty(this, 'constructor', {
value: Decimal
})
can fix the problem, but this looks like a compiled file, I don't know how to make a PR
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.