mikemcl / bignumber.js Goto Github PK
View Code? Open in Web Editor NEWA JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
Home Page: http://mikemcl.github.io/bignumber.js
License: MIT License
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
Home Page: http://mikemcl.github.io/bignumber.js
License: MIT License
Hello and thank you for your work!
Is it possible to add log()
method? similar to Math.log()
?
Thank you.
I noticed some unexpected behavior when dividing numbers. In the following example I have set the precision to 10 decimal places. I expected this to be the number of significant digits, but apparently this means the number of significant digits behind the decimal point.
In the following example I would expect each of the results to be in the format 1.666666667e[+-]EXP
: each time having 10 digits, and the value of EXP
being equal to the exponent of the divided value. Instead, the number of digits varies depending on the values exponent.
BigNumber.config(10);
console.log(new BigNumber('5e-15').div('3').toExponential()); // "0e+0"
console.log(new BigNumber('5e-10').div('3').toExponential()); // "2e-10"
console.log(new BigNumber('5e-5').div('3').toExponential()); // "1.66667e-5"
console.log(new BigNumber('5e+0').div('3').toExponential()); // "1.6666666667e+0"
console.log(new BigNumber('5e+5').div('3').toExponential()); // "1.666666666666667e+5"
console.log(new BigNumber('5e+10').div('3').toExponential()); // "1.66666666666666666667e+10"
Is this intended behavior? If so, is there any workaround so I can actually do divisions on small values without loosing precision?
Hi Mike,
First up!, my compliments on an awesome library!
Second up!, any plans to port/convert to The Dart Language?
Best Regards,
Ian Williams
P.S. My apologies if this is not the correct place to ask a question rather than post an issue!
I install it using bower in my Mac, and serve my sites in vagrant based linux server, and using symlink to share the code between Mac and Linux. and then I got a 404 from nginx in linux server.
2015/04/20 04:58:26 [error] 3133#0: *1 open() "/vagrant/site/www/vendor/bignumber.js/bignumber.js" failed (20: Not a directory), client: 192.168.211.1, server: mydomain, request: "GET /vendor/bignumber.js/bignumber.js HTTP/1.1", host: "mydomain"
I figure it out, there are two ways to solve this:
bignumber.js
to bignumber
I didn't know, if this is an bower issue or just because the filesystem' difference between Mac OS and Linux.
If it is not a bower issue, maybe we should consider to make a alias for bower.
Engineering notation: "the powers of ten must be multiples of three".
It would be nice to have the ability to output 12356 as 12.345e+3, instead of 1.23456e+5.
Either as it's own method toEngineering() - Identical to toExponential() except for the "multiples of three" bit.
OR
As an extra parameter() to toExponential()
() Which perhaps could then allow an arbitrary multiple (not just 3)
And if it was possible to query a BigNumber for this "Engineering" exponent as well (similar to BN.e) that would be an extra bonus
Mike,
May i use this library for arbitrary length integers. e.g. 1000 digits long base 10 numbers ? Excuse me for this hasty question but i was unable to find such example in the description.
The docs say that BigNumber.e should be considered read-only, and my testing confirms that setting it is not reliable. It seems that given the internal representation, it should be easy to add a "shift" method that moves the decimal point. This would be equivalent to multiplying/dividing by powers of 10, but much more efficient and not requiring setting DECIMAL_PLACES to much larger numbers (if you have a large number and multiply by 10^(-50), the second term can get truncated to 0, making the result 0).
This would be very useful for the obvious application of multiplying by powers of 10, but also for situations where you need to convert to a different BigNumber representation and you need to extract the first N digits of the mantissa.
We built a benchmark to measure the difference between BigNumber, Big and jsbn, thought you might be interested in the results:
http://jsperf.com/bignumber-js-vs-big-js-vs-jsbn
/cc @weilu
Linking to "//cdnjs.cloudflare.com/ajax/libs/bignumber.js/2.0.3/bignumber.min.js" will lead to this error:
"GET //cdnjs.cloudflare.com/ajax/libs/bignumber.js/2.0.3/doc/bignumber.js.map 404 (Not Found)".
Please upload the source map file to CDNJS.
It would also be helpful to have an "pretty" version of this lib on CDNJS, even that we have a source map file.
I have a project with karma (and jasmine) for testing, and I'm using browserify.
In a production code, for include Bignumber I have this code.
window.BigNumber = require("pathtobignumber/bignumber.js")
But I cant make this work in a testing environment.
How can I save a big number in cookies and then read it ?
add
, subtract
, multiply
, divide
, remainder
, negate
, compareTo
are implemented by all those libs:
https://github.com/dtrebbien/BigDecimal.js
https://github.com/iriscouch/bigdecimal.js
https://github.com/silentmatt/javascript-biginteger
http://www-cs-students.stanford.edu/~tjw/jsbn/
Your lib provides: plus
, minus
, times
, div
, mod
, neg
, cmp
.
Any chance to change this?
I was wondering why the number type input is limited to 15 significant digits when Javascript can represent an integer to 16?
Ie. -9007199254740991 to 9007199254740991
// Disallow numbers with over 15 significant digits if number type.
if ( isNum && b > 15 && n.slice(i).length > 15 && +n.slice(i) > 9007199254740991) {
// 'new BigNumber() number type has more than 15 significant digits: {n}'
ifExceptionsThrow( orig, 0 )
}
I'm not suggesting this is the fix/enhancement as I'm sure there is more to it than that.
The readme says "It accepts a value of type Number, String or BigNumber Object", which would be nice, but which is currently incorrect. BigNumber(Math.random())
and BigNumber(Math.PI)
and several other higher-precision numbers fail with the following message:
BigNumber Error: new BigNumber() number type has more than 15 significant digits: [digits]
This has several problems:
Math.random()
and Math.PI
are valid numbers, which should be handled properly.Would it be possible to have a noConflict method for BigNumber. This would allow me to have a private BigNumber object for use in my library, where I set specific config() options. And it would allow the user to have a global BigNumber where they can have different config() options that will not affect my private copy. I know that using RequireJS or NodeJS will do this for me, but I'm a little stuck with the case where BigNumber is injected or loaded with <script>
Or perhaps there is a better way?
It would be great if there was a way to do fractional exponents so that:
var number = BigNumber("25").toPower("0.5");
Would equal 5 and not error.
new BigNumber('0.50000025000012500006').pow(-1).toString()
this should work right? trying to invert the number.
after about 15 seconds I get:
FATAL ERROR: JS Allocation failed - process out of memory
Abort trap: 6
on node.js, version 2.0.3
Hi!
Would you be interested in a function like this?
/*jslint maxerr: 50, indent: 4, browser: true, white: true, devel: true */
/*global BigNumber */
(function() {
"use strict";
function pi(digits) {
digits = Math.floor(digits);
var config = BigNumber.config(),
previousConfig = {},
k = 0,
prop,
sum,
ta,
tb,
divisor,
a,
b;
for (prop in config) {
if (config.hasOwnProperty(prop)) {
previousConfig[prop] = config[prop];
}
}
BigNumber.config({
DECIMAL_PLACES : digits,
ROUNDING_MODE : 4,
EXPONENTIAL_AT : [-7, 20],
RANGE : [-1000000000, 1000000000],
ERRORS : true
});
sum = new BigNumber(0);
a = ta = new BigNumber(16).div(5);
b = tb = new BigNumber(-4).div(239);
while (!a.equals(b)) {
divisor = 2 * k + 1;
a = ta.div(divisor);
b = tb.div(divisor);
sum = sum.plus(a).plus(b);
ta = ta.neg().div(25);
tb = tb.neg().div(57121);
k += 1;
}
for (prop in previousConfig) {
if (previousConfig.hasOwnProperty(prop)) {
config[prop] = previousConfig[prop];
}
}
return sum;
}
console.log(pi(20).toString());
}());
I'm using bignumber.js (without modifying it) and it keeps getting stuck in the for loop on line 1557.
Hi @MikeMcl,
I use this library to convert a uuid to a base62 string:
var BigNumber = require('bignumber.js');
var uuid = "a4ead3da8d164477ac9f197f11ff6c7a";
uu = BigNumber(uuid,16);
var b62 = uu.toString(62);
console.log(uuid);
console.log(b62);
Result:
a4ead3da8d164477ac9f197f11ff6c7a
51c3Bp5gPevldjwJD5Fv6y
Now the same in PHP:
$uuid = "a4ead3da8d164477ac9f197f11ff6c7a";
$b62 = gmp_strval(gmp_init($uuid, 16), 62);
echo $uuid . "\n";
echo $b62 . "\n";
Result:
a4ead3da8d164477ac9f197f11ff6c7a
51C3bP5GpEVLDJWjd5fV6Y
You see that lower and upper characters are vice versa:
51c3Bp5gPevldjwJD5Fv6y - bignumbers.js
51C3bP5GpEVLDJWjd5fV6Y - PHP
Now I wonder if bignumbers.js or the PHP gmp extension has a bug. Or is there no standard how to convert?
Thanks for your help.
Ciao,
korpa
I see logarithm (log) method in the README image. But it's neither in the documentation nor it is a method.
I am using bignumber.js for financial calculations, and I noticed that the pow() function can be slow at times. For example, the below takes 4 seconds to execute:
var a = new BigNumber(1.0011012345678).pow(-2000);
Is this expected? If so, are there things that I can do to speed up calculations like this? I know one option is to round to fewer digits before running the pow function, but are there others?
As a comparison, I also use bcmath in PHP, and I noticed the bcpow function returns instantly for the above calculation:
$a = bcpow(1.0011012345678, -2000);
Thanks for your help.
Ryan
First, thanks for this excellent library, much appreciated. I am using it in a financial application and it works great to far. I have the following issue, this is how I use BigNumber.config
:
BigNumber.config({ DECIMAL_PLACES: 10 });
BigNumber.config(null, BigNumber.ROUND_HALF_UP):
var ROUNDING_DP = 8
This is how I instantiate:
value = new BigNumber value;
value.toFixed(ROUNDING_DP);
From my understanding the toFixed
method takes only the ROUND_HALF_UP
property from the .config. Wouldn't it be convenient to be able to also do:
BigNumber.config(null, BigNumber.ROUND_HALF_UP, BigNumber.ROUNDING_DP)
and then just:
value.toFixed()
Can you please add a tag for version 1.0.1?
Right now Bower only sees version 1.0.0
Consider this code:
BigNumber(2).sqrt().toPrecision(50)
You'd expect this to return the squareroot of 2 with a precision of 50 digits... It does, but not exactly in the way I was hoping:
"1.4142135623730950488000000000000000000000000000000"
A little dissapointing.
As seen here, the squareroot of 2 is irrational, so it can in theory display a (near-)infinite number of digits.
The above result looks like the calculation was processed with an arbitrary but fixed amount of precision, and then displayed.
Thanks for this library. It's really great.
I want to be able to pass the results of calculations done with bignumber.js between services. Typically, when something gets sent over the wire, you serialize it with JSON.stringify
and then deserialize it with JSON.parse. Doing so with a BigNumber gives something like {"s":1,"e":0,"c":[1]}
.
Unfortunately, there doesn't seem to be support in the constructor for doing new BigNumber(JSON.parse("{\"s\":1,\"e\":0,\"c\":[1]}"))
and instantiating a new, equivalent BigNumber from those internals. So, at this point, I can send internal values from service to service, but I can't easily rehydrate those numbers unless I'm doing something like .toString() or .toNumber() on the sending side... which is a little janky.
console.log(Math.pow(9,0.5)); // outputs 3 as expected
console.log(new BigNumber(9).pow(0.5)); // Uncaught BigNumber Error: pow() exponent not an integer: 0.5
I have started using bignumber.js for a project. I needed to do some math that involved the use of Pi in one of the equations. I had to construct a BigNumber for Pi myself and I wonder if it would make more sense to have this (and possibly other standard values) built-in?
Creating the Pi value wasn't straightforward either.
I tried BigNumber(Math.PI);
but that gives an error because it has more than 15 significant digits.
This forced me to do something like BigNumber(Math.PI.toFixed(10));
.
Seems like it would just be easier if we could do BigNumber.PI;
.
Best described by example
http://jsfiddle.net/a6t784am/2/
.toFraction() seems to land on a denominator based around the DECIMAL_PLACES config variable when a MaxD is not supplied. A MaxD in range of a known denominator give the correct solution.
If a BigNumber method call is followed by a constructor call with a value which throws an error, then that error's message may wrongly attribute the error to the previous method call instead of the BigNumber constructor.
I've started getting errors on bower install. I checked the tags page, and all old tags (versions mentioned in the change log) are wiped away.
This can lead to some problems for projects which depend on older versions of bignumber. Can you fix this issue?
When using bignumber.js in a larger project there is the possibility that different files in the project will have different configuration requirements. Using a global configuration is unsafe because future changes in unrelated code can break working code. This could be solved by adding a localConfig
method that returns a new BigNumber class with class-scoped configuration.
var config = { DECIMAL_PLACES: 100 };
var BigNumber = require(‘bignumber.js’).localConfig(config);
This is backwards compatible; without localConfig
it could still use the global configuration.
As far as i can see from the code there's no ability to set the roundingmode and other stuff per instance of Bignumber.
Am I wrong?
I was using "bignumber.js": "~1.4.1" in my bower.json file. I looks like the 1.4.x tags were deleted and now bower complains with "ENORESTARGET No tag found that was able to satisfy ~1.4.1". Please keep the tags going forward.
what's the history with having lessThan
as if we're comparing quantities?
there's a properly named greaterThan
, and the complement should be lowerThan
I was curious why some tags have been deleted. Is there a particular reason to clean up and remove previous tags or was it unintentional?
I was previously using a tag to v1.2.0
in a bower file, upgrading to v1.4.1
isn't an issue luckily.
It would be nice if the function BigNumber.toFixed
has a default of 0 decimal places, consistently with the toFixed
of Number
:
var n = 2.34;
console.log(n.toFixed()); // '2'
var b = new BigNumber(2.34);
console.log(b.toFixed()); // '2.34' -> should return '2' for consistency
In the Why are trailing fractional zeros removed from BigNumbers? section:
x = new BigDecimal("1.0")
y = new BigDecimal("1.1000")
z = x.add(y) // 2.1000
x = new BigDecimal("1.20")
y = new BigDecimal("3.45000")
z = x.multiply(y) // 4.1400000
should be
x = new BigNumber("1.0")
y = new BigNumber("1.1000")
z = x.plus(y) // 2.1000
x = new BigNumber("1.20")
y = new BigNumber("3.45000")
z = x.times(y) // 4.1400000
Adding the missing bower.json would be very nice to work with bower. It can download the project but it's not able to know which file to include, which is the exact purpose of the "main" property of the bower.json.
For more infos on how to do it, refer to the following issue: CreateJS/SoundJS#76
hello,
this snippet of code doesn't work with ios8
<script>
var a = new BigNumber(0.5);
var b = new BigNumber(0.5);
a = a.plus(b);
console.log(+a);
</script>
console.log print NaN
with a bit of debug i found that the problem is here
xc[b] %= BASE;
here log of safari console
xc[b]
< 100000000000000
BASE
< 100000000000000
xc[b] % BASE
< 2.5333397e-315
a % 1
< 0
1 % 1
< 0
note the 2.5333397e-315 as a result of mod operation....
seems a safari bug, but as you know, it's easy if your lib support browser os rather than ask to steve jobs (rest in peace) to fix safari in the next ios version 👯
for now my (hugly) fix is
if (BASE == xc[b])
xc[b] = 0;
else
xc[b] %= BASE;
in "plus" function
I've noticed that travis has not been running for recent commits/pull requests:
https://travis-ci.org/MikeMcl/bignumber.js
test/browser/every-test.html no longer displays correctly on Opera. (This is only an issue with the test page, not the library itself.)
The tests all pass, but the messages displayed seem to indicate some sort of JavaScript caching issue.
The page displays correctly if it is loaded in a private tab: Opera -> Tabs and Windows -> New Private Tab.
Input values such as "1" and "1.0" are accepted but "1." is an error. Is this intentional or should that be a valid value?
> BigNumber('1.')
BigNumber Error: new BigNumber() not a number: 1.
If you call isNegative()
on a number that has a value of zero that was produced by adding the absolute value of a negative number to itself, you get true
. It should return false
instead.
var BigNumber = require('bignumber.js');
var foo = new BigNumber(0);
console.log('is 0 negative:', foo.isNegative());
console.log('is 0 negative:', foo.minus(1).plus(1).isNegative());
Output is:
is 0 negative: false
is 0 negative: true
Hi Micheal,
thanks for your fantastic library!
I just have a question on result: I need the result to be shorter than 11 digits, even at the cost of losing a little precision.
E.g:
99999999999 x 9999999999 = 9.999999999800000000001e+21
is possible to truncate to 9.999999999e+21?
Thanks for your response,
R.
Greetings,
Is there any plans to implement bitwise operators?
This library implements them, but it's messy and may not be reliable.
We use this library in the calculator app on Ubuntu Touch.
Under certain conditions (on a Nexus 4/7 with QT 5.3 which is connected via usb to the computer) divisions fails bad (1/3 = 0.3 instead 0.333333, 55/2 = 25). See the original bug to more informations.
We find the bug is caused by timing issue with setting and reading the rem[] array in the do/while cycle in division function.
We fixed in a very symple way, we inverted the adding of next digit to the result array and the update of remainder. See this patch.
Are you interesting to have this change in your code?
I know that is a very specific bug about a certain platform, but can happen also on others platforms.
Hoping to have pleased reporting the bug,
Regards,
Riccardo
I've sent a pull request to jsdelivr to include all of your latest releases on their CDN. This should happen within the next day or so.
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.