From d33f18206cfbba652de4eaa071963b0f252acc65 Mon Sep 17 00:00:00 2001 From: Daniele Date: Wed, 21 Dec 2022 12:11:56 +0100 Subject: [PATCH 1/2] - Add negative number nrt - Add a test in testfunctions --- lib/src/defs.dart | 2 +- test/two-parameter-functions.dart | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/src/defs.dart b/lib/src/defs.dart index 19ecba6..2cac59f 100644 --- a/lib/src/defs.dart +++ b/lib/src/defs.dart @@ -3,7 +3,7 @@ part of function_tree; /// A mapping of string representations to two-parameter functions. final Map _twoParameterFunctionMap = { 'log': (num b, num x) => log(x) / log(b), - 'nrt': (num n, num x) => pow(x, 1 / n), + 'nrt': (num n, num x) => (x.isNegative ? -1:1)* pow(x.abs(), 1 / n), 'pow': (num x, num p) => pow(x, p) }; diff --git a/test/two-parameter-functions.dart b/test/two-parameter-functions.dart index 8013157..386c499 100644 --- a/test/two-parameter-functions.dart +++ b/test/two-parameter-functions.dart @@ -12,4 +12,7 @@ void main() { final h = 'nrt(3, 27)'.interpret(); print(h); + + final i = 'nrt(3, -3)'.interpret(); + print(i); } From 3115b695ae10d266bede0b101da27eea54d65f82 Mon Sep 17 00:00:00 2001 From: Daniele Date: Wed, 21 Dec 2022 12:24:56 +0100 Subject: [PATCH 2/2] - Fix nrt for non-negative number return negative number --- lib/src/defs.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/defs.dart b/lib/src/defs.dart index 2cac59f..32f127b 100644 --- a/lib/src/defs.dart +++ b/lib/src/defs.dart @@ -3,7 +3,7 @@ part of function_tree; /// A mapping of string representations to two-parameter functions. final Map _twoParameterFunctionMap = { 'log': (num b, num x) => log(x) / log(b), - 'nrt': (num n, num x) => (x.isNegative ? -1:1)* pow(x.abs(), 1 / n), + 'nrt': (num n, num x) => (x.isNegative && n.toInt().isOdd ? -1:1)* pow(n.toInt().isOdd ? x.abs() : x, 1 / n), 'pow': (num x, num p) => pow(x, p) };