Skip to content

Instantly share code, notes, and snippets.

@TimToady
Created May 11, 2012 16:36
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 TimToady/2660818 to your computer and use it in GitHub Desktop.
Save TimToady/2660818 to your computer and use it in GitHub Desktop.
sub r2s (Rat $rat) {
my $s = $rat < 0 ?? '-' !! '';
my $r = $rat.abs;
my $i = $r.floor;
$r -= $i;
$s ~= $i;
if $r {
$s ~= '.';
my $place = 0;
my %seen;
while $r {
my $key = $r.perl;
if %seen{$key} {
$s ~= " (repeat {++$place - %seen{$key}})";
last;
}
else {
%seen{$r.perl} = ++$place;
$r *= 10;
$i = $r.floor;
$s ~= $i;
$r -= $i;
}
}
}
$s;
}
say r2s(241025348275725.3352);
say r2s(1/7000);
say r2s(2**64 / 1 + 1/97);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment