Jump to content

Imaginary base numbers: Difference between revisions

m
→‎{{header|Perl 6}}: Expand on precision and rounding parameters
m (→‎{{header|Perl 6}}: Clarification)
m (→‎{{header|Perl 6}}: Expand on precision and rounding parameters)
Line 305:
=={{header|Perl 6}}==
{{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 since those bases will fit within standard base 36 digit representations. 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 here. Note that imaginary numbers are stored as floating point numbers in Perl 6 so some rounding error may creep in during calculations. The precision these conversion routines use is configurable; we are using 8 decimal places of precision here.
 
<lang perl6>multi sub base ( Real $num, Int $radix where -37 < * < -1, :$precision = -58 ) {
return '0' unless $num;
my $value = $num;
Line 375:
my $ibase = $v.&base($r);
printf "%20s.&base\(%2si\) = %-10s : %12s.&parse-base\(%2si\) = %s\n",
$v, $r.im, $ibase, "'$ibase'", $r.im, $ibase.&parse-base($r).round(1e-58);
}</lang>
{{out}}
Line 395:
5+5i.&base(-5i) = 10O5 : '10O5'.&parse-base(-5i) = 5+5i
5+5i.&base(-6i) = 5.U : '5.U'.&parse-base(-6i) = 5+5i
227.65625+10.859375i.&base( 4i) = 10234.5678 : '10234.5678'.&parse-base( 4i) = 227.65625+10.85937i859375i</pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.