@@ -147,4 +147,79 @@ describe("round to decimals", function () {
147
147
} ) ;
148
148
149
149
150
+ describe ( "round to significant digits plus decimals" , function ( ) {
151
+
152
+ it ( "single number" , function ( ) {
153
+
154
+ let expr = me . fromText ( '1.234567890123456789' ) ;
155
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 100 , 6 ) . tree ) . toEqual ( 1.2345678901234567 )
156
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 14 , 6 ) . tree ) . toEqual ( 1.2345678901235 )
157
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 10 , 6 ) . tree ) . toEqual ( 1.23456789 )
158
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 4 , 6 ) . tree ) . toEqual ( 1.234568 )
159
+
160
+ expr = me . fromText ( '12345678901234567890000000000' ) ;
161
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 100 , - 17 ) . tree ) . toEqual ( 12345678901234568000000000000 )
162
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 14 , - 17 ) . tree ) . toEqual ( 12345678901235000000000000000 )
163
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 10 , - 17 ) . tree ) . toEqual ( 12345678901200000000000000000 )
164
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 4 , - 17 ) . tree ) . toEqual ( 12345678901200000000000000000 )
165
+
166
+ expr = me . fromText ( '0.000000000000000000001234567890123456789' ) ;
167
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 100 , 27 ) . tree ) . toEqual ( 0.0000000000000000000012345678901234568 )
168
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 14 , 27 ) . tree ) . toEqual ( 0.0000000000000000000012345678901235 )
169
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 10 , 27 ) . tree ) . toEqual ( 0.00000000000000000000123456789 )
170
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 4 , 27 ) . tree ) . toEqual ( 0.000000000000000000001234568 )
171
+
172
+ expect ( me . fromAst ( 0 ) . round_numbers_to_precision_plus_decimals ( 10 , 2 ) . tree ) . toEqual ( 0 )
173
+ expect ( me . fromAst ( Infinity ) . round_numbers_to_precision_plus_decimals ( 10 , 2 ) . tree ) . toEqual ( Infinity )
174
+ expect ( me . fromAst ( - Infinity ) . round_numbers_to_precision_plus_decimals ( 10 , 2 ) . tree ) . toEqual ( - Infinity )
175
+
176
+ } ) ;
177
+
178
+ it ( "expression" , function ( ) {
179
+
180
+ let expr = me . fromText ( 'exp(1.234567890123456789x+9.876543210987654321)/(5+8520203156.435345956432x)' ) ;
181
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 100 , 5 ) . equals (
182
+ me . fromText ( 'exp(1.2345678901234567 x + 9.876543210987654)/(5 + 8520203156.435346 x)' ) ) ) . toBeTruthy ( ) ;
183
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 14 , 5 ) . equals (
184
+ me . fromText ( 'exp(1.2345678901235 x + 9.8765432109877)/(5 + 8520203156.43535 x)' ) ) ) . toBeTruthy ( ) ;
185
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 10 , 5 ) . equals (
186
+ me . fromText ( 'exp(1.23456789 x + 9.876543211)/(5 + 8520203156.43535 x)' ) ) ) . toBeTruthy ( ) ;
187
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 4 , 5 ) . equals (
188
+ me . fromText ( 'exp(1.23457 x + 9.87654)/(5 + 8520203156.43535 x)' ) ) ) . toBeTruthy ( ) ;
189
+
190
+ } ) ;
191
+
192
+ it ( "don't round fractions" , function ( ) {
193
+
194
+ let expr = me . fromText ( '3/7x + 381439619649.253 y' ) ;
195
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 100 , - 2 ) . equals (
196
+ me . fromText ( '3/7x + 381439619649.253 y' ) ) ) . toBeTruthy ( ) ;
197
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 14 , - 2 ) . equals (
198
+ me . fromText ( '3/7x + 381439619649.25 y' ) ) ) . toBeTruthy ( ) ;
199
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 10 , - 2 ) . equals (
200
+ me . fromText ( '3/7x + 381439619600 y' ) ) ) . toBeTruthy ( ) ;
201
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 4 , - 2 ) . equals (
202
+ me . fromText ( '3/7x + 381439619600 y' ) ) ) . toBeTruthy ( ) ;
203
+
204
+ } ) ;
205
+
206
+
207
+ it ( "don't round pi or e" , function ( ) {
208
+
209
+ let expr = me . fromText ( '3/7e + 381439619649.253 pi' ) ;
210
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 100 , 1 ) . equals (
211
+ me . fromText ( '3/7exp(1) + 381439619649.253 pi' ) ) ) . toBeTruthy ( ) ;
212
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 14 , 1 ) . equals (
213
+ me . fromText ( '3/7exp(1) + 381439619649.25 pi' ) ) ) . toBeTruthy ( ) ;
214
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 10 , 1 ) . equals (
215
+ me . fromText ( '3/7exp(1) + 381439619649.3 pi' ) ) ) . toBeTruthy ( ) ;
216
+ expect ( expr . round_numbers_to_precision_plus_decimals ( 4 , 1 ) . equals (
217
+ me . fromText ( '3/7exp(1) + 381439619649.3 pi' ) ) ) . toBeTruthy ( ) ;
218
+
219
+ } ) ;
220
+
221
+
222
+ } ) ;
223
+
224
+
150
225
} ) ;
0 commit comments