Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds unit support to ceil, floor, and fix functions #3269

Merged
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/expression/embeddedDocs/function/arithmetic/ceil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ export const ceilDocs = {
name: 'ceil',
category: 'Arithmetic',
syntax: [
'ceil(x)'
'ceil(x)',
'ceil(x, n)',
'ceil(unit, valuelessUnit)',
'ceil(unit, n, valuelessUnit)'
],
description:
'Round a value towards plus infinity. If x is complex, both real and imaginary part are rounded towards plus infinity.',
examples: [
'ceil(3.2)',
'ceil(3.8)',
'ceil(-4.2)'
'ceil(-4.2)',
'ceil(3.241cm, cm)',
'ceil(3.241cm, 2, cm)'
],
seealso: ['floor', 'fix', 'round']
}
9 changes: 7 additions & 2 deletions src/expression/embeddedDocs/function/arithmetic/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ export const fixDocs = {
name: 'fix',
category: 'Arithmetic',
syntax: [
'fix(x)'
'fix(x)',
'fix(x, n)',
'fix(unit, valuelessUnit)',
'fix(unit, n, valuelessUnit)'
],
description:
'Round a value towards zero. If x is complex, both real and imaginary part are rounded towards zero.',
examples: [
'fix(3.2)',
'fix(3.8)',
'fix(-4.2)',
'fix(-4.8)'
'fix(-4.8)',
'fix(3.241cm, cm)',
'fix(3.241cm, 2, cm)'
],
seealso: ['ceil', 'floor', 'round']
}
9 changes: 7 additions & 2 deletions src/expression/embeddedDocs/function/arithmetic/floor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ export const floorDocs = {
name: 'floor',
category: 'Arithmetic',
syntax: [
'floor(x)'
'floor(x)',
'floor(x, n)',
'floor(unit, valuelessUnit)',
'floor(unit, n, valuelessUnit)'
],
description:
'Round a value towards minus infinity.If x is complex, both real and imaginary part are rounded towards minus infinity.',
examples: [
'floor(3.2)',
'floor(3.8)',
'floor(-4.2)'
'floor(-4.2)',
'floor(3.241cm, cm)',
'floor(3.241cm, 2, cm)'
],
seealso: ['ceil', 'fix', 'round']
}
27 changes: 25 additions & 2 deletions src/function/arithmetic/ceil.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
*
* math.ceil(x)
* math.ceil(x, n)
* math.ceil(unit, valuelessUnit)
* math.ceil(unit, n, valuelessUnit)
*
* Examples:
*
Expand All @@ -67,16 +69,23 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* math.ceil(c) // returns Complex 4 - 2i
* math.ceil(c, 1) // returns Complex 3.3 - 2.7i
*
* const unit = math.unit('3.241 cm')
* const cm = math.unit('cm')
* const mm = math.unit('mm')
* math.ceil(unit, 1, cm) // returns Unit 3.3 cm
* math.ceil(unit, 1, mm) // returns Unit 32.5 mm
*
* 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 | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
* @param {number | BigNumber | Array} [n=0] Number of decimals
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
* @param {Unit} [valuelessUnit] A valueless unit
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
*/
return typed('ceil', {
number: ceilNumber.signatures.number,
Expand Down Expand Up @@ -122,6 +131,20 @@ export const createCeil = /* #__PURE__ */ factory(name, dependencies, ({ typed,
return x.ceil(n.toNumber())
},

'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
josdejong marked this conversation as resolved.
Show resolved Hide resolved
const valueless = x.toNumeric(unit)
return unit.multiply(self(valueless, n))
}),

'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),

'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
// deep map collection, skip zeros since ceil(0) = 0
return deepMap(x, (value) => self(value, n, unit), true)
}),

'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),

