I am trying to implement a fairly simple Decimal to Binary converter using the following recursive function:
function dectobin(d) {
if (0 >= d) { return 0; }
else if (1 == d) { return 1; }
else {
return 10 * dectobin(Math.floor(d / 2)) + (d % 2);
}
}
Now the problem is that when I test it with 70007, there seems to be some overflow of 1 at the last recursion when the last entry is pop off the stack. So once dectobin(35003) returns with 100010001011101, it is scaled by 10 to 1000100010111010, and a 1 is suppose to be added. Except, instead of adding 1, it adds a 2 so the answer becomes: 1000100010111012. Now I have checked my logic and math and found not mistake in that, so I have a feeling this is an internal structure of the language that is causing this error. So if anyone can help me out here and explain to me what's the issue that would be most gratifying. Thanks in advance.