/**
* This method returns a binary string representation of
* the decimal number. Used for the doubleAndAdd() method.
*
* @see http://php.net/manual/en/function.decbin.php but for large numbers
*
* @param string
* @return string
*/
public static function decToBin($dec)
{
if (substr(strtolower($dec), 0, 2) == '0x') {
$dec = self::decodeHex(substr($dec, 2));
}
$bin = '';
while (Math::cmp($dec, '0') > 0) {
if (Math::mod($dec, 2) == '1') {
$bin .= '1';
} else {
$bin .= '0';
}
$prevDec = $dec;
$dec = Math::div($dec, 2);
//sanity check to avoid infinite loop
if (Math::cmp($prevDec, $dec) < 1) {
throw new \Exception('Math library has unexpected behavior, please report the following information to [email protected]. Math Engine is: ' . Math::getEngineName() . '. PHP Version is: ' . phpversion() . '.');
}
}
return $bin;
}