mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
Refactored the constants into factories
This commit is contained in:
parent
c136e9564f
commit
b92cd833eb
169
src/constants.js
169
src/constants.js
@ -1,84 +1,119 @@
|
||||
'use strict'
|
||||
|
||||
import { lazy } from './utils/object'
|
||||
import { factory } from './utils/factory'
|
||||
import { version } from './version'
|
||||
import * as bigConstants from './utils/bignumber/constants'
|
||||
import {
|
||||
createBigNumberE,
|
||||
createBigNumberPhi,
|
||||
createBigNumberPi,
|
||||
createBigNumberTau
|
||||
} from './utils/bignumber/constants'
|
||||
|
||||
const name = 'constants'
|
||||
const dependencies = ['on', 'math', 'config.number', 'type.Complex', 'type.BigNumber']
|
||||
export const createTrue = factory('true', [], () => true)
|
||||
export const createFalse = factory('false', [], () => false)
|
||||
export const createNull = factory('null', [], () => null)
|
||||
|
||||
// FIXME: implement support for a factory without name
|
||||
export const createConstants = factory(name, dependencies, ({ on, math, config, type: { Complex, BigNumber } }) => {
|
||||
// listen for changed in the configuration, automatically reload
|
||||
// constants when needed
|
||||
on('config', function (curr, prev) {
|
||||
if (curr.number !== prev.number) {
|
||||
createConstants({ on, math, config, type: { Complex, BigNumber } })
|
||||
}
|
||||
})
|
||||
export const createInfinity = factory(
|
||||
'Infinity',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(Infinity)
|
||||
: Infinity
|
||||
)
|
||||
|
||||
setConstant(math, 'true', true)
|
||||
setConstant(math, 'false', false)
|
||||
setConstant(math, 'null', null)
|
||||
setConstant(math, 'uninitialized', 'Error: Constant uninitialized is removed since v4.0.0. Use null instead')
|
||||
export const createNaN = factory(
|
||||
'NaN',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(NaN)
|
||||
: NaN
|
||||
)
|
||||
|
||||
if (config.number === 'BigNumber') {
|
||||
setConstant(math, 'Infinity', new BigNumber(Infinity))
|
||||
setConstant(math, 'NaN', new BigNumber(NaN))
|
||||
export const createPi = factory(
|
||||
'pi',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? createBigNumberPi(BigNumber)
|
||||
: Math.PI
|
||||
)
|
||||
|
||||
setLazyConstant(math, 'pi', function () { return bigConstants.pi(BigNumber) })
|
||||
setLazyConstant(math, 'tau', function () { return bigConstants.tau(BigNumber) })
|
||||
setLazyConstant(math, 'e', function () { return bigConstants.e(BigNumber) })
|
||||
setLazyConstant(math, 'phi', function () { return bigConstants.phi(BigNumber) }) // golden ratio, (1+sqrt(5))/2
|
||||
export const createTau = factory(
|
||||
'tau',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? createBigNumberTau(BigNumber)
|
||||
: (2 * Math.PI)
|
||||
)
|
||||
|
||||
// uppercase constants (for compatibility with built-in Math)
|
||||
setLazyConstant(math, 'E', function () { return math.e })
|
||||
setLazyConstant(math, 'LN2', function () { return new BigNumber(2).ln() })
|
||||
setLazyConstant(math, 'LN10', function () { return new BigNumber(10).ln() })
|
||||
setLazyConstant(math, 'LOG2E', function () { return new BigNumber(1).div(new BigNumber(2).ln()) })
|
||||
setLazyConstant(math, 'LOG10E', function () { return new BigNumber(1).div(new BigNumber(10).ln()) })
|
||||
setLazyConstant(math, 'PI', function () { return math.pi })
|
||||
setLazyConstant(math, 'SQRT1_2', function () { return new BigNumber('0.5').sqrt() })
|
||||
setLazyConstant(math, 'SQRT2', function () { return new BigNumber(2).sqrt() })
|
||||
} else {
|
||||
setConstant(math, 'Infinity', Infinity)
|
||||
setConstant(math, 'NaN', NaN)
|
||||
export const createE = factory(
|
||||
'e',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? createBigNumberE(BigNumber)
|
||||
: Math.E
|
||||
)
|
||||
|
||||
setConstant(math, 'pi', Math.PI)
|
||||
setConstant(math, 'tau', Math.PI * 2)
|
||||
setConstant(math, 'e', Math.E)
|
||||
setConstant(math, 'phi', 1.61803398874989484820458683436563811772030917980576286213545) // golden ratio, (1+sqrt(5))/2
|
||||
// golden ratio, (1+sqrt(5))/2
|
||||
export const createPhi = factory(
|
||||
'phi',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? createBigNumberPhi(BigNumber)
|
||||
: 1.61803398874989484820458683436563811772030917980576286213545
|
||||
)
|
||||
|
||||
// uppercase constants (for compatibility with built-in Math)
|
||||
setConstant(math, 'E', math.e)
|
||||
setConstant(math, 'LN2', Math.LN2)
|
||||
setConstant(math, 'LN10', Math.LN10)
|
||||
setConstant(math, 'LOG2E', Math.LOG2E)
|
||||
setConstant(math, 'LOG10E', Math.LOG10E)
|
||||
setConstant(math, 'PI', math.pi)
|
||||
setConstant(math, 'SQRT1_2', Math.SQRT1_2)
|
||||
setConstant(math, 'SQRT2', Math.SQRT2)
|
||||
}
|
||||
export const createLN2 = factory(
|
||||
'LN2',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(2).ln()
|
||||
: Math.LN2
|
||||
)
|
||||
|
||||
// complex i
|
||||
setConstant(math, 'i', Complex.I)
|
||||
export const createLN10 = factory(
|
||||
'LN10',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(10).ln()
|
||||
: Math.LN10
|
||||
)
|
||||
|
||||
// meta information
|
||||
setConstant(math, 'version', version)
|
||||
})
|
||||
export const createLOG2E = factory(
|
||||
'LOG2E',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(1).div(new BigNumber(2).ln())
|
||||
: Math.LOG2E
|
||||
)
|
||||
|
||||
// disable lazy loading of constants
|
||||
createConstants.lazy = false
|
||||
export const createLOG10E = factory(
|
||||
'LOG10E',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(1).div(new BigNumber(10).ln())
|
||||
: Math.LOG10E
|
||||
)
|
||||
|
||||
// create a constant in both math and mathWithTransform
|
||||
function setConstant (math, name, value) {
|
||||
math[name] = value
|
||||
math.expression.mathWithTransform[name] = value
|
||||
}
|
||||
export const createSQRTHalf = factory(
|
||||
'SQRT1_2',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber('0.5').sqrt()
|
||||
: Math.SQRT1_2
|
||||
)
|
||||
|
||||
// create a lazy constant in both math and mathWithTransform
|
||||
function setLazyConstant (math, name, resolver) {
|
||||
lazy(math, name, resolver)
|
||||
lazy(math.expression.mathWithTransform, name, resolver)
|
||||
}
|
||||
export const createSQRT2 = factory(
|
||||
'SQRT2',
|
||||
['config.number', 'type.BigNumber'],
|
||||
({ config: { number }, type: { BigNumber } }) => (number === 'BigNumber')
|
||||
? new BigNumber(2).sqrt()
|
||||
: Math.SQRT2
|
||||
)
|
||||
|
||||
export const createI = factory(
|
||||
'i',
|
||||
['type.Complex'],
|
||||
({ type: { Complex } }) => Complex.I
|
||||
)
|
||||
|
||||
export const createVersion = factory('version', [], () => version)
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
// TODO: constants2 should fully replace constants
|
||||
|
||||
import { factory } from './utils/factory'
|
||||
|
||||
export const createI = factory('i', ['type.Complex'], ({ type: { Complex } }) => {
|
||||
return new Complex(0, 1)
|
||||
})
|
||||
47
src/index.js
47
src/index.js
@ -3,13 +3,56 @@
|
||||
import expression from './expression'
|
||||
import type from './type'
|
||||
import functions from './function'
|
||||
import { createConstants } from './constants'
|
||||
import json from './json'
|
||||
import error from './error'
|
||||
import {
|
||||
createInfinity,
|
||||
createNull,
|
||||
createFalse,
|
||||
createTrue,
|
||||
createNaN,
|
||||
createPi,
|
||||
createTau,
|
||||
createPhi,
|
||||
createE,
|
||||
createLN2,
|
||||
createLN10,
|
||||
createLOG2E,
|
||||
createLOG10E,
|
||||
createSQRTHalf,
|
||||
createSQRT2,
|
||||
createI,
|
||||
createVersion
|
||||
} from './constants'
|
||||
import { factory } from './utils/factory'
|
||||
|
||||
// for backward compatibility with v5
|
||||
const createUppercasePi = factory('PI', ['pi'], ({ pi }) => pi)
|
||||
const createUppercaseE = factory('E', ['e'], ({ e }) => e)
|
||||
|
||||
export default [
|
||||
type, // data types (Matrix, Complex, Unit, ...)
|
||||
createConstants, // constants
|
||||
|
||||
// constants
|
||||
createTrue,
|
||||
createFalse,
|
||||
createNull,
|
||||
createInfinity,
|
||||
createNaN,
|
||||
createPi,
|
||||
createUppercasePi,
|
||||
createTau,
|
||||
createE,
|
||||
createUppercaseE,
|
||||
createPhi,
|
||||
createLN2,
|
||||
createLN10,
|
||||
createLOG2E,
|
||||
createLOG10E,
|
||||
createSQRTHalf,
|
||||
createSQRT2,
|
||||
createI,
|
||||
createVersion,
|
||||
|
||||
functions, // functions
|
||||
|
||||
|
||||
@ -229,6 +229,26 @@ import { createGamma } from './function/probability/gamma'
|
||||
import { createDistribution } from './function/probability/distribution'
|
||||
import { createTyped } from './core/function/typed'
|
||||
import { DEFAULT_CONFIG } from './core/config'
|
||||
import { createEmbeddedDocs } from './expression/embeddedDocs'
|
||||
import {
|
||||
createE,
|
||||
createFalse,
|
||||
createI,
|
||||
createInfinity,
|
||||
createLN10,
|
||||
createLN2,
|
||||
createLOG10E,
|
||||
createLOG2E,
|
||||
createNaN,
|
||||
createNull,
|
||||
createPhi,
|
||||
createPi,
|
||||
createSQRT2,
|
||||
createSQRTHalf,
|
||||
createTau,
|
||||
createTrue,
|
||||
createVersion
|
||||
} from './constants'
|
||||
|
||||
export { create } from './core/core'
|
||||
|
||||
@ -784,6 +804,32 @@ export const json = {
|
||||
})
|
||||
}
|
||||
|
||||
// constants
|
||||
const constantsDependencies = { config, type: { BigNumber } }
|
||||
export const _true = createTrue()
|
||||
export const _false = createFalse()
|
||||
export const _null = createNull()
|
||||
export const _Infinity = createInfinity(constantsDependencies)
|
||||
export const _NaN = createNaN(constantsDependencies)
|
||||
export const pi = createPi(constantsDependencies)
|
||||
export const PI = pi
|
||||
export const tau = createTau(constantsDependencies)
|
||||
export const e = createE(constantsDependencies)
|
||||
export const E = e
|
||||
export const phi = createPhi(constantsDependencies)
|
||||
export const LN2 = createLN2(constantsDependencies)
|
||||
export const LN10 = createLN10(constantsDependencies)
|
||||
export const LOG2E = createLOG2E(constantsDependencies)
|
||||
export const LOG10E = createLOG10E(constantsDependencies)
|
||||
export const SQRT1_2 = createSQRTHalf(constantsDependencies)
|
||||
export const SQRT2 = createSQRT2(constantsDependencies)
|
||||
export const i = createI({ config, type: { Complex } })
|
||||
export const version = createVersion()
|
||||
|
||||
export const expression = {
|
||||
docs: createEmbeddedDocs()
|
||||
}
|
||||
|
||||
// error classes
|
||||
export { IndexError } from './error/IndexError'
|
||||
export { DimensionError } from './error/DimensionError'
|
||||
|
||||
@ -4,7 +4,7 @@ import { isComplex, isUnit, typeOf } from '../../utils/is'
|
||||
import { factory } from '../../utils/factory'
|
||||
import { endsWith } from '../../utils/string'
|
||||
import { clone } from '../../utils/object'
|
||||
import { pi as createPi } from '../../utils/bignumber/constants'
|
||||
import { createBigNumberPi as createPi } from '../../utils/bignumber/constants'
|
||||
|
||||
const name = 'type.Unit'
|
||||
const dependencies = [
|
||||
|
||||
@ -7,7 +7,7 @@ import { memoize } from '../function'
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns e
|
||||
*/
|
||||
export const e = memoize(function (BigNumber) {
|
||||
export const createBigNumberE = memoize(function (BigNumber) {
|
||||
return new BigNumber(1).exp()
|
||||
}, hasher)
|
||||
|
||||
@ -16,7 +16,7 @@ export const e = memoize(function (BigNumber) {
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns phi
|
||||
*/
|
||||
export const phi = memoize(function (BigNumber) {
|
||||
export const createBigNumberPhi = memoize(function (BigNumber) {
|
||||
return new BigNumber(1).plus(new BigNumber(5).sqrt()).div(2)
|
||||
}, hasher)
|
||||
|
||||
@ -25,7 +25,7 @@ export const phi = memoize(function (BigNumber) {
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns pi
|
||||
*/
|
||||
export const pi = memoize(function (BigNumber) {
|
||||
export const createBigNumberPi = memoize(function (BigNumber) {
|
||||
return BigNumber.acos(-1)
|
||||
}, hasher)
|
||||
|
||||
@ -34,8 +34,8 @@ export const pi = memoize(function (BigNumber) {
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns tau
|
||||
*/
|
||||
export const tau = memoize(function (BigNumber) {
|
||||
return pi(BigNumber).times(2)
|
||||
export const createBigNumberTau = memoize(function (BigNumber) {
|
||||
return createBigNumberPi(BigNumber).times(2)
|
||||
}, hasher)
|
||||
|
||||
/**
|
||||
|
||||
@ -1,171 +1,174 @@
|
||||
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'
|
||||
import {
|
||||
createE,
|
||||
createFalse,
|
||||
createI,
|
||||
createInfinity,
|
||||
createLN10,
|
||||
createLN2,
|
||||
createLOG10E,
|
||||
createLOG2E,
|
||||
createNaN,
|
||||
createNull,
|
||||
createPhi,
|
||||
createPi,
|
||||
createSQRT2,
|
||||
createSQRTHalf,
|
||||
createTau,
|
||||
createTrue
|
||||
} from '../src/constants'
|
||||
|
||||
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)
|
||||
})
|
||||
const config = { number: 'number', precision: 64, epsilon: 1e-12 }
|
||||
const BigNumber = createBigNumberClass({ config })
|
||||
const Complex = createComplexClass({ config })
|
||||
const dependencies = {
|
||||
config: config,
|
||||
type: { BigNumber, Complex }
|
||||
}
|
||||
|
||||
it('should have tau', function () {
|
||||
approx.equal(instance.tau, 6.28318530717959)
|
||||
})
|
||||
it('should create pi', function () {
|
||||
approx.equal(createPi(dependencies), 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664)
|
||||
})
|
||||
|
||||
it('should have phi, golden ratio', function () {
|
||||
approx.equal(instance.phi, 1.61803398874989484820458683436563811772030917980576286213545)
|
||||
})
|
||||
it('should create tau', function () {
|
||||
approx.equal(createTau(dependencies), 6.28318530717959)
|
||||
})
|
||||
|
||||
it('should have e (euler constant)', function () {
|
||||
approx.equal(instance.e, 2.71828182845905)
|
||||
})
|
||||
it('should create phi, golden ratio', function () {
|
||||
approx.equal(createPhi(dependencies), 1.61803398874989484820458683436563811772030917980576286213545)
|
||||
})
|
||||
|
||||
it('should have LN2', function () {
|
||||
approx.equal(instance.LN2, 0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573)
|
||||
})
|
||||
it('should create e (euler constant)', function () {
|
||||
approx.equal(createE(dependencies), 2.71828182845905)
|
||||
})
|
||||
|
||||
it('should have LN10', function () {
|
||||
approx.equal(instance.LN10, 2.30258509299404568401799145468436420760110148862877297603332790096757260967735248023599720508959829834196778404228624863)
|
||||
})
|
||||
it('should create LN2', function () {
|
||||
approx.equal(createLN2(dependencies), 0.69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641868754200148102057068573)
|
||||
})
|
||||
|
||||
it('should have LOG2E', function () {
|
||||
approx.equal(instance.LOG2E, 1.44269504088896340735992468100189213742664595415298593413544940693110921918118507988552662289350634449699751830965254425)
|
||||
})
|
||||
it('should create LN10', function () {
|
||||
approx.equal(createLN10(dependencies), 2.30258509299404568401799145468436420760110148862877297603332790096757260967735248023599720508959829834196778404228624863)
|
||||
})
|
||||
|
||||
it('should have LOG10E', function () {
|
||||
approx.equal(instance.LOG10E, 0.43429448190325182765112891891660508229439700580366656611445378316586464920887077472922494933843174831870610674476630373)
|
||||
})
|
||||
it('should create LOG2E', function () {
|
||||
approx.equal(createLOG2E(dependencies), 1.44269504088896340735992468100189213742664595415298593413544940693110921918118507988552662289350634449699751830965254425)
|
||||
})
|
||||
|
||||
it('should have PI', function () {
|
||||
approx.equal(instance.PI, 3.14159265358979)
|
||||
})
|
||||
it('should create LOG10E', function () {
|
||||
approx.equal(createLOG10E(dependencies), 0.43429448190325182765112891891660508229439700580366656611445378316586464920887077472922494933843174831870610674476630373)
|
||||
})
|
||||
|
||||
it('should have SQRT1_2', function () {
|
||||
approx.equal(instance.SQRT1_2, 0.70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078636750692311545614851)
|
||||
})
|
||||
it('should create PI', function () {
|
||||
approx.equal(createPi(dependencies), 3.14159265358979)
|
||||
})
|
||||
|
||||
it('should have SQRT2', function () {
|
||||
approx.equal(instance.SQRT2, 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702)
|
||||
})
|
||||
it('should create SQRT1_2', function () {
|
||||
approx.equal(createSQRTHalf(dependencies), 0.70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078636750692311545614851)
|
||||
})
|
||||
|
||||
it('should have Infinity', function () {
|
||||
assert.strictEqual(instance.Infinity, Infinity)
|
||||
})
|
||||
it('should create SQRT2', function () {
|
||||
approx.equal(createSQRT2(dependencies), 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702)
|
||||
})
|
||||
|
||||
it('should have NaN', function () {
|
||||
assert.ok(isNaN(instance.NaN))
|
||||
})
|
||||
it('should create Infinity', function () {
|
||||
assert.strictEqual(createInfinity(dependencies), Infinity)
|
||||
})
|
||||
|
||||
it('should create NaN', function () {
|
||||
assert.ok(isNaN(createNaN(dependencies)))
|
||||
})
|
||||
})
|
||||
|
||||
describe('bignumbers', function () {
|
||||
it('should have bignumber pi', function () {
|
||||
assert.strictEqual(bigmath.pi.toString(), '3.141592653589793238462643383279502884197169399375105820974944592')
|
||||
const config = { number: 'BigNumber', precision: 64, epsilon: 1e-12 }
|
||||
const BigNumber = createBigNumberClass({ config })
|
||||
const Complex = createComplexClass({ config })
|
||||
const dependencies = {
|
||||
config: config,
|
||||
type: { BigNumber, Complex }
|
||||
}
|
||||
|
||||
it('should create bignumber pi', function () {
|
||||
assert.strictEqual(createPi(dependencies).toString(), '3.141592653589793238462643383279502884197169399375105820974944592')
|
||||
})
|
||||
|
||||
it('should have bignumber tau', function () {
|
||||
assert.strictEqual(bigmath.tau.toString(), '6.283185307179586476925286766559005768394338798750211641949889184')
|
||||
it('should create bignumber tau', function () {
|
||||
assert.strictEqual(createTau(dependencies).toString(), '6.283185307179586476925286766559005768394338798750211641949889184')
|
||||
})
|
||||
|
||||
it('should have bignumber phi, golden ratio', function () {
|
||||
assert.strictEqual(bigmath.phi.toString(), '1.618033988749894848204586834365638117720309179805762862135448623')
|
||||
it('should create bignumber phi, golden ratio', function () {
|
||||
assert.strictEqual(createPhi(dependencies).toString(), '1.618033988749894848204586834365638117720309179805762862135448623')
|
||||
})
|
||||
|
||||
it('should have bignumber e', function () {
|
||||
assert.strictEqual(bigmath.e.toString(), '2.718281828459045235360287471352662497757247093699959574966967628')
|
||||
it('should create bignumber e', function () {
|
||||
assert.strictEqual(createE(dependencies).toString(), '2.718281828459045235360287471352662497757247093699959574966967628')
|
||||
})
|
||||
|
||||
it('should have bignumber LN2', function () {
|
||||
assert.strictEqual(bigmath.LN2.toString(), '0.6931471805599453094172321214581765680755001343602552541206800095')
|
||||
it('should create bignumber LN2', function () {
|
||||
assert.strictEqual(createLN2(dependencies).toString(), '0.6931471805599453094172321214581765680755001343602552541206800095')
|
||||
})
|
||||
|
||||
it('should have bignumber LN10', function () {
|
||||
assert.strictEqual(bigmath.LN10.toString(), '2.302585092994045684017991454684364207601101488628772976033327901')
|
||||
it('should create bignumber LN10', function () {
|
||||
assert.strictEqual(createLN10(dependencies).toString(), '2.302585092994045684017991454684364207601101488628772976033327901')
|
||||
})
|
||||
|
||||
it('should have bignumber LOG2E', function () {
|
||||
assert.strictEqual(bigmath.LOG2E.toString(), '1.442695040888963407359924681001892137426645954152985934135449407')
|
||||
it('should create bignumber LOG2E', function () {
|
||||
assert.strictEqual(createLOG2E(dependencies).toString(), '1.442695040888963407359924681001892137426645954152985934135449407')
|
||||
})
|
||||
|
||||
it('should have bignumber LOG10E', function () {
|
||||
assert.strictEqual(bigmath.LOG10E.toString(), '0.4342944819032518276511289189166050822943970058036665661144537832')
|
||||
it('should create bignumber LOG10E', function () {
|
||||
assert.strictEqual(createLOG10E(dependencies).toString(), '0.4342944819032518276511289189166050822943970058036665661144537832')
|
||||
})
|
||||
|
||||
it('should have bignumber PI (upper case)', function () {
|
||||
assert.strictEqual(bigmath.PI.toString(), '3.141592653589793238462643383279502884197169399375105820974944592')
|
||||
it('should create bignumber PI (upper case)', function () {
|
||||
assert.strictEqual(createPi(dependencies).toString(), '3.141592653589793238462643383279502884197169399375105820974944592')
|
||||
})
|
||||
|
||||
it('should have bignumber SQRT1_2', function () {
|
||||
assert.strictEqual(bigmath.SQRT1_2.toString(), '0.707106781186547524400844362104849039284835937688474036588339869')
|
||||
it('should create bignumber SQRT1_2', function () {
|
||||
assert.strictEqual(createSQRTHalf(dependencies).toString(), '0.707106781186547524400844362104849039284835937688474036588339869')
|
||||
})
|
||||
|
||||
it('should have bignumber SQRT2', function () {
|
||||
assert.strictEqual(bigmath.SQRT2.toString(), '1.414213562373095048801688724209698078569671875376948073176679738')
|
||||
it('should create bignumber SQRT2', function () {
|
||||
assert.strictEqual(createSQRT2(dependencies).toString(), '1.414213562373095048801688724209698078569671875376948073176679738')
|
||||
})
|
||||
|
||||
it('should have bignumber Infinity', function () {
|
||||
assert(bigmath.Infinity instanceof bigmath.type.BigNumber)
|
||||
assert.strictEqual(bigmath.Infinity.toString(), 'Infinity')
|
||||
it('should create bignumber Infinity', function () {
|
||||
const inf = createInfinity(dependencies)
|
||||
assert(inf instanceof BigNumber)
|
||||
assert.strictEqual(inf.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))
|
||||
it('should create bignumber NaN', function () {
|
||||
const nan = createNaN(dependencies)
|
||||
assert(nan instanceof BigNumber)
|
||||
assert.strictEqual(nan.toString(), 'NaN')
|
||||
assert.ok(isNaN(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))
|
||||
})
|
||||
it('should create i', function () {
|
||||
const config = { epsilon: 1e-12 }
|
||||
const Complex = createComplexClass({ config })
|
||||
const i = createI({ type: Complex })
|
||||
|
||||
assert.deepStrictEqual(i, new 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 create true and false', function () {
|
||||
assert.strictEqual(createTrue(), true)
|
||||
assert.strictEqual(createFalse(), 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 create null', function () {
|
||||
assert.strictEqual(createNull(), null)
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
||||
@ -887,6 +887,20 @@ describe('parse', function () {
|
||||
assert.deepStrictEqual(parse('Infinity'), createConstantNode(Infinity))
|
||||
})
|
||||
|
||||
it('should evaluate 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)
|
||||
})
|
||||
|
||||
// helper function to create a ConstantNode with empty comment
|
||||
function createConstantNode (value) {
|
||||
const c = new ConstantNode(value)
|
||||
|
||||
@ -2,39 +2,39 @@
|
||||
import assert from 'assert'
|
||||
|
||||
import BigNumber from 'decimal.js'
|
||||
import { e, phi, pi, tau } from '../../../src/utils/bignumber/constants'
|
||||
import { createBigNumberE, createBigNumberPhi, createBigNumberPi, createBigNumberTau } from '../../../src/utils/bignumber/constants'
|
||||
|
||||
const Big32 = BigNumber.clone({ precision: 32 })
|
||||
const Big64 = BigNumber.clone({ precision: 64 })
|
||||
|
||||
describe('bignumber', function () {
|
||||
it('should calculate a bignumber e', function () {
|
||||
assert.strictEqual(e(Big32).toString(),
|
||||
assert.strictEqual(createBigNumberE(Big32).toString(),
|
||||
'2.7182818284590452353602874713527')
|
||||
assert.strictEqual(e(Big64).toString(),
|
||||
assert.strictEqual(createBigNumberE(Big64).toString(),
|
||||
'2.718281828459045235360287471352662497757247093699959574966967628')
|
||||
})
|
||||
|
||||
it('should calculate a bignumber pi', function () {
|
||||
assert.strictEqual(pi(Big32).toString(),
|
||||
assert.strictEqual(createBigNumberPi(Big32).toString(),
|
||||
'3.1415926535897932384626433832795')
|
||||
assert.strictEqual(pi(Big64).toString(),
|
||||
assert.strictEqual(createBigNumberPi(Big64).toString(),
|
||||
'3.141592653589793238462643383279502884197169399375105820974944592')
|
||||
})
|
||||
|
||||
it('should calculate a bignumber tau', function () {
|
||||
assert.strictEqual(tau(Big32).toString(),
|
||||
assert.strictEqual(createBigNumberTau(Big32).toString(),
|
||||
'6.283185307179586476925286766559')
|
||||
assert.strictEqual(tau(Big64).toString(),
|
||||
assert.strictEqual(createBigNumberTau(Big64).toString(),
|
||||
'6.283185307179586476925286766559005768394338798750211641949889184')
|
||||
})
|
||||
|
||||
it('should calculate a bignumber phi', function () {
|
||||
// FIXME: round-off error
|
||||
// assert.strictEqual(bignumber.phi(32), '1.6180339887498948482045868343656')
|
||||
assert.strictEqual(phi(Big32).toString(),
|
||||
assert.strictEqual(createBigNumberPhi(Big32).toString(),
|
||||
'1.6180339887498948482045868343657')
|
||||
assert.strictEqual(phi(Big64).toString(),
|
||||
assert.strictEqual(createBigNumberPhi(Big64).toString(),
|
||||
'1.618033988749894848204586834365638117720309179805762862135448623')
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user