1 |
|
2 |
|
3 |
|
4 | const { create, all } = require('..')
|
5 |
|
6 |
|
7 | const config = {
|
8 |
|
9 |
|
10 | number: 'Fraction'
|
11 | }
|
12 |
|
13 |
|
14 | const math = create(all, config)
|
15 |
|
16 | console.log('basic usage')
|
17 | printRatio(math.fraction(0.125))
|
18 | printRatio(math.fraction(0.32))
|
19 | printRatio(math.fraction('1/3'))
|
20 | printRatio(math.fraction('0.(3)'))
|
21 | printRatio(math.fraction(2, 3))
|
22 | printRatio(math.fraction('0.(285714)'))
|
23 | console.log()
|
24 |
|
25 | console.log('round-off errors with numbers')
|
26 | print(math.add(0.1, 0.2))
|
27 | print(math.divide(0.3, 0.2))
|
28 | console.log()
|
29 |
|
30 | console.log('no round-off errors with fractions :)')
|
31 | print(math.add(math.fraction(0.1), math.fraction(0.2)))
|
32 | print(math.divide(math.fraction(0.3), math.fraction(0.2)))
|
33 | console.log()
|
34 |
|
35 | console.log('represent an infinite number of repeating digits')
|
36 | print(math.fraction('1/3'))
|
37 | print(math.fraction('2/7'))
|
38 | print(math.fraction('23/11'))
|
39 | console.log()
|
40 |
|
41 |
|
42 |
|
43 | console.log('use fractions in the expression parser')
|
44 | printRatio(math.evaluate('0.1 + 0.2'))
|
45 | printRatio(math.evaluate('0.3 / 0.2'))
|
46 | printRatio(math.evaluate('23 / 11'))
|
47 | console.log()
|
48 |
|
49 |
|
50 | console.log('output formatting of fractions')
|
51 | const a = math.fraction('2/3')
|
52 | console.log(math.format(a))
|
53 | console.log(math.format(a, { fraction: 'ratio' }))
|
54 | console.log(math.format(a, { fraction: 'decimal' }))
|
55 | console.log(a.toString())
|
56 | console.log()
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function printRatio (value) {
|
64 | console.log(math.format(value, { fraction: 'ratio' }))
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function print (value) {
|
73 | console.log(math.format(value, { fraction: 'decimal' }))
|
74 | }
|