mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
68 lines
2.2 KiB
Markdown
68 lines
2.2 KiB
Markdown
---
|
|
layout: default
|
|
---
|
|
|
|
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|
|
|
|
<h1 id="function-fraction">Function fraction <a href="#function-fraction" title="Permalink">#</a></h1>
|
|
|
|
Create a fraction or convert a value to a fraction.
|
|
|
|
With one numeric argument, produces the closest rational approximation to the
|
|
input.
|
|
With two arguments, the first is the numerator and the second is the denominator,
|
|
and creates the corresponding fraction. Both numerator and denominator must be
|
|
integers.
|
|
With one object argument, looks for the integer numerator as the value of property
|
|
'n' and the integer denominator as the value of property 'd'.
|
|
With a matrix argument, creates a matrix of the same shape with entries
|
|
converted into fractions.
|
|
|
|
|
|
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.fraction(value)
|
|
math.fraction(numerator, denominator)
|
|
math.fraction({n: numerator, d: denominator})
|
|
math.fraction(matrix: Array | Matrix)
|
|
```
|
|
|
|
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`args` | number | string | Fraction | BigNumber | Array | Matrix | Arguments specifying the value, or numerator and denominator of the fraction
|
|
|
|
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
Fraction | Array | Matrix | Returns a fraction
|
|
|
|
|
|
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
|
|
|
|
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.fraction(6.283) // returns Fraction 6283/1000
|
|
math.fraction(1, 3) // returns Fraction 1/3
|
|
math.fraction('2/3') // returns Fraction 2/3
|
|
math.fraction({n: 2, d: 3}) // returns Fraction 2/3
|
|
math.fraction([0.2, 0.25, 1.25]) // returns Array [1/5, 1/4, 5/4]
|
|
math.fraction(4, 5.1) // throws Error: Parameters must be integer
|
|
```
|
|
|
|
|
|
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
|
|
|
|
[bignumber](bignumber.html),
|
|
[number](number.html),
|
|
[string](string.html),
|
|
[unit](unit.html)
|