# math.js can't handle multiple recursive calls #313

Closed
opened this Issue Apr 2, 2015 · 3 comments

Projects
None yet
2 participants

### Zecc commented Apr 2, 2015

 Multiple recursive calls inside a function return wrong results (always the same value, it seems). An example: ``````fib(n) = (n <= 2) ? 1 : fib(n-2)+fib(n-1); [fib(1), fib(2), fib(3), fib(4), fib(5), fib(6), fib(7), fib(8), fib(9)] `````` Expected result: ``````[[1, 1, 2, 3, 5, 8, 13, 21, 34]] `````` Actual result: ``````[[1, 1, 2, 2, 3, 3, 4, 4, 5]] `````` Another example: ``````tohex(n) = (n < 10) ? string(n) : (n == 15) ? "f" : (n == 14) ? "e" : (n == 13) ? "d" : (n == 12) ? "c" : (n == 11) ? "b" : (n == 10) ? "a" : tohex(floor(n/16)) + tohex(mod(n,16)); [tohex(16), tohex(255), tohex(51966)] `````` Expected result: ``````[["10", "ff", "cafe"]] `````` Actual result: ``````[["11", "ff", "cccc"]] ``````

Owner

### josdejong commented Apr 4, 2015

 Thanks for reporting this bug, will fix it asap.
Owner

### josdejong commented Apr 8, 2015

 I know the cause of this and there is an easy solution (creating a new, linked scope in the function with every call), but this solution has terrible performance. I've an other idea in mind though which both solves the issue and will even improve performance (basically the compiled code must get closer to the bare metal than it is right now). Have to work this out...
Owner

### josdejong commented Apr 9, 2015

 Ok this is fixed now on the develop branch, and besides that the performance of parsed functions is improved :).