'Array | Matrix': typed.referToSelf(self => (x) => {
// deep map collection, skip zeros since ceil(0) = 0
return deepMap(x, self, true)
Expand Down
29 changes: 26 additions & 3 deletions src/function/arithmetic/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C
*
* math.fix(x)
* math.fix(x,n)
* math.fix(unit, valuelessUnit)
* math.fix(unit, n, valuelessUnit)
*
* Examples:
*
Expand All @@ -50,16 +52,23 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C
* math.fix(c) // returns Complex 3 - 2i
* math.fix(c, 1) // returns Complex 3.2 -2.7i
*
* const unit = math.unit('3.241 cm')
* const cm = math.unit('cm')
* const mm = math.unit('mm')
* math.fix(unit, 1, cm) // returns Unit 3.2 cm
* math.fix(unit, 1, mm) // returns Unit 32.4 mm
*
* 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
* @param {number | BigNumber | Array} [n=0] Number of decimals
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
* @param {number | BigNumber | Array} [n=0] Number of decimals
* @param {Unit} [valuelessUnit] A valueless unit
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
*/
return typed('fix', {
number: fixNumber.signatures.number,
Expand Down Expand Up @@ -103,6 +112,20 @@ export const createFix = /* #__PURE__ */ factory(name, dependencies, ({ typed, C
return x.s < 0 ? ceil(x, n) : floor(x, n)
},

'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
const valueless = x.toNumeric(unit)
return unit.multiply(self(valueless, n))
}),

'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),

'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
// deep map collection, skip zeros since fix(0) = 0
return deepMap(x, (value) => self(value, n, unit), true)
}),

'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),

'Array | Matrix': typed.referToSelf(self => (x) => {
// deep map collection, skip zeros since fix(0) = 0
return deepMap(x, self, true)
Expand Down
27 changes: 25 additions & 2 deletions src/function/arithmetic/floor.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
*
* math.floor(x)
* math.floor(x, n)
* math.floor(unit, valuelessUnit)
* math.floor(unit, n, valuelessUnit)
*
* Examples:
*
Expand All @@ -66,6 +68,12 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* math.floor(c) // returns Complex 3 - 3i
* math.floor(c, 1) // returns Complex 3.2 -2.8i
*
* const unit = math.unit('3.241 cm')
* const cm = math.unit('cm')
* const mm = math.unit('mm')
* math.floor(unit, 1, cm) // returns Unit 3.2 cm
* math.floor(unit, 1, mm) // returns Unit 32.4 mm
*
* 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]
*
Expand All @@ -77,9 +85,10 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
*
* ceil, fix, round
*
* @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
* @param {number | BigNumber | Array} [n=0] Number of decimals
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
* @param {Unit} [valuelessUnit] A valueless unit
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
*/
return typed('floor', {
number: floorNumber.signatures.number,
Expand Down Expand Up @@ -125,6 +134,20 @@ export const createFloor = /* #__PURE__ */ factory(name, dependencies, ({ typed,
return x.floor(n.toNumber())
},

'Unit, number, Unit': typed.referToSelf(self => function (x, n, unit) {
const valueless = x.toNumeric(unit)
return unit.multiply(self(valueless, n))
}),

'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),

'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
// deep map collection, skip zeros since floor(0) = 0
return deepMap(x, (value) => self(value, n, unit), true)
}),

'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),

'Array | Matrix': typed.referToSelf(self => (x) => {
// deep map collection, skip zeros since floor(0) = 0
return deepMap(x, self, true)
Expand Down
10 changes: 3 additions & 7 deletions src/function/arithmetic/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,
* @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x Value to be rounded
* @param {number | BigNumber | Array} [n=0] Number of decimals
* @param {Unit} [valuelessUnit] A valueless unit
* @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
* @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} Rounded value
*/
return typed(name, {
number: function (x) {
Expand Down Expand Up @@ -154,16 +154,12 @@ export const createRound = /* #__PURE__ */ factory(name, dependencies, ({ typed,

'Unit, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),
josdejong marked this conversation as resolved.
Show resolved Hide resolved

'Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),

'Array | Matrix, number, Unit': typed.referToSelf(self => (x, n, unit) => {
'Array | Matrix, number | BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => {
// deep map collection, skip zeros since round(0) = 0
return deepMap(x, (value) => self(value, n, unit), true)
}),

'Array | Matrix, BigNumber, Unit': typed.referToSelf(self => (x, n, unit) => self(x, n.toNumber(), unit)),

'Array | Matrix, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),
'Array | Matrix | Unit, Unit': typed.referToSelf(self => (x, unit) => self(x, 0, unit)),

'Array | Matrix': typed.referToSelf(self => x => {
// deep map collection, skip zeros since round(0) = 0
Expand Down
Loading