UNPKG

3.46 kBJavaScriptView Raw
1'use strict';
2
3var object = require('./utils/object');
4var bigConstants = require('./utils/bignumber/constants');
5
6function factory (type, config, load, typed, math) {
7 // listen for changed in the configuration, automatically reload
8 // constants when needed
9 math.on('config', function (curr, prev) {
10 if (curr.number !== prev.number) {
11 factory(type, config, load, typed, math);
12 }
13 });
14
15 setConstant(math, 'true', true);
16 setConstant(math, 'false', false);
17 setConstant(math, 'null', null);
18 setConstant(math, 'uninitialized', require('./utils/array').UNINITIALIZED);
19
20 if (config.number === 'BigNumber') {
21 setConstant(math, 'Infinity', new type.BigNumber(Infinity));
22 setConstant(math, 'NaN', new type.BigNumber(NaN));
23
24 setLazyConstant(math, 'pi', function () {return bigConstants.pi(type.BigNumber)});
25 setLazyConstant(math, 'tau', function () {return bigConstants.tau(type.BigNumber)});
26 setLazyConstant(math, 'e', function () {return bigConstants.e(type.BigNumber)});
27 setLazyConstant(math, 'phi', function () {return bigConstants.phi(type.BigNumber)}); // golden ratio, (1+sqrt(5))/2
28
29 // uppercase constants (for compatibility with built-in Math)
30 setLazyConstant(math, 'E', function () {return math.e;});
31 setLazyConstant(math, 'LN2', function () {return new type.BigNumber(2).ln();});
32 setLazyConstant(math, 'LN10', function () {return new type.BigNumber(10).ln()});
33 setLazyConstant(math, 'LOG2E', function () {return new type.BigNumber(1).div(new type.BigNumber(2).ln());});
34 setLazyConstant(math, 'LOG10E', function () {return new type.BigNumber(1).div(new type.BigNumber(10).ln())});
35 setLazyConstant(math, 'PI', function () {return math.pi});
36 setLazyConstant(math, 'SQRT1_2', function () {return new type.BigNumber('0.5').sqrt()});
37 setLazyConstant(math, 'SQRT2', function () {return new type.BigNumber(2).sqrt()});
38 }
39 else {
40 setConstant(math, 'Infinity', Infinity);
41 setConstant(math, 'NaN', NaN);
42
43 setConstant(math, 'pi', Math.PI);
44 setConstant(math, 'tau', Math.PI * 2);
45 setConstant(math, 'e', Math.E);
46 setConstant(math, 'phi', 1.61803398874989484820458683436563811772030917980576286213545); // golden ratio, (1+sqrt(5))/2
47
48 // uppercase constants (for compatibility with built-in Math)
49 setConstant(math, 'E', math.e);
50 setConstant(math, 'LN2', Math.LN2);
51 setConstant(math, 'LN10', Math.LN10);
52 setConstant(math, 'LOG2E', Math.LOG2E);
53 setConstant(math, 'LOG10E', Math.LOG10E);
54 setConstant(math, 'PI', math.pi);
55 setConstant(math, 'SQRT1_2', Math.SQRT1_2);
56 setConstant(math, 'SQRT2', Math.SQRT2);
57 }
58
59 // complex i
60 setConstant(math, 'i', type.Complex.I);
61
62 // meta information
63 setConstant(math, 'version', require('./version'));
64}
65
66// create a constant in both math and mathWithTransform
67function setConstant(math, name, value) {
68 math[name] = value;
69 math.expression.mathWithTransform[name] = value;
70}
71
72// create a lazy constant in both math and mathWithTransform
73function setLazyConstant (math, name, resolver) {
74 object.lazy(math, name, resolver);
75 object.lazy(math.expression.mathWithTransform, name, resolver);
76}
77
78exports.factory = factory;
79exports.lazy = false; // no lazy loading of constants, the constants themselves are lazy when needed
80exports.math = true; // request access to the math namespace
\No newline at end of file