mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
172 lines
6.6 KiB
JavaScript
172 lines
6.6 KiB
JavaScript
import assert from 'assert'
|
|
import { create } from '../src/core/core'
|
|
import math from '../src/main'
|
|
import approx from '../tools/approx'
|
|
import { createConstants } from '../src/constants'
|
|
import { createBigNumberClass } from '../src/type/bignumber/BigNumber'
|
|
import { createComplexClass } from '../src/type/complex/Complex'
|
|
|
|
describe('constants', function () {
|
|
const bigmath = math.create({ number: 'BigNumber', precision: 64 })
|
|
|
|
const realmath = create()
|
|
realmath.import(createBigNumberClass)
|
|
realmath.import(createComplexClass)
|
|
realmath.import(createConstants)
|
|
|
|
describe('number', function () {
|
|
[math, realmath].forEach(function (instance) {
|
|
it('should have pi', function () {
|
|
approx.equal(instance.pi, 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664)
|
|
approx.equal(instance.PI, instance.pi)
|
|
})
|
|
|
|
it('should have tau', function () {
|
|
approx.equal(instance.tau, 6.28318530717959)
|
|
})
|
|
|
|
it('should have phi, golden ratio', function () {
|
|
approx.equal(instance.phi, 1.61803398874989484820458683436563811772030917980576286213545)
|
|
})
|
|
|
|
it('should have e (euler constant)', function () {
|
|
approx.equal(instance.e, 2.71828182845905)
|
|
})
|
|
|
|
it('should have LN2', function () {
|
|
approx.equal(instance.LN2, 0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573)
|
|
})
|
|
|
|
it('should have LN10', function () {
|
|
approx.equal(instance.LN10, 2.30258509299404568401799145468436420760110148862877297603332790096757260967735248023599720508959829834196778404228624863)
|
|
})
|
|
|
|
it('should have LOG2E', function () {
|
|
approx.equal(instance.LOG2E, 1.44269504088896340735992468100189213742664595415298593413544940693110921918118507988552662289350634449699751830965254425)
|
|
})
|
|
|
|
it('should have LOG10E', function () {
|
|
approx.equal(instance.LOG10E, 0.43429448190325182765112891891660508229439700580366656611445378316586464920887077472922494933843174831870610674476630373)
|
|
})
|
|
|
|
it('should have PI', function () {
|
|
approx.equal(instance.PI, 3.14159265358979)
|
|
})
|
|
|
|
it('should have SQRT1_2', function () {
|
|
approx.equal(instance.SQRT1_2, 0.70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078636750692311545614851)
|
|
})
|
|
|
|
it('should have SQRT2', function () {
|
|
approx.equal(instance.SQRT2, 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702)
|
|
})
|
|
|
|
it('should have Infinity', function () {
|
|
assert.strictEqual(instance.Infinity, Infinity)
|
|
})
|
|
|
|
it('should have NaN', function () {
|
|
assert.ok(isNaN(instance.NaN))
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('bignumbers', function () {
|
|
it('should have bignumber pi', function () {
|
|
assert.strictEqual(bigmath.pi.toString(), '3.141592653589793238462643383279502884197169399375105820974944592')
|
|
})
|
|
|
|
it('should have bignumber tau', function () {
|
|
assert.strictEqual(bigmath.tau.toString(), '6.283185307179586476925286766559005768394338798750211641949889184')
|
|
})
|
|
|
|
it('should have bignumber phi, golden ratio', function () {
|
|
assert.strictEqual(bigmath.phi.toString(), '1.618033988749894848204586834365638117720309179805762862135448623')
|
|
})
|
|
|
|
it('should have bignumber e', function () {
|
|
assert.strictEqual(bigmath.e.toString(), '2.718281828459045235360287471352662497757247093699959574966967628')
|
|
})
|
|
|
|
it('should have bignumber LN2', function () {
|
|
assert.strictEqual(bigmath.LN2.toString(), '0.6931471805599453094172321214581765680755001343602552541206800095')
|
|
})
|
|
|
|
it('should have bignumber LN10', function () {
|
|
assert.strictEqual(bigmath.LN10.toString(), '2.302585092994045684017991454684364207601101488628772976033327901')
|
|
})
|
|
|
|
it('should have bignumber LOG2E', function () {
|
|
assert.strictEqual(bigmath.LOG2E.toString(), '1.442695040888963407359924681001892137426645954152985934135449407')
|
|
})
|
|
|
|
it('should have bignumber LOG10E', function () {
|
|
assert.strictEqual(bigmath.LOG10E.toString(), '0.4342944819032518276511289189166050822943970058036665661144537832')
|
|
})
|
|
|
|
it('should have bignumber PI (upper case)', function () {
|
|
assert.strictEqual(bigmath.PI.toString(), '3.141592653589793238462643383279502884197169399375105820974944592')
|
|
})
|
|
|
|
it('should have bignumber SQRT1_2', function () {
|
|
assert.strictEqual(bigmath.SQRT1_2.toString(), '0.707106781186547524400844362104849039284835937688474036588339869')
|
|
})
|
|
|
|
it('should have bignumber SQRT2', function () {
|
|
assert.strictEqual(bigmath.SQRT2.toString(), '1.414213562373095048801688724209698078569671875376948073176679738')
|
|
})
|
|
|
|
it('should have bignumber Infinity', function () {
|
|
assert(bigmath.Infinity instanceof bigmath.type.BigNumber)
|
|
assert.strictEqual(bigmath.Infinity.toString(), 'Infinity')
|
|
})
|
|
|
|
it('should have bignumber NaN', function () {
|
|
assert(bigmath.NaN instanceof bigmath.type.BigNumber)
|
|
assert.strictEqual(bigmath.NaN.toString(), 'NaN')
|
|
assert.ok(isNaN(bigmath.NaN))
|
|
})
|
|
})
|
|
|
|
describe('complex', function () {
|
|
[math, bigmath].forEach(function (math) {
|
|
it('should have i', function () {
|
|
assert.strictEqual(math.i.re, 0)
|
|
assert.strictEqual(math.i.im, 1)
|
|
assert.deepStrictEqual(math.i, math.complex(0, 1))
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('universal behavior', function () {
|
|
[math, bigmath, realmath].forEach(function (instance) {
|
|
it('should have true and false', function () {
|
|
assert.strictEqual(instance.true, true)
|
|
assert.strictEqual(instance.false, false)
|
|
})
|
|
|
|
it('should have null', function () {
|
|
assert.strictEqual(instance['null'], null)
|
|
})
|
|
|
|
it('should return message when uninitialized', function () {
|
|
assert.strictEqual(instance.uninitialized, 'Error: Constant uninitialized is removed since v4.0.0. Use null instead')
|
|
})
|
|
})
|
|
})
|
|
|
|
it('should evaluation functions with constants', function () {
|
|
// Do these tests really belong in constants.test.js ?
|
|
approx.equal(math.sin(math.pi / 2), 1)
|
|
|
|
assert.deepStrictEqual(math.round(math.add(1, math.pow(math.e, math.multiply(math.pi, math.i))), 5), math.complex(0))
|
|
assert.deepStrictEqual(math.round(math.eval('1+e^(pi*i)'), 5), math.complex(0))
|
|
|
|
assert.deepStrictEqual(math.sqrt(-1), math.i)
|
|
assert.deepStrictEqual(math.eval('i'), math.complex(0, 1))
|
|
|
|
assert.strictEqual(math.eval('true'), true)
|
|
assert.strictEqual(math.eval('false'), false)
|
|
})
|
|
})
|