You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conversions/OctalToDecimal: decimalToOctal uses float division (/= 8) so it emits spurious leading zeros / over-iterates #195
Conversions/OctalToDecimal.php::decimalToOctal uses floating-point division for the quotient update, so the loop produces wrong output (spurious leading zeros and a near-infinite number of iterations) for essentially every input.
In PHP, / returns a float whenever the operands are not evenly divisible, while % casts its operands to int. So once $decimalNumber drops below 8 the quotient becomes a fraction that stays > 0 for a very long time, and each extra iteration prepends (int)fraction % 8 == 0, i.e. a spurious '0'.
Trace for decimalToOctal(8) (correct answer: "10"):
0.125 > 0 is still true → (int)0.125 % 8 = 0 → "010"; 0.125 / 8 = 0.015625
… keeps prepending '0' until the float underflows to 0
So the result is "0…010" with a long run of leading zeros (and hundreds of wasted iterations), not "10". Every value that isn't reduced to exactly 0 by integer steps is affected.
The quotient becomes fractional, the while ($decimalNumber > 0) loop over-runs, and the output gains spurious leading zeros (e.g. decimalToOctal(8) does not return "10").
Suggested fix
Use integer division for the quotient:
$decimalNumber = intdiv((int)$decimalNumber, 8);
(or $decimalNumber = (int)($decimalNumber / 8);). A test asserting decimalToOctal(8) === "10" and decimalToOctal(64) === "100" would guard this.
Activity
esacsl29-gif commentedon Oct 21, 2026
$output = shell_exec('ls -lart');
";echo "