npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

preciser-calc

v0.2.0

Published

Offers fast math operation functions with better decimal precision

Downloads

66

Readme

Actions Status Actions Status Actions Status Test Coverage Maintainability Packages npm version

This lib aims to provider faster sum and multiplication operations preserving a good precision with little overload.

Our benchmark tests have shown that preciser-calc sum and weightSum operation are around 5 times faster than just sum all numbers when dealing with numbers with decimal fractions, and our tests shows that the operations keep precision up to 3 decimal places, which avoid a lot of headache in most monetary cases that deals only with 2! The multiplication is much slower than the vanilla one, but still, it is 5 times faster than big.js or decimal.js version. Keep in mind that those libraries prioritize precision, while this one provides a better precision than vanilla, but not loosing performance (or even gaining in some cases). It's meant to be used in no edge cases where we want to mitigate some floating math problems just enough.

How to Install

npm i preciser-calc

How to use it

To sum:

const result = sum([value1, value2, value3, value4, value5]);

To sum with an arbitrary precision:

const result = sum([value1, value2, value3, value4, value5], 10000);

It's ideal for the precision factor to be a power of 10, but any number will work (for the better or the worst). The number of zeroes, ie, the 10 exponent, will be the number of places sum will try to preserve precision. The default precision factor is 1000. Keep in mind that this operation doesn't check the max safe integer limit, and the precision factor will be multiplied by your operands, so, if any number get bigger than 2^52, you can loose precision. The same is valid for the weightSum:

// weight array version
const result = weightSum([value1, value2, value3, value4, value5], [weight1, weight2, weight3, weight4, weight5], 10000 /* optional, default 1000 */);
// object array version
const result = weightSumObj([{ price: 100.14, qty: 2 }, { price: 12.54, qty: 1.5 }, { price: 7.49, qty: 3.7 }], 'price', 'value', 10000 /* optional, default 1000 */);

A multiplication using a precision factor is also implemented:

const result = multiply([value1, value2, value3, value4, value5], 10000 /* optional, default 1000 */)

This multiplication have no problem with the number multiplied by the precision factor, was with sum and sumWeight, but if the numbers get too big it'll use bigint to preserve the most precision possible, and that'll be no good for heavy math processing as bigint is solved in O(n), not O(1) as numbers, so, use it with caution.

Finally, if you're dealing with numbers with more than 3 decimal places, this lib still can be useful for you, but some calculation or other can get a little noise from floating math. To fix it, you can use our round function:

const factor = 1000000;
const result = round(sum([value1, value2, value3, value4, value5], factor), factor);

We still can't mathematically guarantee this, but, every repeating decimal we got when using a power of 10 factor are like x.xx0000000000n, when bigger than the expected result, or x.xx999999999999n, when lesser. In this case, round will get you the right result for your precision. Also, we tested the combination of round and sum and it for 6 x slower than vanilla, which is pretty great, because it is more then 1 million ops/sec (50x faster than big.js, for example).

License

Licensed under MIT.