From b5feba02d1d69a06c74742762cc44a6f3d5b7ec8 Mon Sep 17 00:00:00 2001 From: Stefan Gomez Date: Tue, 6 Apr 2021 21:25:17 -0700 Subject: [PATCH] Fix adaptive expressions divide function (#3528) * fix division + add tests * let -> const Co-authored-by: Steven Gum <14935595+stevengum@users.noreply.github.com> Co-authored-by: Hongyang Du (hond) --- .../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..01a272c78f 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])); + const 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],