Comments (17)
I will consider this.
Alternatively, amending the library as shown below would allow multiple, independent BigNumber objects to be created.
;(function ( global ) {
'use strict';
function BigNumberFactory() { // Added
var MAX = 1E9...
...
return BigNumber; // Added
} // Added
var BigNumber = BigNumberFactory(); // Added
BigNumber['anew'] = function () { return BigNumberFactory(); }; // Added
if ( typeof module !== 'undefined' && module.exports...
...
})( this );
Usage:
var BN = BigNumber.anew();
BigNumber.config(3);
BN.config(6);
console.log( BigNumber(1).div(3).valueOf() ); // 0.333
console.log( BN(1).div(3).valueOf() ); // 0.333333
from bignumber.js.
That certainly looks like it would work for me. :)
from bignumber.js.
The way I am working around this is to include the bignumber.js (@@BigNumber
) source in my project. Then I have a private copy that is not affected by a users config.
function addBigNumberModule(module, define) {
if (!isUndefined(module) || !isUndefined(define)) {
throw new Error();
}
/*jshint validthis:true */
/*@@BigNumber*/
return this.BigNumber;
}
and then my UMD looks like this
/*global module, define */
if (typeof module === 'object' && !isNull(module) && isTypeObject(module.exports)) {
module.exports = defineAstroDate(addBigNumberModule.call({}));
} else if (typeof define === 'function' && isTypeObject(define.amd)) {
// "name" should be removed when finished with local testing
define('astrodate', function () {
return defineAstroDate(addBigNumberModule.call({}));
});
} else {
globalThis.AstroDate = defineAstroDate(addBigNumberModule.call({}));
}
of course the downside to this is that I have to rebuild my project if I wish to include a newer version of bignumber.js, and I have to do my linting before including it. But it's a solution that is working for me.
from bignumber.js.
+1 for this one.
It would be great if we can have different, independent BigNumber instances or factories with their own custom configuration instead of having global configuration. Right now, as soon as you have a project where multiple modules use BigNumber.js under the hood requiring different settings, you have a problem...
from bignumber.js.
Yes, I've been considering whether to add the BigNumberFactory code above when I push the non-integer powers update in a week or two. As it seems to have some support I probably will do so.
from bignumber.js.
Thanks, that would be great.
I think in general only static modules/functionality should be available globally. Non-static modules with configuration should be instantiated to prevent the possibility of conflicts.
Usage could also look something like this (always work via a factory):
var BigNumberFactory = require('bignumber.js');
// create a factory with configuration
var factory1 = new BigNumberFactory({DECIMAL_PLACES: 3});
// create a factory and change configuration later on
var factory2 = new BigNumberFactory();
factory2.config(6);
// create a bignumber using a factory function
console.log(factory1.bignumber(1).div(3).valueOf()); // 0.333
console.log(factory2.bignumber(1).div(3).valueOf()); // 0.333333
from bignumber.js.
I take your point, but considering backwards compatibility and simplicity of use, the BigNumber object will probably remain as it is but with a static new
or copy
method added.
var BigNumber = require('bignumber.js');
var SmallNumber = BigNumber.new();
SmallNumber.config({ DECIMAL_PLACES: 50 });
var x = new SmallNumber('1e-30').div(3);
from bignumber.js.
lol, I like your example ;)
from bignumber.js.
As a thought, and you were asking for ideas for the name, rather than new
(which probably is a bad one, as IDEs will complain about reserved keywords), or ànew
or copy
(which could cause confusion), how about factory
?
from bignumber.js.
I have just published decimal.js which includes a noConflict
method and also allows muliple Decimal constructors to be efficiently created, each with their own independent configuration.
I ended up using the name constructor
not factory
as I wanted to overwrite the existing constructor
property from the prototype.
from bignumber.js.
Thanks, I will giving it a whirl.
from bignumber.js.
Like what you have done there, however, for my application I need precision to work with decimal places and not significant figures. Have you had any further thoughts of adding a factory method to BigNumber?
from bignumber.js.
I added it to big.js not long ago, so perhaps you could use that if you want to work with decimal places.
To create a fresh constructor in decimal.js:
D = Decimal.constructor();
And in big.js:
B = Big();
In bignumber.js? Not sure...
I'll consider it further, but I updated bignumber.js just recently, so I'd prefer not to look at that code again for a few months...
from bignumber.js.
I'll have a closer look at Big and see if that handles everything I need.
from bignumber.js.
So, there is no .config
in big.js other than editing the file itself, and no support for NaN and Infinity? Doesn't seem to fit my bill unfortunately.
from bignumber.js.
No NaN and Infinity, but decimal places and rounding mode can be set using, for example
Big.DP = 20;
BIg.RM = 1;
from bignumber.js.
Aha, thanks for the config
info. Don't think I can do without the NaN etc support though.
from bignumber.js.
Related Issues (20)
- Strangeness in "minimum" method HOT 2
- Using version 9.1.1, run new BigNumber(0).exponentiatedBy('-12345678901234567890123456789012345678901234567890').valueOf() Is return to infinity normal? HOT 1
- Issue toNumber() function round up when number is too big (v9.1.1) HOT 2
- Division on exponential values leads to infinite loop HOT 3
- 负数加减乘除不好用 HOT 1
- [Feature Request]: Can we support `toBigInt`?
- Type error HOT 2
- Big Number (toFixed,dp,decimalPlaces,toFormat) - Chrome Update Issue 115.0.5790.102 HOT 24
- Minus sign in formatted strings HOT 2
- wrong calculation result on multipliedBy/times methods HOT 1
- toNumber does not respect ROUNDING_MODE HOT 11
- Calculation error HOT 1
- Is there any other way to handle new BigNumber(999999999999999999999) without using strings? HOT 2
- Difference between multipliedBy and dividedBy behaviors HOT 2
- Different result between toFixed and toNumber HOT 1
- Can't convert to Base64 HOT 1
- Tree shake commonjs version from mjs version HOT 1
- toFixed error
- [Bug] Return Type of Methods HOT 1
- Logarithms (I know there is another issue about this)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bignumber.js.