3 ms·
The algorithms in question are off by a digit in no cases at all, they are correct for all inputs. If you actually start putting your pseudocode into real code
by klodolph 6y ago
The algorithms in question are off by a digit in no cases at all, they are correct for all inputs.
If you actually start putting your pseudocode into real code you’ll see how quickly it can go wrong, if you want to correctly convert all inputs. For example, once you get past 10^22, you can no longer do a simple division or multiplication to get the leading digit, because 10^22 cannot be represented as a double. For numbers 10^22 or below, you can just use ordinary division or multiplication and rely on the fact that this will be rounded correctly.
It’s a fun exercise to write an exact converter that works in the range -10^22..+10^22, though. It doesn’t require much code.