Skip to content

Instantly share code, notes, and snippets.

@jnthn
Created April 2, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnthn/a066fed40bfe50fb4e993bc7ff9e9422 to your computer and use it in GitHub Desktop.
Save jnthn/a066fed40bfe50fb4e993bc7ff9e9422 to your computer and use it in GitHub Desktop.
my $value = (1 - 0.00342) ** -0.5;
say $value;
say $value.Rat;
say extract-rat($value);
sub extract-rat(num $n) {
my $buffer = Buf.allocate(8);
$buffer.write-num64(0, $n);
my $vi = $buffer.read-uint64(0);
my $sign = $vi +> 63;
my $exponent = ($vi +> 52) +& 0b11111111111;
my $mantissa = $vi +& 0b1111111111111111111111111111111111111111111111111111;
(-1) ** $sign * (1 + $mantissa / 2 ** 52) * 2 ** ($exponent - 1023)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment