Skip to content

Commit 489c6cf

Browse files
gwhitneyjosdejong
andauthored
chore: Update to latest fraction.js (#2467)
Resolves #2427. Also adds tests verifying that creating a fraction from two numbers requires them both to be integers, and slightly updates documentation. Co-authored-by: Jos de Jong <[email protected]>
1 parent 955b596 commit 489c6cf

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"complex.js": "^2.0.15",
3030
"decimal.js": "^10.3.1",
3131
"escape-latex": "^1.2.0",
32-
"fraction.js": "^4.1.3",
32+
"fraction.js": "^4.2.0",
3333
"javascript-natural-sort": "^0.7.1",
3434
"seedrandom": "^3.0.5",
3535
"tiny-emitter": "^2.1.0",

src/expression/embeddedDocs/construction/fraction.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ export const fractionDocs = {
33
category: 'Construction',
44
syntax: [
55
'fraction(num)',
6-
'fraction(num,den)'
6+
'fraction(matrix)',
7+
'fraction(num,den)',
8+
'fraction({n: num, d: den})'
79
],
810
description:
9-
'Create a fraction from a number or from a numerator and denominator.',
11+
'Create a fraction from a number or from integer numerator and denominator.',
1012
examples: [
1113
'fraction(0.125)',
12-
'fraction(1, 3) + fraction(2, 5)'
14+
'fraction(1, 3) + fraction(2, 5)',
15+
'fraction({n: 333, d: 53})',
16+
'fraction([sqrt(9), sqrt(10), sqrt(11)])'
1317
],
1418
seealso: [
1519
'bignumber', 'boolean', 'complex', 'index', 'matrix', 'string', 'unit'

src/type/fraction/function/fraction.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,39 @@ const dependencies = ['typed', 'Fraction']
66

77
export const createFraction = /* #__PURE__ */ factory(name, dependencies, ({ typed, Fraction }) => {
88
/**
9-
* Create a fraction convert a value to a fraction.
9+
* Create a fraction or convert a value to a fraction.
10+
*
11+
* With one numeric argument, produces the closest rational approximation to the
12+
* input.
13+
* With two arguments, the first is the numerator and the second is the denominator,
14+
* and creates the corresponding fraction. Both numerator and denominator must be
15+
* integers.
16+
* With one object argument, looks for the integer numerator as the value of property
17+
* 'n' and the integer denominator as the value of property 'd'.
18+
* With a matrix argument, creates a matrix of the same shape with entries
19+
* converted into fractions.
1020
*
1121
* Syntax:
22+
* math.fraction(value)
1223
* math.fraction(numerator, denominator)
1324
* math.fraction({n: numerator, d: denominator})
14-
* math.fraction(matrix: Array | Matrix) Turn all matrix entries
15-
* into fractions
25+
* math.fraction(matrix: Array | Matrix)
1626
*
1727
* Examples:
1828
*
19-
* math.fraction(1, 3)
20-
* math.fraction('2/3')
21-
* math.fraction({n: 2, d: 3})
22-
* math.fraction([0.2, 0.25, 1.25])
29+
* math.fraction(6.283) // returns Fraction 6283/1000
30+
* math.fraction(1, 3) // returns Fraction 1/3
31+
* math.fraction('2/3') // returns Fraction 2/3
32+
* math.fraction({n: 2, d: 3}) // returns Fraction 2/3
33+
* math.fraction([0.2, 0.25, 1.25]) // returns Array [1/5, 1/4, 5/4]
34+
* math.fraction(4, 5.1) // throws Error: Parameters must be integer
2335
*
2436
* See also:
2537
*
2638
* bignumber, number, string, unit
2739
*
2840
* @param {number | string | Fraction | BigNumber | Array | Matrix} [args]
29-
* Arguments specifying the numerator and denominator of
41+
* Arguments specifying the value, or numerator and denominator of
3042
* the fraction
3143
* @return {Fraction | Array | Matrix} Returns a fraction
3244
*/

test/unit-tests/type/fraction/function/fraction.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ describe('fraction', function () {
1111
equalFraction(math.fraction(null), new Fraction(0))
1212
})
1313

14+
it('should fail to create a fraction in case of non-integer quotient', () => {
15+
assert.throws(() => math.fraction(4, 5.1), /Parameters must be integer/)
16+
assert.throws(() => math.fraction(62.8, 10), /Parameters must be integer/)
17+
assert.throws(() => math.fraction(Infinity, 3), /Parameters must be integer/)
18+
})
19+
20+
it('should create a fraction from a quotient regardless of integrality', () => {
21+
equalFraction(math.divide(math.fraction(4), math.fraction(5.1)),
22+
math.fraction(40, 51))
23+
})
24+
1425
it('should create a fraction from a BigNumber', function () {
1526
const b = math.bignumber(2).div(3)
1627
const f = math.fraction(b)

0 commit comments

Comments
 (0)