mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* Add `.js` extension to source file imports * Specify package `exports` in `package.json` Specify package type as `commonjs` (It's good to be specific) * Move all compiled scripts into `lib` directory Remove ./number.js (You can use the compiled ones in `./lib/*`) Tell node that the `esm` directory is type `module` and enable tree shaking. Remove unused files from packages `files` property * Allow importing of package.json * Make library ESM first * - Fix merge conflicts - Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs` - Refactor unit tests to be able to run with plain nodejs (no transpiling) - Fix browser examples * Fix browser and browserstack tests * Fix running unit tests on Node 10 (which has no support for modules) * Fix node.js examples (those are still commonjs) * Remove the need for `browserBundle.cjs` * Generate minified bundle only * [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963) Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.** - [Release notes](https://github.com/bitinn/node-fetch/releases) - [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md) - [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Cleanup console.log * Add integration tests to test the entry points (commonjs/esm, full/number only) * Create backward compatibility error messages in the files moved/removed since v8 * Describe breaking changes in HISTORY.md * Bump karma from 5.2.1 to 5.2.2 (#1965) Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2. - [Release notes](https://github.com/karma-runner/karma/releases) - [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md) - [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
134 lines
4.9 KiB
JavaScript
134 lines
4.9 KiB
JavaScript
// test cbrt
|
|
import assert from 'assert'
|
|
|
|
import approx from '../../../../tools/approx.js'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
const cbrt = math.cbrt
|
|
const bignumber = math.bignumber
|
|
const complex = math.complex
|
|
|
|
describe('cbrt', function () {
|
|
it('should return the cubic root of a boolean', function () {
|
|
assert.strictEqual(cbrt(true), 1)
|
|
assert.strictEqual(cbrt(false), 0)
|
|
})
|
|
|
|
it('should return the cubic root of a positive number', function () {
|
|
assert.strictEqual(cbrt(0), 0)
|
|
assert.strictEqual(cbrt(1), 1)
|
|
assert.strictEqual(cbrt(8), 2)
|
|
assert.strictEqual(cbrt(27), 3)
|
|
assert.strictEqual(cbrt(64), 4)
|
|
assert.strictEqual(cbrt(125), 5)
|
|
|
|
approx.equal(cbrt(10), 2.1544346900318834)
|
|
})
|
|
|
|
it('should return the cubic root of a negative number', function () {
|
|
assert.strictEqual(cbrt(-8), -2)
|
|
assert.strictEqual(cbrt(-64), -4)
|
|
})
|
|
|
|
it('should return the cubic root of infinity', function () {
|
|
assert.strictEqual(cbrt(Infinity), Infinity)
|
|
assert.strictEqual(cbrt(-Infinity), -Infinity)
|
|
})
|
|
|
|
it('should return all cubic roots of a number', function () {
|
|
approx.deepEqual(cbrt(8, true), math.matrix([
|
|
complex('2'),
|
|
complex('-1 + 1.7321i'),
|
|
complex('-1 - 1.7321i')
|
|
]))
|
|
|
|
approx.deepEqual(cbrt(-8, true), math.matrix([
|
|
complex('1 + 1.7321i'),
|
|
complex('-2'),
|
|
complex('1 - 1.7321i')
|
|
]))
|
|
})
|
|
|
|
it('should return the cubic root of a positive bignumber', function () {
|
|
assert.deepStrictEqual(cbrt(bignumber(0)), bignumber(0))
|
|
assert.deepStrictEqual(cbrt(bignumber(1)), bignumber(1))
|
|
assert.deepStrictEqual(cbrt(bignumber(8)), bignumber(2))
|
|
assert.deepStrictEqual(cbrt(bignumber(27)), bignumber(3))
|
|
assert.deepStrictEqual(cbrt(bignumber(64)), bignumber(4))
|
|
assert.deepStrictEqual(cbrt(bignumber(125)), bignumber(5))
|
|
|
|
assert.deepStrictEqual(cbrt(bignumber(10)), bignumber('2.154434690031883721759293566519350495259344942192108582489235506'))
|
|
})
|
|
|
|
it('should return the cubic root of a negative bignumber', function () {
|
|
assert.deepStrictEqual(cbrt(bignumber(-8)), bignumber(-2))
|
|
assert.deepStrictEqual(cbrt(bignumber(-64)), bignumber(-4))
|
|
})
|
|
|
|
it('should return the cubic root of a complex number', function () {
|
|
approx.deepEqual(cbrt(complex('2 + 3i')), complex('1.451856618352664928164697 + 0.493403534104004716735578i'))
|
|
approx.deepEqual(cbrt(complex('-2 + 3i')), complex('1.15322830402742 + 1.01064294709397i'))
|
|
approx.deepEqual(cbrt(complex('8i')), complex('1.73205080756888 + i'))
|
|
})
|
|
|
|
it('should return all three roots of a complex number', function () {
|
|
approx.deepEqual(cbrt(complex('2 + 3i'), true), math.matrix([
|
|
complex('1.4519 + 0.4934i'),
|
|
complex('-1.1532 + 1.0106i'),
|
|
complex('-0.2986 - 1.5040i')
|
|
]))
|
|
|
|
approx.deepEqual(cbrt(complex('8i'), true), math.matrix([
|
|
complex(' 1.7321 + i'),
|
|
complex('-1.7321 + i'),
|
|
complex('-2i')
|
|
]))
|
|
|
|
const math2 = math.create({ matrix: 'Array' })
|
|
|
|
approx.deepEqual(math2.cbrt(complex('8i'), true), [
|
|
complex(' 1.7321 + i'),
|
|
complex('-1.7321 + i'),
|
|
complex('-2i')
|
|
])
|
|
})
|
|
|
|
it('should return the cubic root of a unit', function () {
|
|
assert.strictEqual(cbrt(math.unit('27 m^3')).toString(), math.unit('3 m').toString())
|
|
assert.strictEqual(cbrt(math.unit('-27 m^3')).toString(), math.unit('-3 m').toString())
|
|
|
|
assert(math.isBigNumber(cbrt(math.unit(math.bignumber(27), 'm^3')).value))
|
|
assert.deepStrictEqual(cbrt(math.unit(math.bignumber(27), 'm^3')).value, math.bignumber(3))
|
|
assert(math.isBigNumber(cbrt(math.unit(math.bignumber(-27), 'm^3')).value))
|
|
assert.deepStrictEqual(cbrt(math.unit(math.bignumber(-27), 'm^3')).value, math.bignumber(-3))
|
|
|
|
assert(math.isComplex(cbrt(math.unit(math.complex(-46, 9), 's^3')).value))
|
|
approx.deepEqual(cbrt(math.unit(math.complex(-46, 9), 's^3')).value, math.complex(2, 3))
|
|
})
|
|
|
|
it('should throw an error when used with a string', function () {
|
|
assert.throws(function () {
|
|
cbrt('a string')
|
|
})
|
|
})
|
|
|
|
it('should return the cubic root of each element of a matrix', function () {
|
|
assert.deepStrictEqual(cbrt([8, 27, 64, 125]), [2, 3, 4, 5])
|
|
assert.deepStrictEqual(cbrt([[8, 27], [64, 125]]), [[2, 3], [4, 5]])
|
|
assert.deepStrictEqual(cbrt(math.matrix([[8, 27], [64, 125]])), math.matrix([[2, 3], [4, 5]]))
|
|
})
|
|
|
|
it('should throw an error in case of invalid number of arguments', function () {
|
|
assert.throws(function () { cbrt() }, /TypeError: Too few arguments/)
|
|
assert.throws(function () { cbrt(1, true, 2) }, /TypeError: Too many arguments/)
|
|
})
|
|
|
|
it('should throw an in case of wrong type of arguments', function () {
|
|
assert.throws(function () { cbrt(null) }, /TypeError: Unexpected type of argument/)
|
|
})
|
|
|
|
it('should LaTeX cbrt', function () {
|
|
const expression = math.parse('cbrt(2)')
|
|
assert.strictEqual(expression.toTex(), '\\sqrt[3]{2}')
|
|
})
|
|
})
|