Skip to content

Instantly share code, notes, and snippets.

@waska14
Last active July 22, 2021 14:07
Show Gist options
  • Save waska14/4e57a0d89e277e503675f5c979a04cbd to your computer and use it in GitHub Desktop.
Save waska14/4e57a0d89e277e503675f5c979a04cbd to your computer and use it in GitHub Desktop.
Temp: order by rank in database
<?php
function prettify($str)
{
$length = strlen($str) - 2;
if ($length > 10) {
$str = sprintf('%s%s[%sx 0]%s%s', substr($str, 0, 3), '...', $length - 3, '...', substr($str, $length));
}
return $str;
}
foreach ([5, 10, 15, 33, 66, 16383] as $digits) {
$value = 1;
$lastValue = '';
$i = 0;
while (true) {
$value = rtrim(bcdiv($value, 2, $digits), '0');
if ($value === '0.') {
echo ((strlen($lastValue) - 2) . " digit after decimal point | iteration:" . ($i) . " | " . prettify($lastValue)) . PHP_EOL;
break;
}
$lastValue = $value;
$i++;
}
}
/*
5 digit after decimal point | iteration:16 | 0.00001
10 digit after decimal point | iteration:33 | 0.0000000001
15 digit after decimal point | iteration:49 | 0.0...[12x 0]...01
33 digit after decimal point | iteration:109 | 0.0...[30x 0]...01
66 digit after decimal point | iteration:219 | 0.0...[63x 0]...01
16383 digit after decimal point | iteration:54423 | 0.0...[16380x 0]...01
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment