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

90 lines
3.9 KiB
JavaScript

// test exp
import assert from 'assert'
import approx from '../../../../tools/approx.js'
import math from '../../../../src/defaultInstance.js'
const complex = math.complex
const matrix = math.matrix
const sparse = math.sparse
const unit = math.unit
const exp = math.exp
describe('exp', function () {
it('should exponentiate a boolean', function () {
approx.equal(exp(true), 2.71828182845905)
approx.equal(exp(false), 1)
})
it('should exponentiate a number', function () {
approx.equal(exp(-3), 0.0497870683678639)
approx.equal(exp(-2), 0.1353352832366127)
approx.equal(exp(-1), 0.3678794411714423)
approx.equal(exp(0), 1)
approx.equal(exp(1), 2.71828182845905)
approx.equal(exp(2), 7.38905609893065)
approx.equal(exp(3), 20.0855369231877)
approx.equal(exp(math.log(100)), 100)
})
it('should exponentiate a bignumber', function () {
const bigmath = math.create({ precision: 100 })
assert.deepStrictEqual(bigmath.exp(bigmath.bignumber(1)), bigmath.bignumber('2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427'))
})
it('should throw an error if there\'s wrong number of arguments', function () {
assert.throws(function () { exp() }, /TypeError: Too few arguments/)
assert.throws(function () { exp(1, 2) }, /TypeError: Too many arguments/)
})
it('should throw an in case of wrong type of arguments', function () {
assert.throws(function () { exp(null) }, /TypeError: Unexpected type of argument/)
})
it('should exponentiate a complex number correctly', function () {
approx.deepEqual(exp(math.i), complex('0.540302305868140 + 0.841470984807897i'))
approx.deepEqual(exp(complex(0, -1)), complex('0.540302305868140 - 0.841470984807897i'))
approx.deepEqual(exp(complex(1, 1)), complex('1.46869393991589 + 2.28735528717884i'))
approx.deepEqual(exp(complex(1, -1)), complex('1.46869393991589 - 2.28735528717884i'))
approx.deepEqual(exp(complex(-1, -1)), complex('0.198766110346413 - 0.309559875653112i'))
approx.deepEqual(exp(complex(-1, 1)), complex('0.198766110346413 + 0.309559875653112i'))
approx.deepEqual(exp(complex(1, 0)), complex('2.71828182845905'))
// test some logic identities
const multiply = math.multiply
const pi = math.pi
const i = math.i
approx.deepEqual(exp(multiply(0.5, multiply(pi, i))), complex(0, 1))
approx.deepEqual(exp(multiply(1, multiply(pi, i))), complex(-1, 0))
approx.deepEqual(exp(multiply(1.5, multiply(pi, i))), complex(0, -1))
approx.deepEqual(exp(multiply(2, multiply(pi, i))), complex(1, 0))
approx.deepEqual(exp(multiply(-0.5, multiply(pi, i))), complex(0, -1))
approx.deepEqual(exp(multiply(-1, multiply(pi, i))), complex(-1, 0))
approx.deepEqual(exp(multiply(-1.5, multiply(pi, i))), complex(0, 1))
})
it('should throw an error on a unit', function () {
assert.throws(function () { exp(unit('5cm')) })
})
it('should throw an error with a string', function () {
assert.throws(function () { exp('text') })
})
it('should exponentiate matrices, arrays and ranges correctly', function () {
// array
approx.deepEqual(exp([0, 1, 2, 3]), [1, 2.71828182845905, 7.38905609893065, 20.0855369231877])
approx.deepEqual(exp([[0, 1], [2, 3]]), [[1, 2.71828182845905], [7.38905609893065, 20.0855369231877]])
// dense matrix
approx.deepEqual(exp(matrix([0, 1, 2, 3])), matrix([1, 2.71828182845905, 7.38905609893065, 20.0855369231877]))
approx.deepEqual(exp(matrix([[0, 1], [2, 3]])), matrix([[1, 2.71828182845905], [7.38905609893065, 20.0855369231877]]))
// sparse matrix, TODO: it should return a dense matrix
approx.deepEqual(exp(sparse([[0, 1], [2, 3]])), sparse([[1, 2.71828182845905], [7.38905609893065, 20.0855369231877]]))
})
it('should LaTeX exp', function () {
const expression = math.parse('exp(0)')
assert.strictEqual(expression.toTex(), '\\exp\\left(0\\right)')
})
})