UNPKG

3.4 kBJavaScriptView Raw
1'use strict'
2
3const object = require('./utils/object')
4const 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', '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) }) // 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 } 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) // golden ratio, (1+sqrt(5))/2
46
47 // uppercase constants (for compatibility with built-in Math)
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 // complex i
59 if (type.Complex) {
60 setConstant(math, 'i', type.Complex.I)
61 }
62
63 // meta information
64 setConstant(math, 'version', require('./version'))
65}
66
67// create a constant in both math and mathWithTransform
68function setConstant (math, name, value) {
69 math[name] = value
70 math.expression.mathWithTransform[name] = value
71}
72
73// create a lazy constant in both math and mathWithTransform
74function setLazyConstant (math, name, resolver) {
75 object.lazy(math, name, resolver)
76 object.lazy(math.expression.mathWithTransform, name, resolver)
77}
78
79exports.factory = factory
80exports.lazy = false // no lazy loading of constants, the constants themselves are lazy when needed
81exports.math = true // request access to the math namespace