Imaginary base numbers: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
→‎{{header|Perl 6}}: Add a Perl 6 example
Thundergnat (talk | contribs)
m →‎{{header|Perl 6}}: fix some spacing issues, minor rewording
Line 305: Line 305:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2017.01}}
{{works with|Rakudo|2017.01}}
These are generalized imaginary-base conversion routines. They only work for imaginary bases, not complex. (Any real coefficient of the radix must be zero.) Theoretically they could be made to work for any imaginary base; in practice, they are limited to bases from -6i to -2i and 2i to 6i since those bases will fit within standard base 36 digit representations. Bases -1i and 1i exist but require special caseing. Bases larger than 6i (or -6i) require digits outside of base 36 to express them, so aren't as standardized, and are implementation dependent and not supported here.
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 bases from -6i to -2i and 2i to 6i since those bases will fit within standard base 36 digit representations. Bases -1i and 1i exist but require special caseing 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 here.


<lang perl6>multi sub base ( Real $num, Int $radix where -37 < * < -1, :$precision = -5 ) {
<lang perl6>multi sub base ( Real $num, Int $radix where -37 < * < -1, :$precision = -5 ) {
Line 334: Line 334:


multi sub base (Numeric $value, Complex $base where *.re == 0 ) {
multi sub base (Numeric $value, Complex $base where *.re == 0 ) {
die "Base $base out of range" unless -6 <= $base.im <= -2 or 2 <= $base.im <= 6;
die "Base $base out of range" unless -6 <= $base.im <= -2 or 2 <= $base.im <= 6;
my ($re, $im) = $value.Complex.reals;
my ($re, $im) = $value.Complex.reals;
$re .= &base((-($base.im)²).Int);
$re .= &base((-($base.im)²).Int);
$im = ($im/($base.im)).&base((-($base.im)²).Int);
$im = ($im/($base.im)).&base((-($base.im)²).Int);
my ($re-wh, $re-fr, $im-wh, $im-fr);
my ($re-wh, $re-fr, $im-wh, $im-fr);
($re-wh, $re-fr) = $re.split: '.';
($re-wh, $re-fr) = $re.split: '.';
($im-wh, $im-fr) = $im.split: '.';
($im-wh, $im-fr) = $im.split: '.';
$_ //= '' for ($re-wh, $re-fr, $im-wh, $im-fr);
$_ //= '' for ($re-wh, $re-fr, $im-wh, $im-fr);


my @whole = do given $re-wh.chars cmp $im-wh.chars {
my @whole = do given $re-wh.chars cmp $im-wh.chars {
when Less { flat (flat $re-wh.flip.comb, '0' xx *) Z flat $im-wh.flip.comb }
when Less { flat (flat $re-wh.flip.comb, '0' xx *) Z flat $im-wh.flip.comb }
when More { flat $re-wh.flip.comb Z flat (flat $im-wh.flip.comb, '0' xx $re-wh.chars - $im-wh.chars) }
when More { flat $re-wh.flip.comb Z flat (flat $im-wh.flip.comb, '0' xx $re-wh.chars - $im-wh.chars) }
default { flat $re-wh.flip.comb Z flat $im-wh.flip.comb }
default { flat $re-wh.flip.comb Z flat $im-wh.flip.comb }
}
}
my @fraction = do given $re-fr.chars cmp $im-fr.chars {
my @fraction = do given $re-fr.chars cmp $im-fr.chars {
when More { flat (flat $im-fr.comb, '0' xx *) Z flat $re-fr.comb }
when More { flat (flat $im-fr.comb, '0' xx *) Z flat $re-fr.comb }
when Less { flat $im-fr.comb Z flat (flat $re-fr.comb, '0' xx $im-fr.chars - $re-fr.chars) }
when Less { flat $im-fr.comb Z flat (flat $re-fr.comb, '0' xx $im-fr.chars - $re-fr.chars) }
default { flat $im-fr.comb Z flat $re-fr.comb }
default { flat $im-fr.comb Z flat $re-fr.comb }
}
}
@whole.pop if +@whole and @whole.tail eq '0';
@whole.pop if +@whole and @whole.tail eq '0';
@fraction.pop while +@fraction and @fraction.tail eq '0';
@fraction.pop while +@fraction and @fraction.tail eq '0';
+@fraction ??
+@fraction ??
@whole.reverse.join ~ '.' ~ @fraction.join !!
@whole.reverse.join ~ '.' ~ @fraction.join !!
@whole.reverse.join
@whole.reverse.join
}
}