Imaginary base numbers: Difference between revisions

→‎{{header|Raku}}: Add a module based version
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(→‎{{header|Raku}}: Add a module based version)
Line 2,847:
=={{header|Raku}}==
(formerly Perl 6)
=== Explicit ===
{{works with|Rakudo|2017.01}}
These are generalized imaginary-base conversion routines. They only work for imaginary bases, not complex. (Any real portion of the radix must be zero.) Theoretically they could be made to work for any imaginary base; in practice, they are limited to integer bases from -6i to -2i and 2i to 6i. Bases -1i and 1i exist but require special handling and are not supported. Bases larger than 6i (or -6i) require digits outside of base 36 to express them, so aren't as standardized, are implementation dependent and are not supported. Note that imaginary number coefficients are stored as floating point numbers in Raku so some rounding error may creep in during calculations. The precision these conversion routines use is configurable; we are using 6 <strike>decimal</strike>, um... radicimal(?) places of precision here.
Line 2,929 ⟶ 2,930:
227.65625+10.859375i.&base( 4i) = 10234.5678 : '10234.5678'.&parse-base( 4i) = 227.65625+10.859375i
31433.3487654321-2902.4480452675i.&base( 6i) = PERL6.ROCKS : 'PERL6.ROCKS'.&parse-base( 6i) = 31433.3487654321-2902.4480452675i</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 imaginary bases.
 
Doing pretty much the same tests as the explicit version.
 
<lang perl6>use Base::Any;
 
# TESTING
for 0, 2i, 1, 2i, 5, 2i, -13, 2i, 9i, 2i, -3i, 2i, 7.75-7.5i, 2i, .25, 2i, # base 2i tests
5+5i, 2i, 5+5i, 3i, 5+5i, 4i, 5+5i, 5i, 5+5i, 6i, # same value, positive imaginary bases
5+5i, -2i, 5+5i, -3i, 5+5i, -4i, 5+5i, -5i, 5+5i, -6i, # same value, negative imaginary bases
227.65625+10.859375i, 4i, # larger test value
31433.3487654321-2902.4480452675i, 6i, # heh
-3544.29+26541.468i, -10i
-> $v, $r {
my $ibase = $v.&to-base($r, :precision(-6));
printf "%33s.&to-base\(%3si\) = %-11s : %13s.&from-base\(%3si\) = %s\n",
$v, $r.im, $ibase, "'$ibase'", $r.im, $ibase.&from-base($r).round(1e-10).narrow;
}</lang>
{{out}}
<pre> 0.&to-base( 2i) = 0 : '0'.&from-base( 2i) = 0
1.&to-base( 2i) = 1 : '1'.&from-base( 2i) = 1
5.&to-base( 2i) = 10301 : '10301'.&from-base( 2i) = 5
-13.&to-base( 2i) = 1030003 : '1030003'.&from-base( 2i) = -13
0+9i.&to-base( 2i) = 103010.2 : '103010.2'.&from-base( 2i) = 0+9i
-0-3i.&to-base( 2i) = 1030.2 : '1030.2'.&from-base( 2i) = 0-3i
7.75-7.5i.&to-base( 2i) = 11210.31 : '11210.31'.&from-base( 2i) = 7.75-7.5i
0.25.&to-base( 2i) = 1.03 : '1.03'.&from-base( 2i) = 0.25
5+5i.&to-base( 2i) = 10331.2 : '10331.2'.&from-base( 2i) = 5+5i
5+5i.&to-base( 3i) = 25.3 : '25.3'.&from-base( 3i) = 5+5i
5+5i.&to-base( 4i) = 25.C : '25.C'.&from-base( 4i) = 5+5i
5+5i.&to-base( 5i) = 15 : '15'.&from-base( 5i) = 5+5i
5+5i.&to-base( 6i) = 15.6 : '15.6'.&from-base( 6i) = 5+5i
5+5i.&to-base( -2i) = 11321.2 : '11321.2'.&from-base( -2i) = 5+5i
5+5i.&to-base( -3i) = 1085.6 : '1085.6'.&from-base( -3i) = 5+5i
5+5i.&to-base( -4i) = 10F5.4 : '10F5.4'.&from-base( -4i) = 5+5i
5+5i.&to-base( -5i) = 10O5 : '10O5'.&from-base( -5i) = 5+5i
5+5i.&to-base( -6i) = 5.U : '5.U'.&from-base( -6i) = 5+5i
227.65625+10.859375i.&to-base( 4i) = 10234.5678 : '10234.5678'.&from-base( 4i) = 227.65625+10.859375i
31433.3487654321-2902.4480452675i.&to-base( 6i) = PERL6.ROCKS : 'PERL6.ROCKS'.&from-base( 6i) = 31433.3487654321-2902.4480452675i
-3544.29+26541.468i.&to-base(-10i) = Raku.FTW : 'Raku.FTW'.&from-base(-10i) = -3544.29+26541.468i</pre>
 
=={{header|Sidef}}==
10,327

edits