11import { NumberFunctions } from "../../../../algorithm/numberFunctions" ;
22import { Random } from "../../../../algorithm/random" ;
3+ import { Decimal } from "decimal.js" ;
34
45/**
56 * 存放和数学逻辑有关的函数
67 */
78export namespace MathFunctions {
89 export function add ( numbers : number [ ] ) : number [ ] {
9- return [ numbers . reduce ( ( acc , cur ) => acc + cur , 0 ) ] ;
10+ return [ numbers . reduce ( ( acc , cur ) => new Decimal ( acc ) . plus ( new Decimal ( cur ) ) . toNumber ( ) , 0 ) ] ;
1011 }
1112
1213 export function subtract ( numbers : number [ ] ) : number [ ] {
@@ -17,15 +18,15 @@ export namespace MathFunctions {
1718 return [ - numbers [ 0 ] ] ;
1819 }
1920 // 累减
20- let result = numbers [ 0 ] ;
21+ let result = new Decimal ( numbers [ 0 ] ) ;
2122 for ( let i = 1 ; i < numbers . length ; i ++ ) {
22- result -= numbers [ i ] ;
23+ result = result . sub ( new Decimal ( numbers [ i ] ) ) ;
2324 }
24- return [ result ] ;
25+ return [ result . toNumber ( ) ] ;
2526 }
2627
2728 export function multiply ( numbers : number [ ] ) : number [ ] {
28- return [ numbers . reduce ( ( acc , cur ) => acc * cur , 1 ) ] ;
29+ return [ numbers . reduce ( ( acc , cur ) => new Decimal ( acc ) . times ( new Decimal ( cur ) ) . toNumber ( ) , 1 ) ] ;
2930 }
3031
3132 export function divide ( numbers : number [ ] ) : number [ ] {
@@ -35,11 +36,11 @@ export namespace MathFunctions {
3536 if ( numbers . length === 1 ) {
3637 return [ 1 / numbers [ 0 ] ] ;
3738 }
38- let result = numbers [ 0 ] ;
39+ let result = new Decimal ( numbers [ 0 ] ) ;
3940 for ( let i = 1 ; i < numbers . length ; i ++ ) {
40- result /= numbers [ i ] ;
41+ result = result . div ( new Decimal ( numbers [ i ] ) ) ;
4142 }
42- return [ result ] ;
43+ return [ result . toNumber ( ) ] ;
4344 }
4445
4546 export function modulo ( numbers : number [ ] ) : number [ ] {
@@ -63,23 +64,23 @@ export namespace MathFunctions {
6364 if ( numbers . length === 1 ) {
6465 return [ Math . pow ( numbers [ 0 ] , 1 ) ] ;
6566 }
66- let result = numbers [ 0 ] ;
67+ let result = new Decimal ( numbers [ 0 ] ) ;
6768 for ( let i = 1 ; i < numbers . length ; i ++ ) {
68- result = Math . pow ( result , numbers [ i ] ) ;
69+ result = result . pow ( new Decimal ( numbers [ i ] ) ) ;
6970 }
70- return [ result ] ;
71+ return [ result . toNumber ( ) ] ;
7172 }
7273
7374 export function factorial ( numbers : number [ ] ) : number [ ] {
7475 return numbers . map ( ( n ) => _factorial ( n ) ) ;
7576 }
7677
7778 export function sqrt ( numbers : number [ ] ) : number [ ] {
78- return numbers . map ( ( n ) => Math . sqrt ( n ) ) ;
79+ return numbers . map ( ( n ) => new Decimal ( n ) . sqrt ( ) . toNumber ( ) ) ;
7980 }
8081
8182 export function abs ( numbers : number [ ] ) : number [ ] {
82- return numbers . map ( ( n ) => Math . abs ( n ) ) ;
83+ return numbers . map ( ( n ) => new Decimal ( n ) . abs ( ) . toNumber ( ) ) ;
8384 }
8485
8586 export function log ( numbers : number [ ] ) : number [ ] {
@@ -94,47 +95,47 @@ export namespace MathFunctions {
9495 }
9596
9697 export function ln ( numbers : number [ ] ) : number [ ] {
97- return numbers . map ( ( n ) => Math . log ( n ) ) ;
98+ return numbers . map ( ( n ) => new Decimal ( n ) . ln ( ) . toNumber ( ) ) ;
9899 }
99100
100101 export function exp ( numbers : number [ ] ) : number [ ] {
101- return numbers . map ( ( n ) => Math . exp ( n ) ) ;
102+ return numbers . map ( ( n ) => new Decimal ( n ) . exp ( ) . toNumber ( ) ) ;
102103 }
103104
104105 export function sin ( numbers : number [ ] ) : number [ ] {
105- return numbers . map ( ( n ) => Math . sin ( n ) ) ;
106+ return numbers . map ( ( n ) => new Decimal ( n ) . sin ( ) . toNumber ( ) ) ;
106107 }
107108
108109 export function cos ( numbers : number [ ] ) : number [ ] {
109- return numbers . map ( ( n ) => Math . cos ( n ) ) ;
110+ return numbers . map ( ( n ) => new Decimal ( n ) . cos ( ) . toNumber ( ) ) ;
110111 }
111112
112113 export function tan ( numbers : number [ ] ) : number [ ] {
113- return numbers . map ( ( n ) => Math . tan ( n ) ) ;
114+ return numbers . map ( ( n ) => new Decimal ( n ) . tan ( ) . toNumber ( ) ) ;
114115 }
115116
116117 export function asin ( numbers : number [ ] ) : number [ ] {
117- return numbers . map ( ( n ) => Math . asin ( n ) ) ;
118+ return numbers . map ( ( n ) => new Decimal ( n ) . asin ( ) . toNumber ( ) ) ;
118119 }
119120
120121 export function acos ( numbers : number [ ] ) : number [ ] {
121- return numbers . map ( ( n ) => Math . acos ( n ) ) ;
122+ return numbers . map ( ( n ) => new Decimal ( n ) . acos ( ) . toNumber ( ) ) ;
122123 }
123124
124125 export function atan ( numbers : number [ ] ) : number [ ] {
125- return numbers . map ( ( n ) => Math . atan ( n ) ) ;
126+ return numbers . map ( ( n ) => new Decimal ( n ) . atan ( ) . toNumber ( ) ) ;
126127 }
127128
128129 export function sinh ( numbers : number [ ] ) : number [ ] {
129- return numbers . map ( ( n ) => Math . sinh ( n ) ) ;
130+ return numbers . map ( ( n ) => new Decimal ( n ) . sinh ( ) . toNumber ( ) ) ;
130131 }
131132
132133 export function cosh ( numbers : number [ ] ) : number [ ] {
133- return numbers . map ( ( n ) => Math . cosh ( n ) ) ;
134+ return numbers . map ( ( n ) => new Decimal ( n ) . cosh ( ) . toNumber ( ) ) ;
134135 }
135136
136137 export function tanh ( numbers : number [ ] ) : number [ ] {
137- return numbers . map ( ( n ) => Math . tanh ( n ) ) ;
138+ return numbers . map ( ( n ) => new Decimal ( n ) . tanh ( ) . toNumber ( ) ) ;
138139 }
139140 export function max ( numbers : number [ ] ) : number [ ] {
140141 return [ Math . max ( ...numbers ) ] ;
@@ -188,16 +189,13 @@ export namespace MathFunctions {
188189 export function count ( numbers : number [ ] ) : number [ ] {
189190 return [ numbers . length ] ;
190191 }
191- export function sum ( numbers : number [ ] ) : number [ ] {
192- return [ numbers . reduce ( ( acc , cur ) => acc + cur , 0 ) ] ;
193- }
194192 /**
195193 * 平均值
196194 * @param numbers
197195 * @returns
198196 */
199197 export function average ( numbers : number [ ] ) : number [ ] {
200- return [ numbers . reduce ( ( acc , cur ) => acc + cur , 0 ) / numbers . length ] ;
198+ return [ new Decimal ( add ( numbers ) [ 0 ] ) . div ( numbers . length ) . toNumber ( ) ] ;
201199 }
202200 /**
203201 * 中位数
0 commit comments