1 | 'use strict'
|
2 |
|
3 | const object = require('./utils/object')
|
4 | const bigConstants = require('./utils/bignumber/constants')
|
5 |
|
6 | function factory (type, config, load, typed, math) {
|
7 |
|
8 |
|
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', 'Error: Constant uninitialized is removed since v4.0.0. Use null instead')
|
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) })
|
28 |
|
29 |
|
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 | } else {
|
39 | setConstant(math, 'Infinity', Infinity)
|
40 | setConstant(math, 'NaN', NaN)
|
41 |
|
42 | setConstant(math, 'pi', Math.PI)
|
43 | setConstant(math, 'tau', Math.PI * 2)
|
44 | setConstant(math, 'e', Math.E)
|
45 | setConstant(math, 'phi', 1.61803398874989484820458683436563811772030917980576286213545)
|
46 |
|
47 |
|
48 | setConstant(math, 'E', math.e)
|
49 | setConstant(math, 'LN2', Math.LN2)
|
50 | setConstant(math, 'LN10', Math.LN10)
|
51 | setConstant(math, 'LOG2E', Math.LOG2E)
|
52 | setConstant(math, 'LOG10E', Math.LOG10E)
|
53 | setConstant(math, 'PI', math.pi)
|
54 | setConstant(math, 'SQRT1_2', Math.SQRT1_2)
|
55 | setConstant(math, 'SQRT2', Math.SQRT2)
|
56 | }
|
57 |
|
58 |
|
59 | if (type.Complex) {
|
60 | setConstant(math, 'i', type.Complex.I)
|
61 | }
|
62 |
|
63 |
|
64 | setConstant(math, 'version', require('./version'))
|
65 | }
|
66 |
|
67 |
|
68 | function setConstant (math, name, value) {
|
69 | math[name] = value
|
70 | math.expression.mathWithTransform[name] = value
|
71 | }
|
72 |
|
73 |
|
74 | function setLazyConstant (math, name, resolver) {
|
75 | object.lazy(math, name, resolver)
|
76 | object.lazy(math.expression.mathWithTransform, name, resolver)
|
77 | }
|
78 |
|
79 | exports.factory = factory
|
80 | exports.lazy = false
|
81 | exports.math = true
|