fractions.md (2976B)
1 # Fractions 2 3 For calculations with fractions, math.js supports a `Fraction` data type. 4 Fraction support is powered by [fraction.js](https://github.com/infusion/Fraction.js). 5 Unlike [numbers](numbers.md) and [BigNumbers](./bignumbers.md), fractions can 6 store numbers with infinitely repeating decimals, for example `1/3 = 0.3333333...`, 7 which can be represented as `0.(3)`, or `2/7` which can be represented as `0.(285714)`. 8 9 10 ## Usage 11 12 A Fraction can be created using the function `fraction`: 13 14 ```js 15 math.fraction('1/3') // Fraction, 1/3 16 math.fraction(2, 3) // Fraction, 2/3 17 math.fraction('0.(3)') // Fraction, 1/3 18 ``` 19 20 And can be used in functions like `add` and `multiply` like: 21 22 ```js 23 math.add(math.fraction('1/3'), math.fraction('1/6')) // Fraction, 1/2 24 math.multiply(math.fraction('1/4'), math.fraction('1/2')) // Fraction, 1/8 25 ``` 26 27 Note that not all functions support fractions. For example trigonometric 28 functions doesn't support fractions. When not supported, the functions 29 will convert the input to numbers and return a number as result. 30 31 Most functions will determine the type of output from the type of input: 32 a number as input will return a number as output, a Fraction as input returns 33 a Fraction as output. Functions which cannot determine the type of output 34 from the input (for example `math.evaluate`) use the default number type `number`, 35 which can be configured when instantiating math.js. To configure the use of 36 fractions 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' 40 math.config({ 41 number: 'Fraction' 42 }) 43 44 // use the expression parser 45 math.evaluate('0.32 + 0.08') // Fraction, 2/5 46 ``` 47 48 ## Support 49 50 The 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 60 Fractions can be converted to numbers and vice versa using the functions 61 `number` and `fraction`. When converting a Fraction to a number, precision 62 may be lost when the value cannot represented in 16 digits. 63 64 ```js 65 // converting numbers and fractions 66 const a = math.number(0.3) // number, 0.3 67 const b = math.fraction(a) // Fraction, 3/10 68 const 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. 73 const d = math.fraction('2/5') // Fraction, 2/5 74 const e = math.number(d) // number, 0.4 75 ```