diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 12a8d90a91..f8cf5f1e53 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -1,13 +1,19 @@ +import { Decimal } from 'decimal.js' import { factory } from '../../utils/factory' import { deepMap } from '../../utils/collection' import { nearlyEqual } from '../../utils/number' import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual' import { ceilNumber } from '../../plain/number' +import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11' +import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14' const name = 'ceil' -const dependencies = ['typed', 'config', 'round'] +const dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar'] + +export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar }) => { + const algorithm11 = createAlgorithm11({ typed, equalScalar }) + const algorithm14 = createAlgorithm14({ typed }) -export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round }) => { /** * Round a value towards plus infinity * If `x` is complex, both real and imaginary part are rounded towards plus infinity. @@ -16,6 +22,7 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, * Syntax: * * math.ceil(x) + * math.ceil(x, n) * * Examples: * @@ -24,16 +31,24 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.ceil(-4.2) // returns number -4 * math.ceil(-4.7) // returns number -4 * - * const c = math.complex(3.2, -2.7) + * math.ceil(3.212, 2) // returns number 3.22 + * math.ceil(3.288, 2) // returns number 3.29 + * math.ceil(-4.212, 2) // returns number -4.21 + * math.ceil(-4.782, 2) // returns number -4.78 + * + * const c = math.complex(3.24, -2.71) * math.ceil(c) // returns Complex 4 - 2i + * math.ceil(c, 1) // returns Complex 3.3 - 2.7i * * math.ceil([3.2, 3.8, -4.7]) // returns Array [4, 4, -4] + * math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.3, 3.9, -4.7] * * See also: * * floor, fix, round * * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded + * @param {number | BigNumber | Array} [n=0] Number of decimals * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ return typed('ceil', { @@ -45,10 +60,25 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, + 'number, number': function (x, n) { + if (nearlyEqual(x, round(x, n), config.epsilon)) { + return round(x, n) + } else { + let [number, exponent] = `${x}e`.split('e') + const result = Math.ceil(Number(`${number}e${Number(exponent) + n}`)); + [number, exponent] = `${result}e`.split('e') + return Number(`${number}e${Number(exponent) - n}`) + } + }, + Complex: function (x) { return x.ceil() }, + 'Complex, number': function (x, n) { + return x.ceil(n) + }, + BigNumber: function (x) { if (bigNearlyEqual(x, round(x), config.epsilon)) { return round(x) @@ -57,13 +87,43 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, + 'BigNumber, BigNumber': function (x, n) { + if (bigNearlyEqual(x, round(x, n), config.epsilon)) { + return round(x, n) + } else { + return x.toDecimalPlaces(n.toNumber(), Decimal.ROUND_CEIL) + } + }, + Fraction: function (x) { return x.ceil() }, + 'Fraction, number': function (x, n) { + return x.ceil(n) + }, + 'Array | Matrix': function (x) { // deep map collection, skip zeros since ceil(0) = 0 return deepMap(x, this, true) + }, + + 'Array | Matrix, number': function (x, n) { + // deep map collection, skip zeros since ceil(0) = 0 + return deepMap(x, i => this(i, n), true) + }, + + 'SparseMatrix, number | BigNumber': function (x, y) { + return algorithm11(x, y, this, false) + }, + + 'DenseMatrix, number | BigNumber': function (x, y) { + return algorithm14(x, y, this, false) + }, + + 'number | Complex | BigNumber, Array': function (x, y) { + // use matrix implementation + return algorithm14(matrix(y), x, this, true).valueOf() } }) }) diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 7f3fe30352..1c1148eab2 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -1,10 +1,12 @@ import { factory } from '../../utils/factory' import { deepMap } from '../../utils/collection' +import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14' const name = 'fix' -const dependencies = ['typed', 'Complex', 'ceil', 'floor'] +const dependencies = ['typed', 'Complex', 'matrix', 'ceil', 'floor'] -export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, Complex, ceil, floor }) => { +export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, Complex, matrix, ceil, floor }) => { + const algorithm14 = createAlgorithm14({ typed }) /** * Round a value towards zero. * For matrices, the function is evaluated element wise. @@ -20,23 +22,35 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C * math.fix(-4.2) // returns number -4 * math.fix(-4.7) // returns number -4 * - * const c = math.complex(3.2, -2.7) + * math.fix(3.12, 1) // returns number 3.1 + * math.fix(3.18, 1) // returns number 3.1 + * math.fix(-4.12, 1) // returns number -4.1 + * math.fix(-4.17, 1) // returns number -4.1 + * + * const c = math.complex(3.22, -2.78) * math.fix(c) // returns Complex 3 - 2i + * math.fix(c, 1) // returns Complex 3.2 - 2.7i * - * math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4] + * math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4] + * math.fix([3.2, 3.8, -4.7], 1) // returns Array [3.2, 3.8, -4.7] * * See also: * * ceil, floor, round * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded - * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value + * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded + * @param {number | BigNumber | Array} [n=0] Number of decimals + * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ return typed('fix', { number: function (x) { return (x > 0) ? floor(x) : ceil(x) }, + 'number, number | BigNumber': function (x, n) { + return (x > 0) ? floor(x, n) : ceil(x, n) + }, + Complex: function (x) { return new Complex( (x.re > 0) ? Math.floor(x.re) : Math.ceil(x.re), @@ -44,17 +58,42 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C ) }, + 'Complex, number | BigNumber': function (x, n) { + return new Complex( + (x.re > 0) ? floor(x.re, n) : ceil(x.re, n), + (x.im > 0) ? floor(x.im, n) : ceil(x.im, n) + ) + }, + BigNumber: function (x) { return x.isNegative() ? ceil(x) : floor(x) }, + 'BigNumber, number | BigNumber': function (x, n) { + return x.isNegative() ? ceil(x, n) : floor(x, n) + }, + Fraction: function (x) { return x.s < 0 ? x.ceil() : x.floor() }, + 'Fraction, number | BigNumber': function (x, n) { + return x.s < 0 ? x.ceil(n) : x.floor(n) + }, + 'Array | Matrix': function (x) { // deep map collection, skip zeros since fix(0) = 0 return deepMap(x, this, true) + }, + + 'Array | Matrix, number | BigNumber': function (x, n) { + // deep map collection, skip zeros since fix(0) = 0 + return deepMap(x, i => this(i, n), true) + }, + + 'number | Complex | BigNumber, Array': function (x, y) { + // use matrix implementation + return algorithm14(matrix(y), x, this, true).valueOf() } }) }) diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 5d802deb4a..c2ceeb90ac 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -1,12 +1,18 @@ +import { Decimal } from 'decimal.js' import { factory } from '../../utils/factory' import { deepMap } from '../../utils/collection' import { nearlyEqual } from '../../utils/number' import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual' +import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11' +import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14' const name = 'floor' -const dependencies = ['typed', 'config', 'round'] +const dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar'] + +export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round, matrix, equalScalar }) => { + const algorithm11 = createAlgorithm11({ typed, equalScalar }) + const algorithm14 = createAlgorithm14({ typed }) -export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, config, round }) => { /** * Round a value towards minus infinity. * For matrices, the function is evaluated element wise. @@ -14,6 +20,7 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * Syntax: * * math.floor(x) + * math.floor(x, n) * * Examples: * @@ -22,16 +29,24 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.floor(-4.2) // returns number -5 * math.floor(-4.7) // returns number -5 * - * const c = math.complex(3.2, -2.7) - * math.floor(c) // returns Complex 3 - 3i + * math.floor(3.212, 2) // returns number 3.21 + * math.floor(3.288, 2) // returns number 3.28 + * math.floor(-4.212, 2) // returns number -4.22 + * math.floor(-4.782, 2) // returns number -4.79 + * + * const c = math.complex(3.24, -2.71) + * math.floor(c) // returns Complex 3 - 3i + * math.floor(c, 1) // returns Complex 3.2 - 2.8i * - * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] + * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] + * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] * * See also: * * ceil, fix, round * * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded + * @param {number | BigNumber | Array} [n=0] Number of decimals * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value */ return typed('floor', { @@ -43,10 +58,25 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, + 'number, number': function (x, n) { + if (nearlyEqual(x, round(x, n), config.epsilon)) { + return round(x, n) + } else { + let [number, exponent] = `${x}e`.split('e') + const result = Math.floor(Number(`${number}e${Number(exponent) + n}`)); + [number, exponent] = `${result}e`.split('e') + return Number(`${number}e${Number(exponent) - n}`) + } + }, + Complex: function (x) { return x.floor() }, + 'Complex, number': function (x, n) { + return x.floor(n) + }, + BigNumber: function (x) { if (bigNearlyEqual(x, round(x), config.epsilon)) { return round(x) @@ -55,13 +85,43 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, } }, + 'BigNumber, BigNumber': function (x, n) { + if (bigNearlyEqual(x, round(x, n), config.epsilon)) { + return round(x, n) + } else { + return x.toDecimalPlaces(n.toNumber(), Decimal.ROUND_FLOOR) + } + }, + Fraction: function (x) { return x.floor() }, + 'Fraction, number': function (x, n) { + return x.floor(n) + }, + 'Array | Matrix': function (x) { // deep map collection, skip zeros since floor(0) = 0 return deepMap(x, this, true) + }, + + 'Array | Matrix, number': function (x, n) { + // deep map collection, skip zeros since ceil(0) = 0 + return deepMap(x, i => this(i, n), true) + }, + + 'SparseMatrix, number | BigNumber': function (x, y) { + return algorithm11(x, y, this, false) + }, + + 'DenseMatrix, number | BigNumber': function (x, y) { + return algorithm14(x, y, this, false) + }, + + 'number | Complex | BigNumber, Array': function (x, y) { + // use matrix implementation + return algorithm14(matrix(y), x, this, true).valueOf() } }) }) diff --git a/src/function/arithmetic/round.js b/src/function/arithmetic/round.js index a2d02d7e5d..0216691414 100644 --- a/src/function/arithmetic/round.js +++ b/src/function/arithmetic/round.js @@ -34,10 +34,14 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed, * * Examples: * - * math.round(3.2) // returns number 3 - * math.round(3.8) // returns number 4 + * math.round(3.22) // returns number 3 + * math.round(3.82) // returns number 4 * math.round(-4.2) // returns number -4 * math.round(-4.7) // returns number -5 + * math.round(3.22, 1) // returns number 3.2 + * math.round(3.88, 1) // returns number 3.8 + * math.round(-4.21, 1) // returns number -4.2 + * math.round(-4.71, 1) // returns number -4.7 * math.round(math.pi, 3) // returns number 3.142 * math.round(123.45678, 2) // returns number 123.46 * diff --git a/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index 1e2db273ee..a18aa54f40 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -3,7 +3,7 @@ import assert from 'assert' import approx from '../../../../tools/approx' import math from '../../../../src/bundleAny' -const { bignumber, ceil, complex, fraction, i, isFraction, matrix, pi, unit, parse } = math +const { bignumber, ceil, complex, fraction, i, isFraction, matrix, pi, unit, parse, sparse } = math describe('ceil', function () { it('should return the ceil of a boolean', function () { @@ -25,6 +25,18 @@ describe('ceil', function () { approx.equal(ceil(pi), 4) }) + it('should return the ceil of a number with a given number of decimals', function () { + approx.equal(ceil(0, 5), 0) + approx.equal(ceil(2, 3), 2) + approx.equal(ceil(math.pi, 3), 3.142) + approx.equal(ceil(math.pi, 6), 3.141593) + approx.equal(ceil(1234.5678, 2), 1234.57) + approx.equal(ceil(2.13, 2), 2.13) + approx.equal(ceil(-1.8, 0), -1) + approx.equal(ceil(-1.8, 1), -1.8) + approx.equal(ceil(-2.178, 2), -2.17) + }) + it('should return the ceil of a big number', function () { assert.deepStrictEqual(ceil(bignumber(0)), bignumber(0)) assert.deepStrictEqual(ceil(bignumber(1)), bignumber(1)) @@ -38,6 +50,19 @@ describe('ceil', function () { assert.deepStrictEqual(ceil(bignumber(-2.1)), bignumber(-2)) }) + it('should return the ceil of a big number with a given number of decimals', function () { + assert.deepStrictEqual(ceil(bignumber(0), 3), bignumber(0)) + assert.deepStrictEqual(ceil(bignumber(1), 3), bignumber(1)) + assert.deepStrictEqual(ceil(bignumber(1.315), 2), bignumber(1.32)) + assert.deepStrictEqual(ceil(bignumber(1.8), 1), bignumber(1.8)) + assert.deepStrictEqual(ceil(bignumber(-1), 4), bignumber(-1)) + assert.deepStrictEqual(ceil(bignumber(-1.3), 4), bignumber(-1.3)) + assert.deepStrictEqual(ceil(bignumber(-1.8), 0), bignumber(-1)) + assert.deepStrictEqual(ceil(bignumber(-1.315), 2), bignumber(-1.31)) + assert.deepStrictEqual(ceil(bignumber(-2), 0), bignumber(-2)) + assert.deepStrictEqual(ceil(bignumber(-2.1), 0), bignumber(-2)) + }) + it('should return the ceil of real and imag part of a complex', function () { approx.deepEqual(ceil(complex(0, 0)), complex(0, 0)) approx.deepEqual(ceil(complex(1.3, 1.8)), complex(2, 2)) @@ -45,6 +70,18 @@ describe('ceil', function () { approx.deepEqual(ceil(complex(-1.3, -1.8)), complex(-1, -1)) }) + it('should return the ceil of real and imag part of a complex with a given number of decimals', function () { + approx.deepEqual(ceil(complex(0, 0), 3), complex(0, 0)) + approx.deepEqual(ceil(complex(1.3, 1.8), 0), complex(2, 2)) + approx.deepEqual(ceil(complex(1.3, 1.8), 1), complex(1.3, 1.8)) + approx.deepEqual(ceil(complex(1.315, 1.878), 2), complex(1.32, 1.88)) + approx.deepEqual(ceil(i, 0), complex(0, 1)) + approx.deepEqual(ceil(i, 4), complex(0, 1)) + approx.deepEqual(ceil(complex(-1.3, -1.8), 0), complex(-1, -1)) + approx.deepEqual(ceil(complex(-1.3, -1.8), 1), complex(-1.3, -1.8)) + approx.deepEqual(ceil(complex(-1.315, -1.878), 2), complex(-1.31, -1.87)) + }) + it('should return the ceil of a fraction', function () { const a = fraction('2/3') assert(isFraction(ceil(a))) @@ -62,6 +99,21 @@ describe('ceil', function () { assert.strictEqual(ceil(fraction(-2.1)).toString(), '-2') }) + it('should return the ceil of a fraction with a given number of decimals', function () { + const a = fraction('2/3') + assert(isFraction(ceil(a, 0))) + assert.strictEqual(a.toString(), '0.(6)') + assert(isFraction(ceil(a, 4))) + assert.strictEqual(a.toString(), '0.(6)') + + assert.strictEqual(ceil(fraction(0), 0).toString(), '0') + assert.strictEqual(ceil(fraction(0), 3).toString(), '0') + assert.strictEqual(ceil(fraction(1), 4).toString(), '1') + assert.strictEqual(ceil(fraction(1.315), 2).toString(), '1.32') + assert.strictEqual(ceil(fraction(-1), 0).toString(), '-1') + assert.strictEqual(ceil(fraction(-1.315), 2).toString(), '-1.31') + }) + it('should gracefully handle round-off errors', function () { assert.strictEqual(ceil(3.0000000000000004), 3) assert.strictEqual(ceil(7.999999999999999), 8) @@ -71,6 +123,15 @@ describe('ceil', function () { assert.strictEqual(ceil(799999.9999999999), 800000) assert.strictEqual(ceil(-30000.000000000004), -30000) assert.strictEqual(ceil(-799999.9999999999), -800000) + + assert.strictEqual(ceil(3.0000000000000004, 2), 3) + assert.strictEqual(ceil(7.999999999999999, 2), 8) + assert.strictEqual(ceil(-3.0000000000000004, 2), -3) + assert.strictEqual(ceil(-7.999999999999999, 2), -8) + assert.strictEqual(ceil(30000.000000000004, 2), 30000) + assert.strictEqual(ceil(799999.9999999999, 2), 800000) + assert.strictEqual(ceil(-30000.000000000004, 2), -30000) + assert.strictEqual(ceil(-799999.9999999999, 2), -800000) }) it('should gracefully handle round-off errors with bignumbers', function () { @@ -88,8 +149,17 @@ describe('ceil', function () { assert.throws(function () { ceil(unit('5cm')) }, TypeError, 'Function ceil(unit) not supported') }) + it('should throw an error if requested number of decimals is incorrect', function () { + assert.throws(function () { ceil(2.5, 1.5) }, TypeError, 'Number of decimals in function round must be an integer') + assert.throws(function () { ceil(2.5, -2) }, Error, ' Number of decimals in function round must be in te range of 0-15') + assert.throws(function () { ceil(2.5, Infinity) }, Error, ' Number of decimals in function round must be in te range of 0-15') + }) + it('should convert a string to a number', function () { assert.strictEqual(ceil('1.8'), 2) + assert.strictEqual(ceil('1.815', '2'), 1.82) + assert.strictEqual(ceil('1.815', 2).toString(), '1.82') + assert.strictEqual(ceil(1.815, '2').toString(), '1.82') }) it('should ceil each element in a matrix, array or range', function () { @@ -97,13 +167,49 @@ describe('ceil', function () { approx.deepEqual(ceil(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([2, 4, 6, 8, 10])) }) + it('should ceil each element in a matrix with a given number of decimals', function () { + approx.deepEqual(ceil([1.282, 3.415, -5.121, -10.128], 2), [1.29, 3.42, -5.12, -10.12]) + approx.deepEqual(ceil(matrix([1.282, 3.415, -5.121, -10.128]), 2), matrix([1.29, 3.42, -5.12, -10.12])) + }) + + it('should ceil when number of decimals is provided in an array', function () { + assert.deepStrictEqual(ceil(3.12385, [2, 3]), [3.13, 3.124]) + }) + + it('should ceil dense matrix', function () { + assert.deepStrictEqual(ceil(matrix([[1.712, 2.345], [8.987, -3.565]]), 2), matrix([[1.72, 2.35], [8.99, -3.56]])) + }) + + it('should ceil dense matrix and scalar', function () { + assert.deepStrictEqual(ceil(matrix([[1.7777, 2.3456], [-90.8272, 0]]), 3), matrix([[1.778, 2.346], [-90.827, 0]])) + }) + + it('should ceil dense matrix with given bignumber decimals', function () { + const expected = matrix([[1.778, 2.346], [-90.827, 0]]) + approx.deepEqual(ceil(matrix([[1.7777, 2.3456], [-90.8272, 0]]), bignumber(3)), expected) + }) + + it('should ceil sparse matrix', function () { + assert.deepStrictEqual(ceil(sparse([[1.7, 0], [8.987, -3.565]]), 2), sparse([[1.7, 0], [8.99, -3.56]])) + }) + + it('should ceil sparse matrix and scalar', function () { + assert.deepStrictEqual(ceil(sparse([[1.7777, 2.3456], [-90.8272, 0]]), 3), sparse([[1.778, 2.346], [-90.827, 0]])) + }) + + it('should ceil sparse matrix with given bignumber decimals', function () { + const expected = bignumber(sparse([[1.778, 2.346], [-90.827, 0]])) + assert.deepStrictEqual(ceil(sparse([[1.7777, 2.3456], [-90.8272, 0]]), bignumber(3)), expected) + }) + it('should throw an error in case of invalid number of arguments', function () { assert.throws(function () { ceil() }, /TypeError: Too few arguments/) - assert.throws(function () { ceil(1, 2) }, /TypeError: Too many arguments/) + assert.throws(function () { ceil(1, 2, 3) }, /TypeError: Too many arguments/) }) it('should throw an in case of wrong type of arguments', function () { assert.throws(function () { ceil(null) }, /TypeError: Unexpected type of argument/) + assert.throws(function () { ceil(42, null) }, /TypeError: Unexpected type of argument/) }) it('should LaTeX ceil', function () { diff --git a/test/unit-tests/function/arithmetic/fix.test.js b/test/unit-tests/function/arithmetic/fix.test.js index a51722f90a..87c86c3588 100644 --- a/test/unit-tests/function/arithmetic/fix.test.js +++ b/test/unit-tests/function/arithmetic/fix.test.js @@ -30,6 +30,26 @@ describe('fix', function () { approx.equal(fix(math.pi), 3) }) + it('should round numbers with a given number of decimals', function () { + approx.equal(fix(0, 5), 0) + approx.equal(fix(1, 5), 1) + approx.equal(fix(1.3, 5), 1.3) + approx.equal(fix(1.313, 2), 1.31) + approx.equal(fix(1.383, 2), 1.38) + approx.equal(fix(2.22, 2), 2.22) + approx.equal(fix(-1, 5), -1) + approx.equal(fix(-1.3, 5), -1.3) + approx.equal(fix(-1.883, 2), -1.88) + approx.equal(fix(-1.888, 2), -1.88) + approx.equal(fix(-2.22, 2), -2.22) + approx.equal(fix(math.pi, 6), 3.141592) + + approx.equal(fix(1.313, bignumber(2)), 1.31) + approx.equal(fix(1.383, bignumber(2)), 1.38) + approx.equal(fix(-1.883, bignumber(2)), -1.88) + approx.equal(fix(-1.888, bignumber(2)), -1.88) + }) + it('should round big numbers correctly', function () { assert.deepStrictEqual(fix(bignumber(0)), bignumber(0)) assert.deepStrictEqual(fix(bignumber(1)), bignumber(1)) @@ -43,6 +63,24 @@ describe('fix', function () { assert.deepStrictEqual(fix(bignumber(-2.1)), bignumber(-2)) }) + it('should round big numbers with a given number of decimals', function () { + assert.deepStrictEqual(fix(bignumber(0), 5), bignumber(0)) + assert.deepStrictEqual(fix(bignumber(1), 5), bignumber(1)) + assert.deepStrictEqual(fix(bignumber(1.315), 2), bignumber(1.31)) + assert.deepStrictEqual(fix(bignumber(1.381), 2), bignumber(1.38)) + assert.deepStrictEqual(fix(bignumber(2), 2), bignumber(2)) + assert.deepStrictEqual(fix(bignumber(-1), 2), bignumber(-1)) + assert.deepStrictEqual(fix(bignumber(-1.31), 1), bignumber(-1.3)) + assert.deepStrictEqual(fix(bignumber(-1.38), 1), bignumber(-1.3)) + assert.deepStrictEqual(fix(bignumber(-2.22), 2), bignumber(-2.22)) + + assert.deepStrictEqual(fix(bignumber(1.315), bignumber(2)), bignumber(1.31)) + assert.deepStrictEqual(fix(bignumber(1.381), bignumber(2)), bignumber(1.38)) + assert.deepStrictEqual(fix(bignumber(-1.31), bignumber(1)), bignumber(-1.3)) + assert.deepStrictEqual(fix(bignumber(-1.38), bignumber(1)), bignumber(-1.3)) + assert.deepStrictEqual(fix(bignumber(math.pi), bignumber(6)), bignumber(3.141592)) + }) + it('should round complex numbers correctly', function () { // complex approx.deepEqual(fix(complex(0, 0)), complex(0, 0)) @@ -51,6 +89,18 @@ describe('fix', function () { approx.deepEqual(fix(complex(-1.3, -1.8)), complex(-1, -1)) }) + it('should round complex numbers with a given number of decimals', function () { + approx.deepEqual(fix(complex(0, 0), 5), complex(0, 0)) + approx.deepEqual(fix(complex(1.335, 2.835), 2), complex(1.33, 2.83)) + approx.deepEqual(fix(math.i, 5), complex(0, 1)) + approx.deepEqual(fix(complex(-1.335, -1.835), 2), complex(-1.33, -1.83)) + + approx.deepEqual(fix(complex(0, 0), bignumber(5)), complex(0, 0)) + approx.deepEqual(fix(complex(1.335, 2.835), bignumber(2)), complex(1.33, 2.83)) + approx.deepEqual(fix(math.i, bignumber(5)), complex(0, 1)) + approx.deepEqual(fix(complex(-1.335, -1.835), bignumber(2)), complex(-1.33, -1.83)) + }) + it('should round fractions correctly', function () { const a = fraction('2/3') assert(fix(a) instanceof math.Fraction) @@ -68,6 +118,28 @@ describe('fix', function () { assert.strictEqual(fix(fraction(-2.1)).toString(), '-2') }) + it('should round fractions with a given number of decimals', function () { + const a = fraction('2/3') + assert(fix(a, 3) instanceof math.Fraction) + assert.strictEqual(a.toString(), '0.(6)') + const b = fraction('-2/3') + assert(fix(b, 3) instanceof math.Fraction) + assert.strictEqual(b.toString(), '-0.(6)') + + assert.strictEqual(fix(fraction(0), 5).toString(), '0') + assert.strictEqual(fix(fraction(1), 5).toString(), '1') + assert.strictEqual(fix(fraction(1.33), 1).toString(), '1.3') + assert.strictEqual(fix(fraction(1.38), 1).toString(), '1.3') + assert.strictEqual(fix(fraction(2), 5).toString(), '2') + assert.strictEqual(fix(fraction(-1), 5).toString(), '-1') + assert.strictEqual(fix(fraction(-1.33), 1).toString(), '-1.3') + assert.strictEqual(fix(fraction(-1.38), 1).toString(), '-1.3') + assert.strictEqual(fix(fraction(-2.22), 2).toString(), '-2.22') + + assert.strictEqual(fix(fraction(1.381), bignumber(2)).toString(), '1.38') + assert.strictEqual(fix(fraction(-1.381), bignumber(2)).toString(), '-1.38') + }) + it('should gracefully handle round-off errors', function () { assert.strictEqual(fix(3.0000000000000004), 3) assert.strictEqual(fix(7.999999999999999), 8) @@ -90,13 +162,27 @@ describe('fix', function () { assert.deepStrictEqual(fix(bignumber(-799999.9999999999)), bignumber(-800000)) }) + it('should gracefully handle round-off errors with given number of decimals', function () { + assert.strictEqual(fix(3.0000000000000004, 3), 3) + assert.strictEqual(fix(7.999999999999999, 3), 8) + assert.strictEqual(fix(-3.0000000000000004, 3), -3) + assert.strictEqual(fix(-7.999999999999999, 3), -8) + assert.strictEqual(fix(30000.000000000004, 3), 30000) + assert.strictEqual(fix(799999.9999999999, 3), 800000) + assert.strictEqual(fix(-30000.000000000004, 3), -30000) + assert.strictEqual(fix(-799999.9999999999, 3), -800000) + }) + it('should throw an error on unit as parameter', function () { // unit assert.throws(function () { fix(unit('5cm')) }, TypeError, 'Function fix(unit) not supported') }) it('should convert a string to a number', function () { - assert.strictEqual(fix('1.8'), 1) + assert.strictEqual(fix('1.81'), 1) + assert.strictEqual(fix('1.815', '2').toString(), '1.81') + assert.strictEqual(fix('1.815', 2).toString(), '1.81') + assert.strictEqual(fix(1.815, '2').toString(), '1.81') }) it('should correctly round all values of a matrix element-wise', function () { @@ -105,13 +191,28 @@ describe('fix', function () { approx.deepEqual(fix(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([1, 3, 5, 7, 10])) }) + it('should round all values of a matrix element-wise with a given number of decimals', function () { + approx.deepEqual(fix([1.234, 3.456, 5.678, 7.891, 10.01], 2), [1.23, 3.45, 5.67, 7.89, 10.01]) + approx.deepEqual(fix(matrix([1.234, 3.456, 5.678, 7.891, 10.01]), 2), matrix([1.23, 3.45, 5.67, 7.89, 10.01])) + + approx.deepEqual(fix([1.234, 3.456, 5.678, 7.891, 10.01], bignumber(2)), [1.23, 3.45, 5.67, 7.89, 10.01]) + approx.deepEqual(fix(matrix([1.234, 3.456, 5.678, 7.891, 10.01]), bignumber(2)), matrix([1.23, 3.45, 5.67, 7.89, 10.01])) + }) + + it('should round correctly with decimals provided in an array', function () { + approx.deepEqual(fix(1.234567, [0, 1, 2, 3, 4]), [1, 1.2, 1.23, 1.234, 1.2345]) + approx.deepEqual(fix(bignumber(math.pi), [0, 1, 2, 3, 4]), [3, 3.1, 3.14, 3.141, 3.1415]) + approx.deepEqual(fix(complex(1.335, 2.835), [1, 2]), [complex(1.3, 2.8), complex(1.33, 2.83)]) + }) + it('should throw an error in case of invalid number of arguments', function () { assert.throws(function () { fix() }, /TypeError: Too few arguments/) - assert.throws(function () { fix(1, 2) }, /TypeError: Too many arguments/) + assert.throws(function () { fix(1, 2, 3) }, /TypeError: Too many arguments/) }) it('should throw an in case of wrong type of arguments', function () { assert.throws(function () { fix(null) }, /TypeError: Unexpected type of argument/) + assert.throws(function () { fix(1, null) }, /TypeError: Unexpected type of argument/) }) it('should LaTeX fix', function () { diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index 4c76e08fb5..6160de178a 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -9,6 +9,8 @@ const fraction = math.fraction const matrix = math.matrix const unit = math.unit const floor = math.floor +const i = math.i +const sparse = math.sparse describe('floor', function () { it('should round booleans correctly', function () { @@ -30,6 +32,19 @@ describe('floor', function () { approx.equal(floor(math.pi), 3) }) + it('should return the floor of a number with a given number of decimals', function () { + approx.equal(floor(0, 5), 0) + approx.equal(floor(2, 3), 2) + approx.equal(floor(1.3216, 2), 1.32) + approx.equal(floor(math.pi, 3), 3.141) + approx.equal(floor(math.pi, 6), 3.141593) + approx.equal(floor(1234.5678, 2), 1234.56) + approx.equal(floor(2.135, 2), 2.13) + approx.equal(floor(-1.8, 0), -2) + approx.equal(floor(-1.8, 1), -1.8) + approx.equal(floor(-2.178, 2), -2.18) + }) + it('should floor big numbers correctly', function () { assert.deepStrictEqual(floor(bignumber(0)), bignumber(0)) assert.deepStrictEqual(floor(bignumber(1)), bignumber(1)) @@ -43,13 +58,38 @@ describe('floor', function () { assert.deepStrictEqual(floor(bignumber(-2.1)), bignumber(-3)) }) + it('should return the floor of a big number with a given number of decimals', function () { + assert.deepStrictEqual(floor(bignumber(0), 3), bignumber(0)) + assert.deepStrictEqual(floor(bignumber(1), 3), bignumber(1)) + assert.deepStrictEqual(floor(bignumber(1.315), 2), bignumber(1.31)) + assert.deepStrictEqual(floor(bignumber(1.8), 1), bignumber(1.8)) + assert.deepStrictEqual(floor(bignumber(-1), 4), bignumber(-1)) + assert.deepStrictEqual(floor(bignumber(-1.3), 4), bignumber(-1.3)) + assert.deepStrictEqual(floor(bignumber(-1.8), 0), bignumber(-2)) + assert.deepStrictEqual(floor(bignumber(-1.315), 2), bignumber(-1.32)) + assert.deepStrictEqual(floor(bignumber(-2), 0), bignumber(-2)) + assert.deepStrictEqual(floor(bignumber(-2.1), 0), bignumber(-3)) + }) + it('should floor complex numbers correctly', function () { approx.deepEqual(floor(complex(0, 0)), complex(0, 0)) approx.deepEqual(floor(complex(1.3, 1.8)), complex(1, 1)) - approx.deepEqual(floor(math.i), complex(0, 1)) + approx.deepEqual(floor(i), complex(0, 1)) approx.deepEqual(floor(complex(-1.3, -1.8)), complex(-2, -2)) }) + it('should return the floor of real and imag part of a complex with a given number of decimals', function () { + approx.deepEqual(floor(complex(0, 0), 3), complex(0, 0)) + approx.deepEqual(floor(complex(1.3, 1.8), 0), complex(1, 1)) + approx.deepEqual(floor(complex(1.3, 1.8), 1), complex(1.3, 1.8)) + approx.deepEqual(floor(complex(1.315, 1.878), 2), complex(1.31, 1.87)) + approx.deepEqual(floor(i, 0), complex(0, 1)) + approx.deepEqual(floor(i, 4), complex(0, 1)) + approx.deepEqual(floor(complex(-1.3, -1.8), 0), complex(-2, -2)) + approx.deepEqual(floor(complex(-1.3, -1.8), 1), complex(-1.3, -1.8)) + approx.deepEqual(floor(complex(-1.315, -1.878), 2), complex(-1.32, -1.88)) + }) + it('should floor fractions correctly', function () { const a = fraction('2/3') assert(floor(a) instanceof math.Fraction) @@ -67,6 +107,15 @@ describe('floor', function () { assert.strictEqual(floor(fraction(-2.1)).toString(), '-3') }) + it('should return the floor of a fraction with a given number of decimals', function () { + assert.strictEqual(floor(fraction(0), 0).toString(), '0') + assert.strictEqual(floor(fraction(0), 3).toString(), '0') + assert.strictEqual(floor(fraction(1), 4).toString(), '1') + assert.strictEqual(floor(fraction(1.315), 2).toString(), '1.31') + assert.strictEqual(floor(fraction(-1), 0).toString(), '-1') + assert.strictEqual(floor(fraction(-1.315), 2).toString(), '-1.32') + }) + it('should gracefully handle round-off errors', function () { assert.strictEqual(floor(3.0000000000000004), 3) assert.strictEqual(floor(7.999999999999999), 8) @@ -75,6 +124,14 @@ describe('floor', function () { assert.strictEqual(floor(30000.000000000004), 30000) assert.strictEqual(floor(799999.9999999999), 800000) assert.strictEqual(floor(-30000.000000000004), -30000) + + assert.strictEqual(floor(3.0000000000000004, 2), 3) + assert.strictEqual(floor(7.999999999999999, 2), 8) + assert.strictEqual(floor(-3.0000000000000004, 2), -3) + assert.strictEqual(floor(-7.999999999999999, 2), -8) + assert.strictEqual(floor(30000.000000000004, 2), 30000) + assert.strictEqual(floor(799999.9999999999, 2), 800000) + assert.strictEqual(floor(-30000.000000000004, 2), -30000) }) it('should gracefully handle round-off errors with bignumbers', function () { @@ -93,6 +150,9 @@ describe('floor', function () { it('should convert a string to a number', function () { assert.strictEqual(floor('1.8'), 1) + assert.strictEqual(floor('1.812', '2'), 1.81) + assert.strictEqual(floor('1.812', 2).toString(), '1.81') + assert.strictEqual(floor(1.812, '2').toString(), '1.81') }) it('should floor all elements in a matrix', function () { @@ -100,13 +160,57 @@ describe('floor', function () { approx.deepEqual(floor(matrix([1.2, 3.4, 5.6, 7.8, 10.0])), matrix([1, 3, 5, 7, 10])) }) + it('should floor each element in a matrix with a given number of decimals', function () { + approx.deepEqual(floor([1.282, 3.415, -5.121, -10.128], 2), [1.28, 3.41, -5.13, -10.13]) + approx.deepEqual(floor(matrix([1.282, 3.415, -5.121, -10.128]), 2), matrix([1.28, 3.41, -5.13, -10.13])) + }) + + it('should floor when number of decimals is provided in an array', function () { + assert.deepStrictEqual(floor(3.12385, [2, 3]), [3.12, 3.123]) + }) + + it('should floor dense matrix', function () { + assert.deepStrictEqual(floor(matrix([[1.712, 2.345], [8.987, -3.565]]), 2), matrix([[1.71, 2.34], [8.98, -3.57]])) + }) + + it('should floor dense matrix and scalar', function () { + assert.deepStrictEqual(floor(matrix([[1.7777, 2.3456], [-90.8272, 0]]), 3), matrix([[1.777, 2.345], [-90.828, 0]])) + }) + + it('should floor dense matrix with given bignumber decimals', function () { + const expected = bignumber(matrix([[1.777, 2.345], [-90.828, 0]])) + const decimals = bignumber(3) + approx.deepEqual(floor(matrix([[1.7777, 2.3456], [-90.8272, 0]]), decimals), expected) + }) + + it('should floor sparse matrix', function () { + assert.deepStrictEqual(floor(sparse([[1.7, 0], [8.987, -3.565]]), 2), sparse([[1.7, 0], [8.98, -3.57]])) + }) + + it('should floor sparse matrix and scalar', function () { + assert.deepStrictEqual(floor(sparse([[1.7777, 2.3456], [-90.8272, 0]]), 3), sparse([[1.777, 2.345], [-90.828, 0]])) + }) + + it('should floor sparse matrix with given bignumber decimals', function () { + const expected = bignumber(sparse([[1.777, 2.345], [-90.828, 0]])) + const decimals = bignumber(3) + assert.deepStrictEqual(floor(sparse([[1.7777, 2.3456], [-90.8272, 0]]), decimals), expected) + }) + it('should throw an error in case of invalid number of arguments', function () { assert.throws(function () { floor() }, /TypeError: Too few arguments/) - assert.throws(function () { floor(1, 2) }, /TypeError: Too many arguments/) + assert.throws(function () { floor(1, 2, 3) }, /TypeError: Too many arguments/) }) it('should throw an in case of wrong type of arguments', function () { assert.throws(function () { floor(null) }, /TypeError: Unexpected type of argument/) + assert.throws(function () { floor(42, null) }, /TypeError: Unexpected type of argument/) + }) + + it('should throw an error if requested number of decimals is incorrect', function () { + assert.throws(function () { floor(2.5, 1.5) }, TypeError, 'Number of decimals in function round must be an integer') + assert.throws(function () { floor(2.5, -2) }, Error, ' Number of decimals in function round must be in te range of 0-15') + assert.throws(function () { floor(2.5, Infinity) }, Error, ' Number of decimals in function round must be in te range of 0-15') }) it('should LaTeX floor', function () { diff --git a/test/unit-tests/function/arithmetic/round.test.js b/test/unit-tests/function/arithmetic/round.test.js index 3d525e4e27..10980d3c6d 100644 --- a/test/unit-tests/function/arithmetic/round.test.js +++ b/test/unit-tests/function/arithmetic/round.test.js @@ -45,6 +45,12 @@ describe('round', function () { assert.throws(function () { round(math.pi, -2) }, /Number of decimals in function round must be in te range of 0-15/) assert.throws(function () { round(math.pi, 20) }, /Number of decimals in function round must be in te range of 0-15/) assert.throws(function () { round(math.pi, 2.5) }, /Number of decimals in function round must be an integer/) + assert.throws(function () { round(1, 1.2) }, /TypeError: Number of decimals in function round must be an integer/) + assert.throws(function () { round(1, bignumber(1.2)) }, /TypeError: Number of decimals in function round must be an integer/) + assert.throws(function () { round(math.complex(1, 1), 1.2) }, /TypeError: Number of decimals in function round must be an integer/) + assert.throws(function () { round(math.complex(1, 1), bignumber(1.2)) }, /TypeError: Number of decimals in function round must be an integer/) + assert.throws(function () { round(bignumber(1.2), bignumber(1.2)) }, /TypeError: Number of decimals in function round must be an integer/) + assert.throws(function () { round(round(fraction('1/2'), 1.2)) }, /TypeError: Number of decimals in function round must be an integer/) }) it('should throw an error if used with wrong number of arguments', function () { @@ -78,6 +84,28 @@ describe('round', function () { assert.strictEqual(round(fraction('2/3')).toString(), '1') assert.strictEqual(round(fraction('1/3')).toString(), '0') assert.strictEqual(round(fraction('1/2')).toString(), '1') + + assert.strictEqual(round(fraction('1/2'), 1).toString(), '0.5') + }) + + it('should gracefully handle round-off errors', function () { + assert.strictEqual(round(3.0000000000000004), 3) + assert.strictEqual(round(7.999999999999999), 8) + assert.strictEqual(round(-3.0000000000000004), -3) + assert.strictEqual(round(-7.999999999999999), -8) + assert.strictEqual(round(30000.000000000004), 30000) + assert.strictEqual(round(799999.9999999999), 800000) + assert.strictEqual(round(-30000.000000000004), -30000) + assert.strictEqual(round(-799999.9999999999), -800000) + + assert.strictEqual(round(3.0000000000000004, 2), 3) + assert.strictEqual(round(7.999999999999999, 2), 8) + assert.strictEqual(round(-3.0000000000000004, 2), -3) + assert.strictEqual(round(-7.999999999999999, 2), -8) + assert.strictEqual(round(30000.000000000004, 2), 30000) + assert.strictEqual(round(799999.9999999999, 2), 800000) + assert.strictEqual(round(-30000.000000000004, 2), -30000) + assert.strictEqual(round(-799999.9999999999, 2), -800000) }) it('should round real and imag part of a complex number', function () { @@ -126,6 +154,7 @@ describe('round', function () { it('should round dense matrix and scalar', function () { assert.deepStrictEqual(round(matrix([[1.7777, 2.3456], [-90.8272, 0]]), 3), matrix([[1.778, 2.346], [-90.827, 0]])) assert.deepStrictEqual(round(3.12385, matrix([[2, 3], [0, 2]])), matrix([[3.12, 3.124], [3, 3.12]])) + assert.deepStrictEqual(round(0.0, matrix([2, 3])), matrix([0, 0])) }) }) @@ -137,6 +166,7 @@ describe('round', function () { it('should round sparse matrix and scalar', function () { assert.deepStrictEqual(round(sparse([[1.7777, 2.3456], [-90.8272, 0]]), 3), sparse([[1.778, 2.346], [-90.827, 0]])) assert.deepStrictEqual(round(3.12385, sparse([[2, 3], [0, 2]])), matrix([[3.12, 3.124], [3, 3.12]])) + assert.deepStrictEqual(round(0.0, sparse([2, 3])), sparse([0, 0])) }) })