@@ -12,55 +12,67 @@ function factory (type, config, load, typed, math) {
1212 }
1313 } ) ;
1414
15- math [ 'true' ] = true ;
16- math [ 'false' ] = false ;
17- math [ 'null' ] = null ;
18- math [ 'uninitialized' ] = require ( './utils/array' ) . UNINITIALIZED ;
15+ setConstant ( math , 'true' , true ) ;
16+ setConstant ( math , 'false' , false ) ;
17+ setConstant ( math , 'null' , null ) ;
18+ setConstant ( math , 'uninitialized' , require ( './utils/array' ) . UNINITIALIZED ) ;
1919
2020 if ( config . number === 'BigNumber' ) {
21- math [ 'Infinity' ] = new type . BigNumber ( Infinity ) ;
22- math [ 'NaN' ] = new type . BigNumber ( NaN ) ;
21+ setConstant ( math , 'Infinity' , new type . BigNumber ( Infinity ) ) ;
22+ setConstant ( math , 'NaN' , new type . BigNumber ( NaN ) ) ;
2323
24- object . lazy ( math , 'pi' , function ( ) { return bigConstants . pi ( type . BigNumber ) } ) ;
25- object . lazy ( math , 'tau' , function ( ) { return bigConstants . tau ( type . BigNumber ) } ) ;
26- object . lazy ( math , 'e' , function ( ) { return bigConstants . e ( type . BigNumber ) } ) ;
27- object . lazy ( math , 'phi' , function ( ) { return bigConstants . phi ( type . BigNumber ) } ) ; // golden ratio, (1+sqrt(5))/2
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
2828
2929 // uppercase constants (for compatibility with built-in Math)
30- object . lazy ( math , 'E' , function ( ) { return math . e ; } ) ;
31- object . lazy ( math , 'LN2' , function ( ) { return new type . BigNumber ( 2 ) . ln ( ) ; } ) ;
32- object . lazy ( math , 'LN10' , function ( ) { return new type . BigNumber ( 10 ) . ln ( ) } ) ;
33- object . lazy ( math , 'LOG2E' , function ( ) { return new type . BigNumber ( 1 ) . div ( new type . BigNumber ( 2 ) . ln ( ) ) ; } ) ;
34- object . lazy ( math , 'LOG10E' , function ( ) { return new type . BigNumber ( 1 ) . div ( new type . BigNumber ( 10 ) . ln ( ) ) } ) ;
35- object . lazy ( math , 'PI' , function ( ) { return math . pi } ) ;
36- object . lazy ( math , 'SQRT1_2' , function ( ) { return new type . BigNumber ( '0.5' ) . sqrt ( ) } ) ;
37- object . lazy ( math , 'SQRT2' , function ( ) { return new type . BigNumber ( 2 ) . sqrt ( ) } ) ;
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 ( ) } ) ;
3838 }
3939 else {
40- math [ 'Infinity' ] = Infinity ;
41- math [ 'NaN' ] = NaN ;
40+ setConstant ( math , 'Infinity' , Infinity ) ;
41+ setConstant ( math , 'NaN' , NaN ) ;
4242
43- math . pi = Math . PI ;
44- math . tau = Math . PI * 2 ;
45- math . e = Math . E ;
46- math . phi = 1.61803398874989484820458683436563811772030917980576286213545 ; // golden ratio, (1+sqrt(5))/2
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
4747
4848 // uppercase constants (for compatibility with built-in Math)
49- math . E = math . e ;
50- math . LN2 = Math . LN2 ;
51- math . LN10 = Math . LN10 ;
52- math . LOG2E = Math . LOG2E ;
53- math . LOG10E = Math . LOG10E ;
54- math . PI = math . pi ;
55- math . SQRT1_2 = Math . SQRT1_2 ;
56- math . SQRT2 = Math . SQRT2 ;
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 ) ;
5757 }
5858
5959 // complex i
60- math . i = type . Complex . I ;
60+ setConstant ( math , 'i' , type . Complex . I ) ;
6161
6262 // meta information
63- math . version = require ( './version' ) ;
63+ setConstant ( math , 'version' , require ( './version' ) ) ;
64+ }
65+
66+ // create a constant in both math and mathWithTransform
67+ function 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
73+ function setLazyConstant ( math , name , resolver ) {
74+ object . lazy ( math , name , resolver ) ;
75+ object . lazy ( math . expression . mathWithTransform , name , resolver ) ;
6476}
6577
6678exports . factory = factory ;
0 commit comments