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>
114 lines
4.7 KiB
JavaScript
114 lines
4.7 KiB
JavaScript
import assert from 'assert'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
import approx from '../../../../tools/approx.js'
|
|
const pi = math.pi
|
|
const acos = math.acos
|
|
const cos = math.cos
|
|
const complex = math.complex
|
|
const matrix = math.matrix
|
|
const unit = math.unit
|
|
const bigmath = math.create({ number: 'BigNumber', precision: 20 })
|
|
const mathPredictable = math.create({ predictable: true })
|
|
const acosBig = bigmath.acos
|
|
const cosBig = bigmath.cos
|
|
const Big = bigmath.bignumber
|
|
|
|
describe('acos', function () {
|
|
it('should return the arccos of a boolean', function () {
|
|
approx.equal(acos(true), 0)
|
|
approx.equal(acos(false), 0.5 * pi)
|
|
})
|
|
|
|
it('should return the arccos of a number', function () {
|
|
approx.equal(acos(-1) / pi, 1)
|
|
approx.equal(acos(-0.5) / pi, 2 / 3)
|
|
approx.equal(acos(0) / pi, 0.5)
|
|
approx.equal(acos(0.5) / pi, 1 / 3)
|
|
approx.equal(acos(1) / pi, 0)
|
|
|
|
approx.deepEqual(acos(-2), complex('3.14159265358979 - 1.31695789692482i'))
|
|
approx.deepEqual(acos(2), complex('1.316957896924817i'))
|
|
})
|
|
|
|
it('should return the arccos of a number when predictable:true', function () {
|
|
assert.strictEqual(typeof mathPredictable.acos(-2), 'number')
|
|
assert(isNaN(mathPredictable.acos(-2)))
|
|
})
|
|
|
|
it('should return the arccos of a bignumber', function () {
|
|
const arg = Big(-1)
|
|
assert.deepStrictEqual(acosBig(arg).toString(), bigmath.pi.toString())
|
|
assert.deepStrictEqual(acosBig(Big(-0.5)), Big('2.0943951023931954923'))
|
|
assert.deepStrictEqual(acosBig(Big(0)), Big('1.5707963267948966192'))
|
|
assert.deepStrictEqual(acosBig(Big(0.5)), Big('1.0471975511965977462'))
|
|
assert.deepStrictEqual(acosBig(Big(1)), Big(0))
|
|
|
|
// Hit Newton's method case
|
|
const bigmath61 = math.create({ number: 'BigNumber', precision: 61 })
|
|
assert.deepStrictEqual(bigmath61.acos(bigmath61.bignumber(0.00000001)),
|
|
bigmath61.bignumber('1.570796316794896619231321524973084775431910533020886243820359'))
|
|
// Wolfram: 1.5707963167948966192313215249730847754319105330208862438203592009158129650174844596314777278941600852176250962802
|
|
// Make sure arg was not changed
|
|
assert.deepStrictEqual(arg, Big(-1))
|
|
})
|
|
|
|
it('should be the inverse function of cos', function () {
|
|
approx.equal(acos(cos(-1)), 1)
|
|
approx.equal(acos(cos(0)), 0)
|
|
approx.equal(acos(cos(0.1)), 0.1)
|
|
approx.equal(acos(cos(0.5)), 0.5)
|
|
approx.equal(acos(cos(2)), 2)
|
|
})
|
|
|
|
it('should be the inverse function of bignumber cos', function () {
|
|
bigmath.config({ precision: 20 })
|
|
assert.deepStrictEqual(acosBig(cosBig(Big(-1))), Big(1))
|
|
assert.deepStrictEqual(acosBig(cosBig(Big(0))), Big('0'))
|
|
assert.deepStrictEqual(acosBig(cosBig(Big(0.1))), Big('0.099999999999999999956'))
|
|
assert.deepStrictEqual(acosBig(cosBig(Big(0.5))), Big('0.49999999999999999999'))
|
|
assert.deepStrictEqual(acosBig(cosBig(Big(2))), Big(2))
|
|
})
|
|
|
|
it('should return for bignumber cos for x > 1', function () {
|
|
assert.ok(acos(Big(1.1)).isNaN())
|
|
assert.ok(acos(Big(-1.1)).isNaN())
|
|
})
|
|
|
|
it('should return the arccos of a complex number', function () {
|
|
approx.deepEqual(acos(complex('2+3i')), complex(1.00014354247380, -1.98338702991654))
|
|
approx.deepEqual(acos(complex('2-3i')), complex(1.00014354247380, 1.98338702991654))
|
|
approx.deepEqual(acos(complex('-2+3i')), complex(2.14144911111600, -1.98338702991654))
|
|
approx.deepEqual(acos(complex('-2-3i')), complex(2.14144911111600, 1.98338702991654))
|
|
approx.deepEqual(acos(complex('i')), complex(1.570796326794897, -0.881373587019543))
|
|
approx.deepEqual(acos(complex('1')), complex(0, 0))
|
|
approx.deepEqual(acos(complex('1+i')), complex(0.904556894302381, -1.061275061905036))
|
|
})
|
|
|
|
it('should throw an error if called with a unit', function () {
|
|
assert.throws(function () { acos(unit('45deg')) })
|
|
assert.throws(function () { acos(unit('5 celsius')) })
|
|
})
|
|
|
|
it('should throw an error if called with a string', function () {
|
|
assert.throws(function () { acos('string') })
|
|
})
|
|
|
|
it('should calculate the arccos element-wise for arrays and matrices', function () {
|
|
// note: the results of acos(2) and acos(3) differs in octave
|
|
// the next tests are verified with mathematica
|
|
const acos123 = [0, complex(0, 1.316957896924817), complex(0, 1.762747174039086)]
|
|
approx.deepEqual(acos([1, 2, 3]), acos123)
|
|
approx.deepEqual(acos(matrix([1, 2, 3])), matrix(acos123))
|
|
})
|
|
|
|
it('should throw an error in case of invalid number of arguments', function () {
|
|
assert.throws(function () { acos() }, /TypeError: Too few arguments/)
|
|
assert.throws(function () { acos(1, 2) }, /TypeError: Too many arguments/)
|
|
})
|
|
|
|
it('should LaTeX acos', function () {
|
|
const expression = math.parse('acos(1)')
|
|
assert.strictEqual(expression.toTex(), '\\cos^{-1}\\left(1\\right)')
|
|
})
|
|
})
|