From fdc6ec67ce179ebd75a10dd67818faf883d56e54 Mon Sep 17 00:00:00 2001 From: stefangomez Date: Mon, 5 Apr 2021 21:39:53 -0700 Subject: [PATCH 1/2] fix division + add tests --- .../adaptive-expressions/src/builtinFunctions/divide.ts | 6 +++++- .../adaptive-expressions/tests/expressionParser.test.js | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/adaptive-expressions/src/builtinFunctions/divide.ts b/libraries/adaptive-expressions/src/builtinFunctions/divide.ts index 3bbbf164c4..0c2f95275b 100644 --- a/libraries/adaptive-expressions/src/builtinFunctions/divide.ts +++ b/libraries/adaptive-expressions/src/builtinFunctions/divide.ts @@ -26,7 +26,11 @@ export class Divide extends MultivariateNumericEvaluator { * @private */ private static func(args: any[]): number { - return Math.floor(Number(args[0]) / Number(args[1])); + let result: number = Number(args[0]) / Number(args[1]); + if (Number.isInteger(args[0]) && Number.isInteger(args[1])) { + return Math.floor(result); + } + return result; } /** diff --git a/libraries/adaptive-expressions/tests/expressionParser.test.js b/libraries/adaptive-expressions/tests/expressionParser.test.js index a7a49e3e39..d4907fda59 100644 --- a/libraries/adaptive-expressions/tests/expressionParser.test.js +++ b/libraries/adaptive-expressions/tests/expressionParser.test.js @@ -89,6 +89,7 @@ const testCases = [ ['1 *\r\n 2 + 3', 5], ['1 + 2 * 3', 7], ['4 / 2', 2], + ['11.2 / 2', 5.6], ['4 /\r\n 2', 2], ['1 + 3 / 2', 2], ['(1 + 3) / 2', 2], @@ -483,6 +484,7 @@ const testCases = [ ['div(mul(2, 5), 2)', 5], ['div(5, 2)', 2], ['div(5, 2, 2)', 1], + ['div(11.2, 2)', 5.6], ['exp(2,2)', 4.0], ['mod(5,2)', 1], ['rand(1, 2)', 1], From da58677957788424a03b1948d615efeb56c65ddb Mon Sep 17 00:00:00 2001 From: stefangomez Date: Tue, 6 Apr 2021 14:48:08 -0700 Subject: [PATCH 2/2] let -> const --- libraries/adaptive-expressions/src/builtinFunctions/divide.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/adaptive-expressions/src/builtinFunctions/divide.ts b/libraries/adaptive-expressions/src/builtinFunctions/divide.ts index 0c2f95275b..01a272c78f 100644 --- a/libraries/adaptive-expressions/src/builtinFunctions/divide.ts +++ b/libraries/adaptive-expressions/src/builtinFunctions/divide.ts @@ -26,7 +26,7 @@ export class Divide extends MultivariateNumericEvaluator { * @private */ private static func(args: any[]): number { - let result: number = Number(args[0]) / Number(args[1]); + const result: number = Number(args[0]) / Number(args[1]); if (Number.isInteger(args[0]) && Number.isInteger(args[1])) { return Math.floor(result); }