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

124 lines
5.6 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 sin = math.sin
const bigmath = math.create({ precision: 242 })
describe('sin', function () {
it('should return the sine of a boolean', function () {
approx.equal(sin(true), 0.841470984807897)
approx.equal(sin(false), 0)
})
it('should return the sine of a number', function () {
approx.equal(sin(0), 0)
approx.equal(sin(pi / 8), 0.382683432365090)
approx.equal(sin(pi / 4), Math.SQRT1_2)
approx.equal(sin(pi / 2), 1)
approx.equal(sin(pi * 3 / 4), 0.707106781186548)
approx.equal(sin(pi), 0)
approx.equal(sin(pi * 5 / 4), -0.707106781186548)
approx.equal(sin(pi * 3 / 2), -1)
approx.equal(sin(pi * 7 / 4), -0.707106781186548)
approx.equal(sin(pi * 2), 0)
})
it('should return the sine of a bignumber', function () {
const Big = bigmath.bignumber
assert.deepStrictEqual(bigmath.sin(Big(0)), Big(0))
// 103.64 % tau = 3.109... <- pretty close to the pi boundary
let resultVal = bigmath.sin(Big(103.64))
assert.deepStrictEqual(resultVal, Big('0.0325518169566161584427313159942672130512044591216893328934710' +
'307148043832988055013958395123418887322610809247793661058554' +
'935758353628919004205593985094895305777198408601067175226892' +
'4960612126026291341865833521451170868744460464214033460336158'))
const arg = Big(-103.64)
resultVal = bigmath.sin(arg)
assert.deepStrictEqual(arg, Big(-103.64)) // Make sure arg wasn't changed
assert.deepStrictEqual(resultVal, Big('-0.0325518169566161584427313159942672130512044591216893328934710' +
'3071480438329880550139583951234188873226108092477936610585549' +
'3575835362891900420559398509489530577719840860106717522689249' +
'60612126026291341865833521451170868744460464214033460336158'))
const bigmath2 = bigmath.create({ number: 'BigNumber', precision: 15 })
// we've had a bug in reducing the period, affecting integer values around multiples of tau (like 6, 7)
for (let x = -20; x < 20; x += 1) {
approx.equal(bigmath2.sin(bigmath2.bignumber(x)).toNumber(), Math.sin(x))
}
const bigPi = bigmath2.pi
assert.deepStrictEqual(bigmath2.sin(bigPi.div(8)).toString(), '0.38268343236509')
assert.deepStrictEqual(bigmath2.sin(bigPi.div(4)).toString(), '0.707106781186547')
assert.deepStrictEqual(bigmath2.sin(bigPi.div(2)).toString(), '1')
assert.deepStrictEqual(bigmath2.sin(bigPi.times(3).div(4)).toString(), '0.707106781186551')
assert.ok(bigmath2.sin(bigPi).lt(1e-14))
assert.deepStrictEqual(bigmath2.sin(bigPi.times(5).div(4)).toString(), '-0.707106781186554')
assert.deepStrictEqual(bigmath2.sin(bigPi.times(3).div(2)).toString(), '-1')
assert.deepStrictEqual(bigmath2.sin(bigPi.times(7).div(4)).toString(), '-0.707106781186553')
assert.ok(bigmath2.sin(bigPi.times(2)).lt(1e-13))
assert.ok(bigmath2.sin(bigmath2.tau).lt(1e-14))
assert.ok(bigmath2.sin(bigmath2.tau.times(2)).lt(1e-13))
const bigmath61 = bigmath.create({ number: 'BigNumber', precision: 61 })
assert.deepStrictEqual(bigmath61.sin(bigmath61.bignumber(-2)).toString(), '-0.909297426825681695396019865911744842702254971447890268378973')
})
it('should return the sine of a complex number', function () {
const re = 9.15449914691143
const im = 4.16890695996656
approx.deepEqual(sin(complex('2+3i')), complex(re, -im))
approx.deepEqual(sin(complex('2-3i')), complex(re, im))
approx.deepEqual(sin(complex('-2+3i')), complex(-re, -im))
approx.deepEqual(sin(complex('-2-3i')), complex(-re, im))
approx.deepEqual(sin(complex('i')), complex(0, 1.175201193643801))
approx.deepEqual(sin(complex('1')), complex(0.841470984807897, 0))
approx.deepEqual(sin(complex('1+i')), complex(1.298457581415977, 0.634963914784736))
approx.deepEqual(sin(complex('1e-10i')), complex('1e-10i'))
})
it('should return the sine of an angle', function () {
approx.equal(sin(unit('45deg')), 0.707106781186548)
approx.equal(sin(unit('-45deg')), -0.707106781186548)
assert(math.isBigNumber(sin(unit(math.bignumber(45), 'deg'))))
approx.equal(sin(unit(math.bignumber(45), 'deg')).toNumber(), 0.707106781186548)
approx.deepEqual(sin(unit(complex('1+i'), 'rad')), complex(1.298457581415977, 0.634963914784736))
})
it('should throw an error if called with an invalid unit', function () {
assert.throws(function () { sin(unit('5 celsius')) })
})
it('should throw an error if called with a string', function () {
assert.throws(function () { sin('string') })
})
const sin123 = [0.84147098480789, 0.909297426825682, 0.141120008059867]
it('should return the sin of each element of an array', function () {
approx.deepEqual(sin([1, 2, 3]), sin123)
})
it('should return the sin of each element of a matrix', function () {
approx.deepEqual(sin(matrix([1, 2, 3])), matrix(sin123))
})
it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { sin() }, /TypeError: Too few arguments/)
assert.throws(function () { sin(1, 2) }, /TypeError: Too many arguments/)
})
it('should LaTeX sin', function () {
const expression = math.parse('sin(0.5)')
assert.strictEqual(expression.toTex(), '\\sin\\left(0.5\\right)')
})
})