Skip to content

Commit

Permalink
fix #2761: extend function round with support for units
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Nov 15, 2023
1 parent 9f4b8e8 commit 23fcb98
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion test/node-tests/doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ Chaining examples
expectTypeOf(math.chain([1]).round()).toMatchTypeOf<
MathJsChain<MathCollection>
>()
expectTypeOf(
math.chain(math.unit('5.2cm')).round(math.unit('cm'))
).toMatchTypeOf<MathJsChain<Unit>>()

// cube
expectTypeOf(math.chain(1).cube()).toMatchTypeOf<MathJsChain<number>>()
Expand Down Expand Up @@ -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])
Expand Down
36 changes: 25 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ export interface MathJsInstance extends MathJsFactory {
x: T,
n?: number | BigNumber
): NoLiteralType<T>
round<U extends MathCollection>(x: MathNumericType | U, n: U): U
round<U extends MathCollection>(x: MathNumericType, n: U): U
round(x: Unit, n: number | BigNumber, unit: Unit): Unit
round(x: Unit, unit: Unit): Unit
round<U extends MathCollection>(x: U, unit: Unit): U
Expand Down Expand Up @@ -4745,27 +4745,41 @@ export interface MathJsChain<TValue> {
*/
round<T extends MathNumericType | MathCollection>(
this: MathJsChain<T>,
n?: number | BigNumber
): NoLiteralType<T>
n?: number | BigNumber | MathCollection
): MathJsChain<T>
round<U extends MathCollection>(
this: MathJsChain<MathNumericType | U>,
n: U
): U
round(this: MathJsChain<Unit>, n: number | BigNumber, unit: Unit): Unit
round(this: MathJsChain<Unit>, unit: Unit): Unit
round<U extends MathCollection>(this: MathJsChain<U>, unit: Unit): U
): MathJsChain<U>
round(
this: MathJsChain<Unit>,
n: number | BigNumber,
unit: Unit
): MathJsChain<Unit>
round(this: MathJsChain<Unit>, unit: Unit): MathJsChain<Unit>
round<U extends MathCollection>(
this: MathJsChain<U>,
unit: Unit
): MathJsChain<U>
round<U extends MathCollection>(
this: MathJsChain<U>,
n: number | BigNumber,
unit: Unit
): U
round<U extends MathCollection>(this: MathJsChain<Unit>, n: U, unit: Unit): U
): MathJsChain<U>
round<U extends MathCollection>(
this: MathJsChain<Unit>,
n: U,
unit: Unit
): MathJsChain<U>
round<U extends MathCollection>(
this: MathJsChain<Unit>,
n: number | BigNumber,
unit: U
): U
round<U extends MathCollection>(this: MathJsChain<Unit>, unit: U): U
): MathJsChain<U>
round<U extends MathCollection>(
this: MathJsChain<Unit>,
unit: U
): MathJsChain<U>

// End of rounding group

Expand Down

0 comments on commit 23fcb98

Please sign in to comment.