From 44c2827c8027007129ef014c22197da98a14eba1 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Mon, 14 Sep 2020 20:51:07 +0200 Subject: [PATCH 01/12] floor and cell with precision --- src/function/arithmetic/ceil.js | 63 ++++++++++++- .../function/arithmetic/ceil.test.js | 92 ++++++++++++++++++- 2 files changed, 150 insertions(+), 5 deletions(-) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index 12a8d90a91..a4e076e3f1 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,22 @@ 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 { + return ceilNumber(x * Math.pow(10, n)) * Math.pow(10, -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 +84,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/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index 1e2db273ee..315d047b6d 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,19 @@ 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(1.3216, 2), 1.33) + 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.135, 2), 2.14) + 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 +51,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 +71,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 +100,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) @@ -88,8 +141,15 @@ 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) }) it('should ceil each element in a matrix, array or range', function () { @@ -97,13 +157,41 @@ 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, array or range 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 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 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 () { From 8e1792a653af5be0558c3baed69639db2e6e5b50 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Mon, 14 Sep 2020 21:11:51 +0200 Subject: [PATCH 02/12] appends missing files --- src/function/arithmetic/ceil.js | 5 +- src/function/arithmetic/floor.js | 65 +++++++++++++- .../function/arithmetic/ceil.test.js | 6 +- .../function/arithmetic/floor.test.js | 86 ++++++++++++++++++- 4 files changed, 152 insertions(+), 10 deletions(-) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index a4e076e3f1..ea33d54164 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -64,7 +64,10 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, if (nearlyEqual(x, round(x, n), config.epsilon)) { return round(x, n) } else { - return ceilNumber(x * Math.pow(10, n)) * Math.pow(10, -n) + let [number, exponent] = `${x}e`.split('e') + const result = Math.ceil(`${number}e${Number(exponent) + n}`); + [number, exponent] = `${result}e`.split('e') + return Number(`${number}e${Number(exponent) - n}`) } }, diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 5d802deb4a..9433c1a1d9 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. @@ -21,17 +27,25 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.floor(3.8) // returns number 3 * math.floor(-4.2) // returns number -5 * math.floor(-4.7) // returns number -5 + * + * 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.2, -2.7) + * 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.ceil([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 +57,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}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 +84,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/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index 315d047b6d..25ea7a455e 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -157,17 +157,15 @@ 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, array or range with a given number of decimals', function () { + 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]])) }) @@ -177,7 +175,7 @@ describe('ceil', function () { }) 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]])) + 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 () { diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index 4c76e08fb5..b19a59132d 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) @@ -93,6 +142,7 @@ 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) }) it('should floor all elements in a matrix', function () { @@ -100,13 +150,45 @@ 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 ceil 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 ceil 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 ceil 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 ceil 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 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 () { From 5896c227fb81c461e273a4460b4d30148d7c5168 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Mon, 14 Sep 2020 21:17:40 +0200 Subject: [PATCH 03/12] fixing lint --- src/function/arithmetic/floor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 9433c1a1d9..68d5801e7e 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -27,17 +27,17 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.floor(3.8) // returns number 3 * math.floor(-4.2) // returns number -5 * math.floor(-4.7) // returns number -5 - * + * * 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) // 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.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] * * See also: From bcc0199d2f345f716e83e225871625e51dce3772 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sat, 19 Sep 2020 08:40:31 +0200 Subject: [PATCH 04/12] fixing ceil and floor tests: string typing issue --- test/unit-tests/function/arithmetic/ceil.test.js | 4 +++- test/unit-tests/function/arithmetic/floor.test.js | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index 25ea7a455e..d75480b586 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -149,7 +149,9 @@ describe('ceil', function () { 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'), 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 () { diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index b19a59132d..211bab4e20 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -142,7 +142,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') + assert.strictEqual(floor(1.812, '2').toString(), '1.81') }) it('should floor all elements in a matrix', function () { From 00638da6a111740fcf6395d93d70512561b5c060 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sat, 19 Sep 2020 08:44:05 +0200 Subject: [PATCH 05/12] fixing ceil and floor tests: string typing issue --- test/unit-tests/function/arithmetic/floor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index 211bab4e20..c488abe133 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -142,7 +142,7 @@ describe('floor', function () { it('should convert a string to a number', function () { assert.strictEqual(floor('1.8'), 1) - assert.strictEqual(floor('1.812', '2').toString(), '1.81') + 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') }) From 9299fabca27b3353cf46e32698f643265325aa48 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sat, 19 Sep 2020 08:45:46 +0200 Subject: [PATCH 06/12] fixing ceil and floor tests: string typing issue --- test/unit-tests/function/arithmetic/floor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index c488abe133..00ddfaf043 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -142,7 +142,7 @@ 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'), 1.81) assert.strictEqual(floor('1.812', 2).toString(), '1.81') assert.strictEqual(floor(1.812, '2').toString(), '1.81') }) From 8a6bc763b9caa4edb3fe140c46ab1d399a57b659 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sat, 19 Sep 2020 08:51:23 +0200 Subject: [PATCH 07/12] fixing ceil and floor tests: string typing issue --- test/unit-tests/function/arithmetic/ceil.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index d75480b586..8cc57b5550 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -150,8 +150,8 @@ describe('ceil', function () { 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') + 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 () { From 0224eb1ed6cc3e5bbfb3ea974d045c261dff9d64 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sun, 27 Sep 2020 08:27:31 +0200 Subject: [PATCH 08/12] adding fix(x,n); adding more tests for floor and ceil --- src/function/arithmetic/ceil.js | 2 +- src/function/arithmetic/fix.js | 35 +++++++- src/function/arithmetic/floor.js | 5 +- src/function/arithmetic/round.js | 8 +- .../function/arithmetic/ceil.test.js | 12 ++- .../function/arithmetic/fix.test.js | 79 ++++++++++++++++++- .../function/arithmetic/floor.test.js | 8 ++ .../function/arithmetic/round.test.js | 20 +++++ 8 files changed, 156 insertions(+), 13 deletions(-) diff --git a/src/function/arithmetic/ceil.js b/src/function/arithmetic/ceil.js index ea33d54164..f8cf5f1e53 100644 --- a/src/function/arithmetic/ceil.js +++ b/src/function/arithmetic/ceil.js @@ -65,7 +65,7 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed, return round(x, n) } else { let [number, exponent] = `${x}e`.split('e') - const result = Math.ceil(`${number}e${Number(exponent) + n}`); + const result = Math.ceil(Number(`${number}e${Number(exponent) + n}`)); [number, exponent] = `${result}e`.split('e') return Number(`${number}e${Number(exponent) - n}`) } diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 7f3fe30352..665e434fd9 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -20,23 +20,30 @@ 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) + * 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': 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 +51,37 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C ) }, + 'Complex, number': 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': 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': 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': function (x, n) { + // deep map collection, skip zeros since fix(0) = 0 + return deepMap(x, i => this(i, n), true) } }) }) diff --git a/src/function/arithmetic/floor.js b/src/function/arithmetic/floor.js index 68d5801e7e..c2ceeb90ac 100644 --- a/src/function/arithmetic/floor.js +++ b/src/function/arithmetic/floor.js @@ -20,6 +20,7 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * Syntax: * * math.floor(x) + * math.floor(x, n) * * Examples: * @@ -38,7 +39,7 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, * math.floor(c, 1) // returns Complex 3.2 - 2.8i * * math.floor([3.2, 3.8, -4.7]) // returns Array [3, 3, -5] - * math.ceil([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] + * math.floor([3.21, 3.82, -4.71], 1) // returns Array [3.2, 3.8, -4.8] * * See also: * @@ -62,7 +63,7 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed, return round(x, n) } else { let [number, exponent] = `${x}e`.split('e') - const result = Math.floor(`${number}e${Number(exponent) + n}`); + const result = Math.floor(Number(`${number}e${Number(exponent) + n}`)); [number, exponent] = `${result}e`.split('e') return Number(`${number}e${Number(exponent) - n}`) } 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 8cc57b5550..aeac2ca1e7 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -28,11 +28,10 @@ describe('ceil', function () { 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(1.3216, 2), 1.33) 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.135, 2), 2.14) + 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) @@ -124,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 () { diff --git a/test/unit-tests/function/arithmetic/fix.test.js b/test/unit-tests/function/arithmetic/fix.test.js index a51722f90a..410a93703c 100644 --- a/test/unit-tests/function/arithmetic/fix.test.js +++ b/test/unit-tests/function/arithmetic/fix.test.js @@ -30,6 +30,21 @@ 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) + }) + it('should round big numbers correctly', function () { assert.deepStrictEqual(fix(bignumber(0)), bignumber(0)) assert.deepStrictEqual(fix(bignumber(1)), bignumber(1)) @@ -43,6 +58,18 @@ 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)) + }) + it('should round complex numbers correctly', function () { // complex approx.deepEqual(fix(complex(0, 0)), complex(0, 0)) @@ -51,6 +78,14 @@ 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 () { + // complex + 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)) + }) + it('should round fractions correctly', function () { const a = fraction('2/3') assert(fix(a) instanceof math.Fraction) @@ -68,6 +103,25 @@ 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') + }) + it('should gracefully handle round-off errors', function () { assert.strictEqual(fix(3.0000000000000004), 3) assert.strictEqual(fix(7.999999999999999), 8) @@ -90,13 +144,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'), 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 +173,20 @@ 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 () { + // matrix, array, range + 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])) + }) + 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 00ddfaf043..26bc3de087 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -124,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 () { diff --git a/test/unit-tests/function/arithmetic/round.test.js b/test/unit-tests/function/arithmetic/round.test.js index 3d525e4e27..74a0d8ec15 100644 --- a/test/unit-tests/function/arithmetic/round.test.js +++ b/test/unit-tests/function/arithmetic/round.test.js @@ -80,6 +80,26 @@ describe('round', function () { assert.strictEqual(round(fraction('1/2')).toString(), '1') }) + 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 () { assert.deepStrictEqual(round(math.complex(2.2, math.pi)), math.complex(2, 3)) }) From c38efeac323afb05b155dd43360dfe4201c4434d Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sun, 27 Sep 2020 10:27:52 +0200 Subject: [PATCH 09/12] rising coverage --- .../function/arithmetic/ceil.test.js | 10 ++++++++++ .../function/arithmetic/floor.test.js | 20 +++++++++++++++---- .../function/arithmetic/round.test.js | 10 ++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/test/unit-tests/function/arithmetic/ceil.test.js b/test/unit-tests/function/arithmetic/ceil.test.js index aeac2ca1e7..a18aa54f40 100644 --- a/test/unit-tests/function/arithmetic/ceil.test.js +++ b/test/unit-tests/function/arithmetic/ceil.test.js @@ -184,6 +184,11 @@ describe('ceil', 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]])) }) @@ -192,6 +197,11 @@ describe('ceil', 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, 3) }, /TypeError: Too many arguments/) diff --git a/test/unit-tests/function/arithmetic/floor.test.js b/test/unit-tests/function/arithmetic/floor.test.js index 26bc3de087..6160de178a 100644 --- a/test/unit-tests/function/arithmetic/floor.test.js +++ b/test/unit-tests/function/arithmetic/floor.test.js @@ -169,22 +169,34 @@ describe('floor', function () { assert.deepStrictEqual(floor(3.12385, [2, 3]), [3.12, 3.123]) }) - it('should ceil dense matrix', function () { + 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 ceil dense matrix and scalar', function () { + 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 ceil sparse matrix', function () { + 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 ceil sparse matrix and scalar', function () { + 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, 3) }, /TypeError: Too many arguments/) diff --git a/test/unit-tests/function/arithmetic/round.test.js b/test/unit-tests/function/arithmetic/round.test.js index 74a0d8ec15..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,8 @@ 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 () { @@ -146,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])) }) }) @@ -157,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])) }) }) From 49494ebc014b7b4410ada92741222b820f581408 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sun, 27 Sep 2020 10:34:01 +0200 Subject: [PATCH 10/12] adding docs for fix(x,n) --- src/function/arithmetic/fix.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 665e434fd9..9efcff62e4 100644 --- a/src/function/arithmetic/fix.js +++ b/src/function/arithmetic/fix.js @@ -20,6 +20,11 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C * math.fix(-4.2) // returns number -4 * math.fix(-4.7) // returns number -4 * + * 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 From fbac9110271b810e20eef74e2481b63fa87ba315 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sun, 27 Sep 2020 12:04:29 +0200 Subject: [PATCH 11/12] completing fix(x, n) --- src/function/arithmetic/fix.js | 23 +++++++++----- .../function/arithmetic/fix.test.js | 30 +++++++++++++++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/function/arithmetic/fix.js b/src/function/arithmetic/fix.js index 9efcff62e4..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. @@ -36,7 +38,7 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C * * ceil, floor, round * - * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded + * @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 */ @@ -45,7 +47,7 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return (x > 0) ? floor(x) : ceil(x) }, - 'number, number': function (x, n) { + 'number, number | BigNumber': function (x, n) { return (x > 0) ? floor(x, n) : ceil(x, n) }, @@ -56,7 +58,7 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C ) }, - 'Complex, number': function (x, n) { + '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) @@ -67,7 +69,7 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return x.isNegative() ? ceil(x) : floor(x) }, - 'BigNumber, number': function (x, n) { + 'BigNumber, number | BigNumber': function (x, n) { return x.isNegative() ? ceil(x, n) : floor(x, n) }, @@ -75,7 +77,7 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return x.s < 0 ? x.ceil() : x.floor() }, - 'Fraction, number': function (x, n) { + 'Fraction, number | BigNumber': function (x, n) { return x.s < 0 ? x.ceil(n) : x.floor(n) }, @@ -84,9 +86,14 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C return deepMap(x, this, true) }, - 'Array | Matrix, number': function (x, n) { + '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/test/unit-tests/function/arithmetic/fix.test.js b/test/unit-tests/function/arithmetic/fix.test.js index 410a93703c..23e25a7c16 100644 --- a/test/unit-tests/function/arithmetic/fix.test.js +++ b/test/unit-tests/function/arithmetic/fix.test.js @@ -43,6 +43,11 @@ describe('fix', function () { 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 () { @@ -68,6 +73,12 @@ describe('fix', function () { 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 () { @@ -79,11 +90,15 @@ describe('fix', function () { }) it('should round complex numbers with a given number of decimals', function () { - // complex 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 () { @@ -120,6 +135,9 @@ describe('fix', function () { 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 () { @@ -174,9 +192,17 @@ describe('fix', function () { }) it('should round all values of a matrix element-wise with a given number of decimals', function () { - // matrix, array, range 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 () { From f5faf63b208b105fe24110d72136df8b46a32d63 Mon Sep 17 00:00:00 2001 From: rnd-debug Date: Sun, 27 Sep 2020 12:16:06 +0200 Subject: [PATCH 12/12] fixing string typing issue --- test/unit-tests/function/arithmetic/fix.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit-tests/function/arithmetic/fix.test.js b/test/unit-tests/function/arithmetic/fix.test.js index 23e25a7c16..87c86c3588 100644 --- a/test/unit-tests/function/arithmetic/fix.test.js +++ b/test/unit-tests/function/arithmetic/fix.test.js @@ -180,7 +180,7 @@ describe('fix', function () { it('should convert a string to a number', function () { assert.strictEqual(fix('1.81'), 1) - assert.strictEqual(fix('1.815', '2'), 1.81) + 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') })