Skip to content

Instantly share code, notes, and snippets.

@TimToady
Created May 17, 2012 18:18
Show Gist options
  • Save TimToady/2720715 to your computer and use it in GitHub Desktop.
Save TimToady/2720715 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.exists($key) {
my $rep = $place - %seen{$key};
$s = substr($s,0,*-$rep) ~ substr($s, *-$rep).subst(/(.)/, -> $/ { $0 ~ "\x0305" }, :g);
last;
}
else {
%seen{$key} = $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