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