Jos de Jong 6f00715754
Specify import require paths (continuation of #1941) (#1962)
* 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>
2020-09-20 18:01:29 +02:00

112 lines
4.5 KiB
JavaScript

import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
import approx from '../../../../tools/approx.js'
const pi = math.pi
const complex = math.complex
const matrix = math.matrix
const unit = math.unit
const atan = math.atan
const tan = math.tan
const bigmath = math.create({ number: 'BigNumber', precision: 20 })
const atanBig = bigmath.atan
const Big = bigmath.bignumber
describe('atan', function () {
it('should return the arctan of a boolean', function () {
approx.equal(atan(true), 0.25 * pi)
approx.equal(atan(false), 0)
})
it('should return the arctan of a number', function () {
approx.equal(atan(-1) / pi, -0.25)
approx.equal(atan(-0.5) / pi, -0.147583617650433)
approx.equal(atan(0) / pi, 0)
approx.equal(atan(0.5) / pi, 0.147583617650433)
approx.equal(atan(1) / pi, 0.25)
})
it('should return the arctan of a bignumber', function () {
const arg1 = Big(-1)
const arg2 = Big(-0.5)
const arg3 = Big(0)
const arg6 = Big(2)
const arg7 = Big(Infinity)
assert.deepStrictEqual(atanBig(arg1), Big('-0.78539816339744830962'))
assert.deepStrictEqual(atanBig(arg2), Big('-0.46364760900080611621'))
assert.deepStrictEqual(atanBig(arg3), Big(0))
assert.deepStrictEqual(atanBig(Big(0.5)), Big('0.46364760900080611621'))
assert.deepStrictEqual(atanBig(Big(1)), Big('0.78539816339744830962'))
assert.deepStrictEqual(atanBig(arg6), Big('1.107148717794090503'))
assert.deepStrictEqual(atanBig(arg7).toString(), '1.5707963267948966192')
// Ensure the arguments where not changed
assert.deepStrictEqual(arg1, Big(-1))
assert.deepStrictEqual(arg2, Big(-0.5))
assert.deepStrictEqual(arg3, Big(0))
assert.deepStrictEqual(arg6, Big(2))
assert.deepStrictEqual(arg7.toString(), 'Infinity')
// Hit Newton's method case
const bigmath61 = bigmath.create({ number: 'BigNumber', precision: 61 })
assert.deepStrictEqual(bigmath61.atan(bigmath61.bignumber(0.9)),
bigmath61.bignumber('0.7328151017865065916407920727342802519857556793582560863105069'))
})
it('should be the inverse function of tan', function () {
approx.equal(atan(tan(-1)), -1)
approx.equal(atan(tan(0)), 0)
approx.equal(atan(tan(0.1)), 0.1)
approx.equal(atan(tan(0.5)), 0.5)
approx.equal(atan(tan(2)), -1.14159265358979)
})
it('should be the inverse function of bignumber tan', function () {
bigmath.config({ precision: 20 })
assert.deepStrictEqual(atanBig(bigmath.tan(Big(-1))), Big(-1))
assert.deepStrictEqual(atanBig(bigmath.tan(Big(0))), Big(0))
assert.deepStrictEqual(atanBig(bigmath.tan(Big(0.1))), Big(0.1))
assert.deepStrictEqual(atanBig(bigmath.tan(Big(0.5))), Big(0.5))
assert.deepStrictEqual(atanBig(bigmath.tan(Big(2))), Big('-1.1415926535897932385'))
assert.deepStrictEqual(atanBig(bigmath.tan(bigmath.pi.div(2))).toString(), '-1.570796326794895205')
})
it('should return the arctan of a complex number', function () {
const re = 1.409921049596575
const im = 0.229072682968539
approx.deepEqual(atan(complex('2+3i')), complex(re, im))
approx.deepEqual(atan(complex('2-3i')), complex(re, -im))
approx.deepEqual(atan(complex('-2+3i')), complex(-re, im))
approx.deepEqual(atan(complex('-2-3i')), complex(-re, -im))
approx.deepEqual(atan(complex('i')), complex(0, Infinity))
approx.deepEqual(atan(complex('-i')), complex(0, -Infinity))
approx.deepEqual(atan(complex('1')), complex(0.785398163397448, 0))
approx.deepEqual(atan(complex('1+i')), complex(1.017221967897851, 0.402359478108525))
})
it('should throw an error if called with a unit', function () {
assert.throws(function () { atan(unit('45deg')) })
assert.throws(function () { atan(unit('5 celsius')) })
})
it('should throw an error if called with a string', function () {
assert.throws(function () { atan('string') })
})
it('should calculate the arctan element-wise for arrays and matrices', function () {
// matrix, array, range
const atan123 = [0.785398163397448, 1.107148717794090, 1.249045772398254]
approx.deepEqual(atan([1, 2, 3]), atan123)
approx.deepEqual(atan(matrix([1, 2, 3])), matrix(atan123))
})
it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { atan() }, /TypeError: Too few arguments/)
assert.throws(function () { atan(1, 2) }, /TypeError: Too many arguments/)
})
it('should LaTeX atan', function () {
const expression = math.parse('atan(10)')
assert.strictEqual(expression.toTex(), '\\tan^{-1}\\left(10\\right)')
})
})