1 |
|
2 |
|
3 |
|
4 | const math = require('../index')
|
5 |
|
6 |
|
7 | math.config({
|
8 | number: 'BigNumber',
|
9 |
|
10 | precision: 20
|
11 | })
|
12 |
|
13 | console.log('round-off errors with numbers')
|
14 | print(math.add(0.1, 0.2))
|
15 | print(math.divide(0.3, 0.2))
|
16 | console.log()
|
17 |
|
18 | console.log('no round-off errors with BigNumbers')
|
19 | print(math.add(math.bignumber(0.1), math.bignumber(0.2)))
|
20 | print(math.divide(math.bignumber(0.3), math.bignumber(0.2)))
|
21 | console.log()
|
22 |
|
23 | console.log('create BigNumbers from strings when exceeding the range of a number')
|
24 | print(math.bignumber(1.2e+500))
|
25 | print(math.bignumber('1.2e+500'))
|
26 | console.log()
|
27 |
|
28 | console.log('BigNumbers still have a limited precision and are no silve bullet')
|
29 | const third = math.divide(math.bignumber(1), math.bignumber(3))
|
30 | const total = math.add(third, third, third)
|
31 | print(total)
|
32 | console.log()
|
33 |
|
34 |
|
35 |
|
36 | console.log('use BigNumbers in the expression parser')
|
37 | print(math.eval('0.1 + 0.2'))
|
38 | print(math.eval('0.3 / 0.2'))
|
39 | console.log()
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function print (value) {
|
46 | console.log(math.format(value))
|
47 | }
|