说明
入门
入门
例子
文档
核心
配置
链式调用
扩展
序列化
表达式
解析和评估
句法
表达式树
代数
定制
安全
数据类型
数字
大数字
分数
复数
矩阵
单位
参考
类
方法
常量
定制捆绑
命令行界面
历史
分数 - Math.js文档 - 笔下光年
网站首页
分数
For calculations with fractions, math.js supports a `Fraction` data type. Fraction support is powered by [fraction.js](https://github.com/rawify/Fraction.js "fraction.js"). Unlike [numbers](http://www.bixiaguangnian.com/manual/mathjs/374.html "numbers") and [BigNumbers](http://www.bixiaguangnian.com/manual/mathjs/375.html "BigNumbers"), fractions can store numbers with infinitely repeating decimals, for example `1/3 = 0.3333333...,` which can be represented as `0.(3)`, or `2/7` which can be represented as `0.(285714)`. ### Usage A Fraction can be created using the function `fraction`: ```javascript math.fraction('1/3') // Fraction, 1/3 math.fraction(2, 3) // Fraction, 2/3 math.fraction('0.(3)') // Fraction, 1/3 ``` And can be used in functions like `add` and `multiply` like: ```javascript math.add(math.fraction('1/3'), math.fraction('1/6')) // Fraction, 1/2 math.multiply(math.fraction('1/4'), math.fraction('1/2')) // Fraction, 1/8 ``` Note that not all functions support fractions. For example trigonometric functions doesn’t support fractions. When not supported, the functions will convert the input to numbers and return a number as result. Most functions will determine the type of output from the type of input: a number as input will return a number as output, a Fraction as input returns a Fraction as output. Functions which cannot determine the type of output from the input (for `example math.evaluate`) use the default number type `number`, which can be configured when instantiating math.js. To configure the use of fractions instead of [numbers](http://www.bixiaguangnian.com/manual/mathjs/374.html "numbers") by default, configure math.js like: ```javascript // Configure the default type of number: 'number' (default), 'BigNumber', or 'Fraction' math.config({ number: 'Fraction' }) // use the expression parser math.evaluate('0.32 + 0.08') // Fraction, 2/5 ``` ### Support The following functions support fractions: - Arithmetic functions: `abs`, `add`, `ceil`, `cube`, `divide`, `dotDivide`, `dotMultiply`, `fix`, `floor`, `gcd`, `mod`, `multiply`, `round`, `sign`, `square`, `subtract`, `unaryMinus`, `unaryPlus`. - Construction functions: `fraction`. - Relational functions: `compare`, `deepEqual`, `equal`, `larger`, `largerEq`, `smaller`, `smallerEq`, `unequal`. - Utils functions: `format`. ### Conversion Fractions can be converted to numbers and vice versa using the functions `number` and `fraction`. When converting a Fraction to a number, precision may be lost when the value cannot represented in 16 digits. ```javascript // converting numbers and fractions const a = math.number(0.3) // number, 0.3 const b = math.fraction(a) // Fraction, 3/10 const c = math.number(b) // number, 0.3 // loosing precision when converting to number: a fraction can represent // a number with an infinite number of repeating decimals, a number just // stores about 16 digits and cuts consecutive digits. const d = math.fraction('2/5') // Fraction, 2/5 const e = math.number(d) // number, 0.4 ```
上一篇:
大数字
下一篇:
复数