From 23fcb9862aeb866d6c13b46bcf7155bec97b5c0d Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 15 Nov 2023 13:51:12 +0100 Subject: [PATCH] fix #2761: extend function `round` with support for units --- test/node-tests/doc.test.js | 2 +- test/typescript-tests/testTypes.ts | 13 +++++++++++ types/index.d.ts | 36 +++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/test/node-tests/doc.test.js b/test/node-tests/doc.test.js index a33b2f7c5e..e01b18a3ef 100644 --- a/test/node-tests/doc.test.js +++ b/test/node-tests/doc.test.js @@ -99,7 +99,7 @@ const knownProblems = new Set([ 'mod', 'invmod', 'floor', 'fix', 'expm1', 'exp', 'dotPow', 'dotMultiply', 'dotDivide', 'divide', 'ceil', 'cbrt', 'add', 'usolveAll', 'usolve', 'slu', 'rationalize', 'qr', 'lusolve', 'lup', 'lsolveAll', 'lsolve', 'derivative', - 'symbolicEqual', 'map', 'schur', 'sylvester', 'freqz' + 'symbolicEqual', 'map', 'schur', 'sylvester', 'freqz', 'round' ]) function maybeCheckExpectation (name, expected, expectedFrom, got, gotFrom) { diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 8ce11c3f87..730fb665f7 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -633,6 +633,9 @@ Chaining examples expectTypeOf(math.chain([1]).round()).toMatchTypeOf< MathJsChain >() + expectTypeOf( + math.chain(math.unit('5.2cm')).round(math.unit('cm')) + ).toMatchTypeOf>() // cube expectTypeOf(math.chain(1).cube()).toMatchTypeOf>() @@ -1904,6 +1907,16 @@ Function round examples math.complex(3.2, -2.7) ) + // unit input + assert.deepStrictEqual( + math.round(math.unit('5.21 cm'), math.unit('cm')), + math.unit('5 cm') + ) + assert.deepStrictEqual( + math.round(math.unit('5.21 cm'), 1, math.unit('cm')), + math.unit('5.2 cm') + ) + // array input assert.deepStrictEqual(math.round([3.2, 3.8, -4.7]), [3, 4, -5]) assert.deepStrictEqual(math.round([3.21, 3.82, -4.71], 1), [3.2, 3.8, -4.7]) diff --git a/types/index.d.ts b/types/index.d.ts index 22e467b0ff..7ebc8e0f79 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1103,7 +1103,7 @@ export interface MathJsInstance extends MathJsFactory { x: T, n?: number | BigNumber ): NoLiteralType - round(x: MathNumericType | U, n: U): U + round(x: MathNumericType, n: U): U round(x: Unit, n: number | BigNumber, unit: Unit): Unit round(x: Unit, unit: Unit): Unit round(x: U, unit: Unit): U @@ -4745,27 +4745,41 @@ export interface MathJsChain { */ round( this: MathJsChain, - n?: number | BigNumber - ): NoLiteralType + n?: number | BigNumber | MathCollection + ): MathJsChain round( this: MathJsChain, n: U - ): U - round(this: MathJsChain, n: number | BigNumber, unit: Unit): Unit - round(this: MathJsChain, unit: Unit): Unit - round(this: MathJsChain, unit: Unit): U + ): MathJsChain + round( + this: MathJsChain, + n: number | BigNumber, + unit: Unit + ): MathJsChain + round(this: MathJsChain, unit: Unit): MathJsChain + round( + this: MathJsChain, + unit: Unit + ): MathJsChain round( this: MathJsChain, n: number | BigNumber, unit: Unit - ): U - round(this: MathJsChain, n: U, unit: Unit): U + ): MathJsChain + round( + this: MathJsChain, + n: U, + unit: Unit + ): MathJsChain round( this: MathJsChain, n: number | BigNumber, unit: U - ): U - round(this: MathJsChain, unit: U): U + ): MathJsChain + round( + this: MathJsChain, + unit: U + ): MathJsChain // End of rounding group