UNPKG

2.98 kBMarkdownView Raw
1# Fractions
2
3For calculations with fractions, math.js supports a `Fraction` data type.
4Fraction support is powered by [fraction.js](https://github.com/infusion/Fraction.js).
5Unlike [numbers](numbers.md) and [BigNumbers](./bignumbers.md), fractions can
6store numbers with infinitely repeating decimals, for example `1/3 = 0.3333333...`,
7which can be represented as `0.(3)`, or `2/7` which can be represented as `0.(285714)`.
8
9
10## Usage
11
12A Fraction can be created using the function `fraction`:
13
14```js
15math.fraction('1/3') // Fraction, 1/3
16math.fraction(2, 3) // Fraction, 2/3
17math.fraction('0.(3)') // Fraction, 1/3
18```
19
20And can be used in functions like `add` and `multiply` like:
21
22```js
23math.add(math.fraction('1/3'), math.fraction('1/6')) // Fraction, 1/2
24math.multiply(math.fraction('1/4'), math.fraction('1/2')) // Fraction, 1/8
25```
26
27Note that not all functions support fractions. For example trigonometric
28functions doesn't support fractions. When not supported, the functions
29will convert the input to numbers and return a number as result.
30
31Most functions will determine the type of output from the type of input:
32a number as input will return a number as output, a Fraction as input returns
33a Fraction as output. Functions which cannot determine the type of output
34from the input (for example `math.evaluate`) use the default number type `number`,
35which can be configured when instantiating math.js. To configure the use of
36fractions instead of [numbers](numbers.md) by default, configure math.js like:
37
38```js
39// Configure the default type of number: 'number' (default), 'BigNumber', or 'Fraction'
40math.config({
41 number: 'Fraction'
42})
43
44// use the expression parser
45math.evaluate('0.32 + 0.08') // Fraction, 2/5
46```
47
48## Support
49
50The following functions support fractions:
51
52- Arithmetic functions: `abs`, `add`, `ceil`, `cube`, `divide`, `dotDivide`, `dotMultiply`, `fix`, `floor`, `gcd`, `mod`, `multiply`, `round`, `sign`, `square`, `subtract`, `unaryMinus`, `unaryPlus`.
53- Construction functions: `fraction`.
54- Relational functions: `compare`, `deepEqual`, `equal`, `larger`, `largerEq`, `smaller`, `smallerEq`, `unequal`.
55- Utils functions: `format`.
56
57
58## Conversion
59
60Fractions can be converted to numbers and vice versa using the functions
61`number` and `fraction`. When converting a Fraction to a number, precision
62may be lost when the value cannot represented in 16 digits.
63
64```js
65// converting numbers and fractions
66const a = math.number(0.3) // number, 0.3
67const b = math.fraction(a) // Fraction, 3/10
68const c = math.number(b) // number, 0.3
69
70// loosing precision when converting to number: a fraction can represent
71// a number with an infinite number of repeating decimals, a number just
72// stores about 16 digits and cuts consecutive digits.
73const d = math.fraction('2/5') // Fraction, 2/5
74const e = math.number(d) // number, 0.4
75```