Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# seyfer/negabase.php

Forked from CMCDragonkai/negabase.php
Created Mar 9, 2017
PHP: Negative Base Conversion from Base 10 Decimal
 (b*q + r = a) where // a => numerator, // b => denominator, // q => quotient, // r => remainder // If we get a negative remainder, we can just increment the quotient multiplying the denominator // giving us a number greater than the numerator // Subtracting this number from the numerator gives us a positive remainder // For example: (-5 / -2) => (2 r:-1) => (3 r:1) because [3 * -2 = -6 -> -6 + 1 -5 -> r:1] if (\$remainder < 0) { \$quotient += 1; \$remainder += (-1 * \$base); } // repeat the division using the new quotient \$decimal = \$quotient; // push the current number onto the stack array_unshift(\$digits, \$remainder); } return \$digits; } \$number = 42; \$base = -2; \$negative_base_number = negative_base(\$number, \$base);
to join this conversation on GitHub. Already have an account? Sign in to comment