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