Skip to content

Commit

Permalink
Changed references to epsilon in docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Apr 7, 2024
1 parent 05aab2b commit e7eb46e
Show file tree
Hide file tree
Showing 29 changed files with 188 additions and 158 deletions.
9 changes: 7 additions & 2 deletions docs/core/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { create, all } from 'mathjs'

// create a mathjs instance with configuration
const config = {
epsilon: 1e-12,
relTol: 1e-12,
absTol: 1e.15,
matrix: 'Matrix',
number: 'number',
precision: 64,
Expand All @@ -28,10 +29,14 @@ math.config({

The following configuration options are available:

- `epsilon`. The minimum relative difference used to test equality between two
- `relTol`. The minimum relative difference used to test equality between two
compared values. This value is used by all relational functions.
Default value is `1e-12`.

- `absTol`. The minimum absolute difference used to test equality between two
compared values. This value is used by all relational functions.
Default value is `1e-15`.

- `matrix`. The default type of matrix output for functions.
Available values are: `'Matrix'` (default) or `'Array'`.
Where possible, the type of matrix output from functions is determined from
Expand Down
11 changes: 6 additions & 5 deletions docs/datatypes/bignumbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ math.config({
number: 'BigNumber', // Default type of number:
// 'number' (default), 'BigNumber', or 'Fraction'
precision: 64, // Number of significant digits for BigNumbers
epsilon: 1e-60
relTol: 1e-60,
absTol: 1e-63
})

// use math
Expand All @@ -34,12 +35,12 @@ math.evaluate('0.1 + 0.2') // BigNumber, 0.3
The default precision for BigNumber is 64 digits, and can be configured with
the option `precision`.

Note that we also change the configuration of `epsilon`
to be close to the precision limit of our BigNumbers. `epsilon` is used for
Note that we also change the configuration of `relTol` and `absTol`
to be close to the precision limit of our BigNumbers. `relTol` and `absTol` are used for
example in relational and rounding functions (`equal`, `larger`, `smaller`,
`round`, `floor`, etc) to determine when a value is nearly equal,
see [Equality](numbers.md#equality). If we would leave `epsilon` unchanged,
having the default value of `1e-12`, we could get inaccurate and misleading
see [Equality](numbers.md#equality). If we would leave `relTol` and `absTol` unchanged,
having the default value of `1e-12` and `1e-15` respectively, we could get inaccurate and misleading
results since we're now working with a higher precision.


Expand Down
7 changes: 2 additions & 5 deletions docs/datatypes/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ return exactly `0.3`.

To solve this problem, the relational functions of math.js check whether the
relative and absolute differences between the compared values is smaller than the configured
option `epsilon`. In pseudo code (without exceptions for 0, Infinity and NaN):
option `relTol` and `absTol`. In pseudo code (without exceptions for 0, Infinity and NaN):

relTol = epsilon
absTol = epsilon / 1000
abs(a-b) <= max(relTol * max(abs(a), abs(b)), absTol)

where:

- `EPSILON` is the relative difference between x and y. Epsilon is configurable
and is `1e-12` by default. See [Configuration](../core/configuration.md).
- `relTol` is the relative tolerance between x and y and `absTol` the absolute tolerance. Relative tolerance and absolute tolerance are configurable and are `1e-12` and `1e-15` respectively by default. See [Configuration](../core/configuration.md).
- `DBL_EPSILON` is the minimum positive floating point number such that
`1.0 + DBL_EPSILON !== 1.0`. This is a constant with a value of approximately
`2.2204460492503130808472633361816e-16`.
Expand Down
5 changes: 4 additions & 1 deletion src/core/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ import { DEFAULT_CONFIG } from './config.js'
* The object can contain nested objects,
* all nested objects will be flattened.
* @param {Object} [config] Available options:
* {number} epsilon
* {number} relTol
* Minimum relative difference between two
* compared values, used by all comparison functions.
* {number} absTol
* Minimum absolute difference between two
* compared values, used by all comparison functions.
* {string} matrix
* A string 'Matrix' (default) or 'Array'.
* {string} number
Expand Down
5 changes: 4 additions & 1 deletion src/core/function/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ export function configFactory (config, emit) {
* math.evaluate('0.4') // outputs Fraction 2/5
*
* @param {Object} [options] Available options:
* {number} epsilon
* {number} relTol
* Minimum relative difference between two
* compared values, used by all comparison functions.
* {number} absTol
* Minimum absolute difference between two
* compared values, used by all comparison functions.
* {string} matrix
* A string 'Matrix' (default) or 'Array'.
* {string} number
Expand Down
8 changes: 4 additions & 4 deletions src/function/arithmetic/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,
*/
return typed(name, {
number: function (x) {
// Handle round off errors by first rounding to epsilon precision
// Handle round off errors by first rounding to relTol precision
const xEpsilon = roundNumber(x, toExponent(config.relTol))
const xSelected = nearlyEqual(x, xEpsilon, config.relTol, config.absTol) ? xEpsilon : x
return roundNumber(xSelected)
},

'number, number': function (x, n) {
// Same as number: unless user specifies more decimals than epsilon
// Same as number: unless user specifies more decimals than relTol
const epsilonExponent = toExponent(config.relTol)
if (n >= epsilonExponent) { return roundNumber(x, n) }

Expand Down Expand Up @@ -115,7 +115,7 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,
},

BigNumber: function (x) {
// Handle round off errors by first rounding to epsilon precision
// Handle round off errors by first rounding to relTol precision
const xEpsilon = new BigNumber(x).toDecimalPlaces(toExponent(config.relTol))
const xSelected = bigNearlyEqual(x, xEpsilon, config.relTol, config.absTol) ? xEpsilon : x
return xSelected.toDecimalPlaces(0)
Expand All @@ -124,7 +124,7 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,
'BigNumber, BigNumber': function (x, n) {
if (!n.isInteger()) { throw new TypeError(NO_INT) }

// Same as BigNumber: unless user specifies more decimals than epsilon
// Same as BigNumber: unless user specifies more decimals than relTol
const epsilonExponent = toExponent(config.relTol)
if (n >= epsilonExponent) { return x.toDecimalPlaces(n.toNumber()) }

Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const createCompare = /* #__PURE__ */ factory(name, dependencies, ({ type
* Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
*
* x and y are considered equal when the relative difference between x and y
* is smaller than the configured epsilon. The function cannot be used to
* is smaller than the configured absTol and relTol. The function cannot be used to
* compare values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/compareNatural.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const createCompareNatural = /* #__PURE__ */ factory(name, dependencies,
* the function compares in a natural way.
*
* For numeric values, x and y are considered equal when the relative
* difference between x and y is smaller than the configured epsilon.
* difference between x and y is smaller than the configured relTol and absTol.
* The function cannot be used to compare values smaller than
* approximately 2.22e-16.
*
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const createEqual = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* Test whether two values are equal.
*
* The function tests whether the relative difference between x and y is
* smaller than the configured epsilon. The function cannot be used to
* smaller than the configured relTol and absTol. The function cannot be used to
* compare values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/larger.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createLarger = /* #__PURE__ */ factory(name, dependencies, ({ typed
* Test whether value x is larger than y.
*
* The function returns true when x is larger than y and the relative
* difference between x and y is larger than the configured epsilon. The
* difference between x and y is larger than the configured relTol and absTol. The
* function cannot be used to compare values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/largerEq.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createLargerEq = /* #__PURE__ */ factory(name, dependencies, ({ typ
* Test whether value x is larger or equal to y.
*
* The function returns true when x is larger than y or the relative
* difference between x and y is smaller than the configured epsilon. The
* difference between x and y is smaller than the configured relTol and absTol. The
* function cannot be used to compare values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/smaller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createSmaller = /* #__PURE__ */ factory(name, dependencies, ({ type
* Test whether value x is smaller than y.
*
* The function returns true when x is smaller than y and the relative
* difference between x and y is smaller than the configured epsilon. The
* difference between x and y is smaller than the configured relTol and absTol. The
* function cannot be used to compare values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/smallerEq.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createSmallerEq = /* #__PURE__ */ factory(name, dependencies, ({ ty
* Test whether value x is smaller or equal to y.
*
* The function returns true when x is smaller than y or the relative
* difference between x and y is smaller than the configured epsilon. The
* difference between x and y is smaller than the configured relTol and absTol. The
* function cannot be used to compare values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/relational/unequal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createUnequal = /* #__PURE__ */ factory(name, dependencies, ({ type
* Test whether two values are unequal.
*
* The function tests whether the relative difference between x and y is
* larger than the configured epsilon. The function cannot be used to compare
* larger than the configured relTol and absTol. The function cannot be used to compare
* values smaller than approximately 2.22e-16.
*
* For matrices, the function is evaluated element wise.
Expand Down
2 changes: 1 addition & 1 deletion src/function/special/zeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const createZeta = /* #__PURE__ */ factory(name, dependencies, ({ typed,
s,
value => new BigNumber(value),
() => {
// epsilon is for example 1e-12. Extract the positive exponent 12 from that
// relTol is for example 1e-12. Extract the positive exponent 12 from that
return Math.abs(Math.log10(config.relTol))
}
),
Expand Down
12 changes: 6 additions & 6 deletions src/utils/complex.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { nearlyEqual } from './number.js'

/**
* Test whether two complex values are equal provided a given epsilon.
* Test whether two complex values are equal provided a given relTol and absTol.
* Does not use or change the global Complex.EPSILON setting
* @param {Complex} x
* @param {Complex} y
* @param {number} relTol
* @param {number} absTol
* @returns {boolean}
* @param {Complex} x - The first complex number for comparison.
* @param {Complex} y - The second complex number for comparison.
* @param {number} relTol - The relative tolerance for comparison.
* @param {number} absTol - The absolute tolerance for comparison.
* @returns {boolean} - Returns true if the two complex numbers are equal within the given tolerances, otherwise returns false.
*/
export function complexEquals (x, y, relTol, absTol) {
return nearlyEqual(x.re, y.re, relTol, absTol) && nearlyEqual(x.im, y.im, relTol, absTol)
Expand Down
12 changes: 8 additions & 4 deletions test/node-tests/defaultInstance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('defaultInstance', function () {
number: 'number',
precision: 64,
predictable: false,
epsilon: 1e-12,
relTol: 1e-12,
absTol: 1e-15,
randomSeed: null
})
})
Expand All @@ -29,7 +30,8 @@ describe('defaultInstance', function () {
number: 'BigNumber',
precision: 64,
predictable: false,
epsilon: 1e-12,
relTol: 1e-12,
absTol: 1e-15,
randomSeed: null
})
})
Expand Down Expand Up @@ -72,7 +74,8 @@ describe('defaultInstance', function () {
number: 'BigNumber',
precision: 4,
predictable: true,
epsilon: 1e-12,
relTol: 1e-12,
absTol: 1e-15,
randomSeed: null
})

Expand All @@ -92,7 +95,8 @@ describe('defaultInstance', function () {
number: 'number',
precision: 64,
predictable: false,
epsilon: 1e-12,
relTol: 1e-12,
absTol: 1e-15,
randomSeed: null
})

Expand Down
4 changes: 2 additions & 2 deletions test/unit-tests/constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

describe('constants', function () {
describe('number', function () {
const config = { number: 'number', precision: 64, epsilon: 1e-12 }
const config = { number: 'number', precision: 64, relTol: 1e-12 }
const BigNumber = createBigNumberClass({ config })
const Complex = createComplexClass({ config })
const dependencies = {
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('constants', function () {
})

describe('bignumbers', function () {
const config = { number: 'BigNumber', precision: 64, epsilon: 1e-12 }
const config = { number: 'BigNumber', precision: 64, relTol: 1e-12 }
const BigNumber = createBigNumberClass({ config })
const Complex = createComplexClass({ config })
const dependencies = {
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/arithmetic/round.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('round', function () {
})

it('uses updated config.relTol value', function () {
math2.config({ epsilon: 1e-13 })
math2.config({ relTol: 1e-13 })
assert.strictEqual(math2.round((0.000000000001459), 12), 1e-12)
assert.deepStrictEqual(math2.round(bignumber(1.49e-12), bignumber(12)), bignumber(1e-12))
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/relational/compare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('compare', function () {
})
})

it('should apply configuration option epsilon', function () {
it('should apply configuration option relTol', function () {
const mymath = math.create()
assert.strictEqual(mymath.compare(1, 0.991), 1)
assert.strictEqual(mymath.compare(mymath.bignumber(1), mymath.bignumber(0.991)).valueOf(), '1')
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/relational/compareNatural.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('compareNatural', function () {
assert.strictEqual(compareNatural({ a: 2, b: { c: 3 } }, { a: 2, b: { c: 4 } }), -1)
})

it('should apply configuration option epsilon', function () {
it('should apply configuration option relTol', function () {
const mymath = math.create()

assert.strictEqual(mymath.compareNatural(1, 0.991), 1)
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/relational/equal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('equal', function () {
assert.throws(function () { equal('A', 'B') }, /Cannot convert "A" to a number/)
})

it('should apply configuration option epsilon', function () {
it('should apply configuration option relTol', function () {
const mymath = math.create()
assert.strictEqual(mymath.equal(1, 0.991), false)
assert.strictEqual(mymath.equal(mymath.bignumber(1), mymath.bignumber(0.991)), false)
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/relational/larger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('larger', function () {
assert.strictEqual(larger(unit('101cm'), unit('1m')), true)
})

it('should apply configuration option epsilon', function () {
it('should apply configuration option relTol', function () {
const mymath = math.create()
assert.strictEqual(mymath.larger(1, 0.991), true)
assert.strictEqual(mymath.larger(mymath.bignumber(1), mymath.bignumber(0.991)), true)
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/relational/largerEq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('largerEq', function () {
assert.strictEqual(largerEq(unit('101cm'), unit('1m')), true)
})

it('should apply configuration option epsilon', function () {
it('should apply configuration option relTol', function () {
const mymath = math.create()
assert.strictEqual(mymath.largerEq(1, 1.01), false)
assert.strictEqual(mymath.largerEq(mymath.bignumber(1), mymath.bignumber(1.01)), false)
Expand Down
2 changes: 1 addition & 1 deletion test/unit-tests/function/relational/smaller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('smaller', function () {
assert.strictEqual(smaller(unit('101cm'), unit('1m')), false)
})

it('should apply configuration option epsilon', function () {
it('should apply configuration option relTol', function () {
const mymath = math.create()
assert.strictEqual(mymath.smaller(0.991, 1), true)
assert.strictEqual(mymath.smaller(mymath.bignumber(0.991), mymath.bignumber(1)), true)
Expand Down
4 changes: 2 additions & 2 deletions test/unit-tests/type/unit/physicalConstants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const { BigNumber, Unit } = math
describe('physical constants', function () {
it('should return the correct value and unit for physical constants', function () {
// Note: to keep these unit tests readable and compact, the toString() of the units is compared
const config = { number: 'number', precision: 64, epsilon: 1e-12 }
const config = { number: 'number', precision: 64, relTol: 1e-12 }
const dependencies = { config, BigNumber, Unit }

// Universal constants
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('physical constants', function () {
})

it('should create BigNumber unit values if configured', function () {
const config = { number: 'BigNumber', precision: 64, epsilon: 1e-12 }
const config = { number: 'BigNumber', precision: 64, relTol: 1e-12 }
const dependencies = { config, BigNumber, Unit }
const molarMass = createMolarMass(dependencies)

Expand Down

0 comments on commit e7eb46e

Please sign in to comment.