Negative base numbers: Difference between revisions
Content added Content deleted
(→{{header|Factor}}: simplify) |
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a module based version) |
||
Line 1,441: | Line 1,441: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
=== Explicit === |
|||
{{works with|Rakudo|2016.11}} |
{{works with|Rakudo|2016.11}} |
||
Raku provides built-in methods / routines base and parse-base to convert to and from bases 2 through 36. We'll just shadow the core routines with versions that accept negative bases. |
Raku provides built-in methods / routines base and parse-base to convert to and from bases 2 through 36. We'll just shadow the core routines with versions that accept negative bases. |
||
Line 1,515: | Line 1,516: | ||
41371457.268272761.&base(-36) = PERL6.ROCKS : 'PERL6.ROCKS'.&parse-base(-36) = 41371457.268272761 |
41371457.268272761.&base(-36) = PERL6.ROCKS : 'PERL6.ROCKS'.&parse-base(-36) = 41371457.268272761 |
||
'-21'.&parse-base(-10) = 19</pre> |
'-21'.&parse-base(-10) = 19</pre> |
||
=== Module === |
|||
{{works with|Rakudo|2020.02}} |
|||
Using the module [https://modules.raku.org/search/?q=Base%3A%3AAny Base::Any] from the Raku ecosystem. |
|||
Does everything the explicit version does but also handles a '''much''' larger range of negative bases. |
|||
Doing pretty much the same tests as the explicit version. |
|||
<lang perl6>use Base::Any; |
|||
for < 4 -4 0 -7 10 -2 146 -3 15 -10 -19 -10 107 -16 |
|||
227.65625 -16 2.375 -4 -1.3e2 -8 41371457.268272761 -36 |
|||
-145115966751439403/3241792 -1184 > -> $v, $r { |
|||
my $nbase = $v.&to-base($r, :precision(-5)); |
|||
printf "%21s.&to-base\(%5d\) = %-11s : %13s.&from-base\(%5d\) = %s\n", |
|||
+$v, $r, $nbase, "'$nbase'", $r, $nbase.&from-base($r); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> 4.&to-base( -4) = 130 : '130'.&from-base( -4) = 4 |
|||
0.&to-base( -7) = 0 : '0'.&from-base( -7) = 0 |
|||
10.&to-base( -2) = 11110 : '11110'.&from-base( -2) = 10 |
|||
146.&to-base( -3) = 21102 : '21102'.&from-base( -3) = 146 |
|||
15.&to-base( -10) = 195 : '195'.&from-base( -10) = 15 |
|||
-19.&to-base( -10) = 21 : '21'.&from-base( -10) = -19 |
|||
107.&to-base( -16) = 1AB : '1AB'.&from-base( -16) = 107 |
|||
227.65625.&to-base( -16) = 124.68 : '124.68'.&from-base( -16) = 227.65625 |
|||
2.375.&to-base( -4) = 3.32 : '3.32'.&from-base( -4) = 2.375 |
|||
-130.&to-base( -8) = 1616 : '1616'.&from-base( -8) = -130 |
|||
41371457.268272761.&to-base( -36) = PERL6.ROCKS : 'PERL6.ROCKS'.&from-base( -36) = 41371457.268272761 |
|||
-44764120200.01264825.&to-base(-1184) = Raku.FTW : 'Raku.FTW'.&from-base(-1184) = -44764120200.01264825</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |