Numerical and alphabetical suffixes: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Handle input numbers with commas correctly, add a demo of mixing named and factorial suffixes)
m (→‎{{header|Perl 6}}: wording, whitespace and style tweaks)
Line 138: Line 138:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2018.09}}
{{works with|Rakudo|2018.09}}
Scientific notation, while supported in Perl 6, is limited to IEE-752 64bit accuracy so there is some rounding on values using it. It would certainly be possible to implement a custom high accuracy conversion routine but I can't really see the point.
Scientific notation, while supported in Perl 6, is limited to IEE-752 64bit accuracy so there is some rounding on values using it. It would certainly be possible to implement a custom high accuracy conversion routine but I can't really see the point since unfortunately, this routine is of limited use for practical everyday purposes. It focuses on handling excessively large and archaic units (googol, greatgross) and completely ignores or makes unusable (due to forcing case insensitivity) many common current ones: c(centi), m(milli), μ(micro). Ah well.

Unfortunately, this routine is almost useless for practical everyday use. It focuses on handling excessively large and archaic units (googol, greatgross) and completely ignores or makes unusable (due to forcing case insensitivity) many common current ones: c(centi), m(milli), μ(micro). Ah well.


<lang perl6><PAIRs 2 SCOres 20 DOZens 12 GRoss 144 GREATGRoss 1728
<lang perl6><PAIRs 2 SCOres 20 DOZens 12 GRoss 144 GREATGRoss 1728
GOOGOLs 1e100> ~~ m:g/ ((<.:Lu>+) <.:Ll>*)\s+(\S+) /;
GOOGOLs 1e100> ~~ m:g/ ((<.:Lu>+) <.:Ll>*) \s+ (\S+) /;


my %abr = |$/.map: {
my %abr = |$/.map: {
my $abbrv = $_[0].Str.fc;
my $abbrv = .[0].Str.fc;
my $mag = +$_[1];
my $mag = +.[1];
|map { $abbrv.substr( 0, $_ ) => $mag },
|map { $abbrv.substr( 0, $_ ) => $mag },
$_[0][0].Str.chars .. $abbrv.chars
.[0][0].Str.chars .. $abbrv.chars
}
}


my %suffix = flat %abr,
my %suffix = flat %abr,
(<K M G T P E Z Y X W V U>».fc Z=> (1000, * * 1000 … *)),
(<K M G T P E Z Y X W V U>».fc Z=> (1000, * * 1000 … *)),
(<Ki Mi Gi Ti Pi Ei Zi Yi Xi Wi Vi Ui>».fc Z=> (1024, * * 1024 … *));
(<Ki Mi Gi Ti Pi Ei Zi Yi Xi Wi Vi Ui>».fc Z=> (1024, * * 1024 … *));


my $reg = %suffix.keys.sort(-*.chars).join('|');
my $reg = %suffix.keys.join('|');


sub comma ($i is copy) {
sub comma ($i is copy) {
my $s = $i < 0 ?? '-' !! '';
my $s = $i < 0 ?? '-' !! '';
my $whole = $i.abs.floor;
my $whole = $i.abs.floor;
my $frac = $i.abs - $whole ?? '.' ~ $i.abs.split('.')[1] !! '';
my $frac = $i.abs - $whole ?? '.' ~ $i.abs.split('.')[1] !! '';
$s ~ $whole.flip.comb(3).join(',').flip ~ $frac
$s ~ $whole.flip.comb(3).join(',').flip ~ $frac
}
}
Line 167: Line 165:
sub units (Str $str) {
sub units (Str $str) {
$str.fc ~~ /^(.+?)(<alpha>*)('!'*)$/;
$str.fc ~~ /^(.+?)(<alpha>*)('!'*)$/;
my ($val, $unit, $fact) = $0, $1.Str.fc, $2;
my ($val, $unit, $fact) = $0, $1.Str.fc, $2.Str;
$val.=subst(',', '', :g);
$val.=subst(',', '', :g);
my @suf = $unit;
my @suf = $unit;
Line 175: Line 173:
}
}
my $ret = $val<>;
my $ret = $val<>;
if @suf[0] {
$ret = [*] $ret, |@suf.map: { %suffix{$_} } if @suf[0];
$ret = [*] $ret, |@suf.map: {%suffix{$_}}
$ret = [*] ($ret, * - $fact.chars …^ * < 2) if $fact.chars;
}
if $fact.chars {
$ret = [*] ($ret, * - $fact.chars …^ * < 2);
}
$ret
$ret
}
}


printf "%16s: %s\n", $_, comma .&units for
printf "%16s: %s\n", $_, comma .&units for <
<2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
1,567 +1.567k 0.1567e-2m
1,567 +1.567k 0.1567e-2m
25.123kK 25.123m 2.5123e-00002G
25.123kK 25.123m 2.5123e-00002G
25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
-.25123e-34Vikki 2e-77gooGols
-.25123e-34Vikki 2e-77gooGols
9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
.017k!!>;</lang>
.017k!!
>;</lang>
{{out}}
{{out}}
<pre> 2greatGRo: 3,456
<pre> 2greatGRo: 3,456