mathjs/examples/fractions.js
greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

71 lines
2.3 KiB
JavaScript

// Fractions
// load math.js (using node.js)
const math = require('../index')
// configure the default type of numbers as Fractions
math.config({
number: 'Fraction' // Default type of number:
// 'number' (default), 'BigNumber', or 'Fraction'
})
console.log('basic usage')
printRatio(math.fraction(0.125)) // Fraction, 1/8
printRatio(math.fraction(0.32)) // Fraction, 8/25
printRatio(math.fraction('1/3')) // Fraction, 1/3
printRatio(math.fraction('0.(3)')) // Fraction, 1/3
printRatio(math.fraction(2, 3)) // Fraction, 2/3
printRatio(math.fraction('0.(285714)')) // Fraction, 2/7
console.log()
console.log('round-off errors with numbers')
print(math.add(0.1, 0.2)) // number, 0.30000000000000004
print(math.divide(0.3, 0.2)) // number, 1.4999999999999998
console.log()
console.log('no round-off errors with fractions :)')
print(math.add(math.fraction(0.1), math.fraction(0.2))) // Fraction, 0.3
print(math.divide(math.fraction(0.3), math.fraction(0.2))) // Fraction, 1.5
console.log()
console.log('represent an infinite number of repeating digits')
print(math.fraction('1/3')) // Fraction, 0.(3)
print(math.fraction('2/7')) // Fraction, 0.(285714)
print(math.fraction('23/11')) // Fraction, 2.(09)
console.log()
// one can work conveniently with fractions using the expression parser.
// note though that Fractions are only supported by basic arithmetic functions
console.log('use fractions in the expression parser')
printRatio(math.eval('0.1 + 0.2')) // Fraction, 3/10
printRatio(math.eval('0.3 / 0.2')) // Fraction, 3/2
printRatio(math.eval('23 / 11')) // Fraction, 23/11
console.log()
// output formatting
console.log('output formatting of fractions')
const a = math.fraction('2/3')
console.log(math.format(a)) // Fraction, 2/3
console.log(math.format(a, { fraction: 'ratio' })) // Fraction, 2/3
console.log(math.format(a, { fraction: 'decimal' })) // Fraction, 0.(6)
console.log(a.toString()) // Fraction, 0.(6)
console.log()
/**
* Helper function to output a value in the console.
* Fractions will be formatted as ratio, like '1/3'.
* @param {*} value
*/
function printRatio (value) {
console.log(math.format(value, { fraction: 'ratio' }))
}
/**
* Helper function to output a value in the console.
* Fractions will be formatted as decimal, like '0.(3)'.
* @param {*} value
*/
function print (value) {
console.log(math.format(value, { fraction: 'decimal' }))
}