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>
109 lines
4.9 KiB
JavaScript
109 lines
4.9 KiB
JavaScript
// test log1p
|
|
import assert from 'assert'
|
|
|
|
import approx from '../../../../tools/approx.js'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
const mathPredictable = math.create({ predictable: true })
|
|
const complex = math.complex
|
|
const matrix = math.matrix
|
|
const unit = math.unit
|
|
const log1p = math.log1p
|
|
|
|
describe('log1p', function () {
|
|
it('should return the log1p of a boolean value', function () {
|
|
approx.equal(log1p(true), 0.6931471805599)
|
|
assert.strictEqual(log1p(false), 0)
|
|
assert.strictEqual(log1p(1, false), -0)
|
|
})
|
|
|
|
it('should return the log1p of positive numbers', function () {
|
|
assert.strictEqual(log1p(-1), -Infinity)
|
|
assert.strictEqual(log1p(-0), -0)
|
|
assert.strictEqual(log1p(+0), +0)
|
|
approx.deepEqual(log1p(1), 0.693147180559945)
|
|
approx.deepEqual(log1p(2), 1.098612288668110)
|
|
approx.deepEqual(math.exp(log1p(99)), 100)
|
|
})
|
|
|
|
it('should return the log1p of negative numbers', function () {
|
|
approx.deepEqual(log1p(-2), complex('0.000000000000000 + 3.141592653589793i'))
|
|
approx.deepEqual(log1p(-3), complex('0.693147180559945 + 3.141592653589793i'))
|
|
approx.deepEqual(log1p(-4), complex('1.098612288668110 + 3.141592653589793i'))
|
|
})
|
|
|
|
it('should return the log1p of negative numbers with predictable: true', function () {
|
|
assert.strictEqual(typeof mathPredictable.log1p(-2), 'number')
|
|
assert(isNaN(mathPredictable.log1p(-2)))
|
|
})
|
|
|
|
it('should return the log1p base N of a number', function () {
|
|
approx.deepEqual(log1p(99, 10), 2)
|
|
approx.deepEqual(log1p(999, 10), 3)
|
|
approx.deepEqual(log1p(7, 2), 3)
|
|
approx.deepEqual(log1p(15, 2), 4)
|
|
})
|
|
|
|
it('should throw an error if invalid number of arguments', function () {
|
|
assert.throws(function () { log1p() }, /TypeError: Too few arguments in function log1p \(expected: any, index: 0\)/)
|
|
assert.throws(function () { log1p(1, 2, 3) }, /TypeError: Too many arguments in function log1p \(expected: 2, actual: 3\)/)
|
|
})
|
|
|
|
it('should return the log1p of positive bignumbers', function () {
|
|
const bigmath = math.create({ precision: 100 })
|
|
|
|
assert.deepStrictEqual(bigmath.log1p(bigmath.bignumber(-1)).toString(), '-Infinity')
|
|
assert.deepStrictEqual(bigmath.log1p(bigmath.bignumber(0)), bigmath.bignumber('0'))
|
|
assert.deepStrictEqual(bigmath.log1p(bigmath.bignumber(1)), bigmath.bignumber('0.6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875'))
|
|
assert.deepStrictEqual(bigmath.log1p(bigmath.bignumber(2)), bigmath.bignumber('1.098612288668109691395245236922525704647490557822749451734694333637494293218608966873615754813732089'))
|
|
|
|
// note: the following gives a round-off error with regular numbers
|
|
assert.deepStrictEqual(bigmath.log1p(bigmath.bignumber(999), bigmath.bignumber(10)), bigmath.bignumber(3))
|
|
})
|
|
|
|
it('should return the log1p of negative bignumbers', function () {
|
|
const bigmath = math.create({ precision: 100 })
|
|
|
|
approx.deepEqual(bigmath.log1p(bigmath.bignumber(-2)), complex('0.000000000000000 + 3.141592653589793i'))
|
|
approx.deepEqual(bigmath.log1p(bigmath.bignumber(-3)), complex('0.693147180559945 + 3.141592653589793i'))
|
|
approx.deepEqual(bigmath.log1p(bigmath.bignumber(-4)), complex('1.098612288668110 + 3.141592653589793i'))
|
|
})
|
|
|
|
it('should return the log1p of negative bignumbers with predictable:true', function () {
|
|
assert(mathPredictable.log1p(math.bignumber(-2)).isNaN(), 'should be NaN')
|
|
})
|
|
|
|
it('should return the log1p of a complex number', function () {
|
|
approx.deepEqual(log1p(math.i), complex('0.346573590279973 + 0.785398163397448i'))
|
|
approx.deepEqual(log1p(complex(0, -1)), complex('0.346573590279973 - 0.785398163397448i'))
|
|
approx.deepEqual(log1p(complex(1, 1)), complex('0.80471895621705 + 0.463647609000806i'))
|
|
approx.deepEqual(log1p(complex(1, -1)), complex('0.80471895621705 - 0.463647609000806i'))
|
|
approx.deepEqual(log1p(complex(-1, -1)), complex('-1.570796326794897i'))
|
|
approx.deepEqual(log1p(complex(-1, 1)), complex('1.570796326794897i'))
|
|
approx.deepEqual(log1p(complex(1, 0)), complex('0.693147180559945'))
|
|
})
|
|
|
|
it('should throw an error when used on a unit', function () {
|
|
assert.throws(function () { log1p(unit('5cm')) })
|
|
})
|
|
|
|
it('should throw an error when used on a string', function () {
|
|
assert.throws(function () { log1p('text') })
|
|
})
|
|
|
|
it('should return the log1p of each element of a matrix', function () {
|
|
const res = [0, 0.693147180559945, 1.098612288668110, 1.386294361119891]
|
|
approx.deepEqual(log1p([0, 1, 2, 3]), res)
|
|
approx.deepEqual(log1p(matrix([0, 1, 2, 3])), matrix(res))
|
|
approx.deepEqual(log1p(matrix([[0, 1], [2, 3]])),
|
|
matrix([[0, 0.693147180559945], [1.098612288668110, 1.386294361119891]]))
|
|
})
|
|
|
|
it('should LaTeX log1p', function () {
|
|
const expr1 = math.parse('log1p(e)')
|
|
const expr2 = math.parse('log1p(32,2)')
|
|
|
|
assert.strictEqual(expr1.toTex(), '\\ln\\left( e+1\\right)')
|
|
assert.strictEqual(expr2.toTex(), '\\log_{2}\\left(32+1\\right)')
|
|
})
|
|
})
|