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

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

Closed
Zecc opened this Issue Apr 2, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@Zecc

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"]]

@josdejong josdejong added the bug label Apr 4, 2015

@josdejong

This comment has been minimized.

Owner

josdejong commented Apr 4, 2015

Thanks for reporting this bug, will fix it asap.

@josdejong

This comment has been minimized.

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...

@josdejong

This comment has been minimized.

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 :).

@josdejong josdejong closed this in 3150e21 Apr 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment