Imaginary base numbers: Difference between revisions
Content added Content deleted
Line 707: | Line 707: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{incorrect|D|The outputs for the imaginary portions are suspect. The odd numbers are not round tripping correctly.}} |
|||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
<lang D>import std.algorithm; |
<lang D>import std.algorithm; |
||
Line 808: | Line 807: | ||
foreach (j; posLen+1..b2i.length) { |
foreach (j; posLen+1..b2i.length) { |
||
auto k = (b2i[j] - '0').to!double; |
auto k = (b2i[j] - '0').to!double; |
||
if (k > 0.0) { |
|||
sum += prod * k; |
|||
} |
|||
prod *= invTwoI; |
prod *= invTwoI; |
||
} |
} |
||
Line 845: | Line 847: | ||
qi = c1.toQuaterImaginary(); |
qi = c1.toQuaterImaginary(); |
||
c2 = cast(Complex!double) qi; |
c2 = cast(Complex!double) qi; |
||
writefln("%4s -> %8s -> %4s |
writefln("%4s -> %8s -> %4s", c1.re, qi, c2.re); |
||
} |
} |
||
writeln; |
writeln; |
||
Line 852: | Line 854: | ||
auto qi = c1.toQuaterImaginary; |
auto qi = c1.toQuaterImaginary; |
||
auto c2 = qi.to!(Complex!double); |
auto c2 = qi.to!(Complex!double); |
||
writef("% |
writef("%4si -> %8s -> %4si ", c1.im, qi, c2.im); |
||
c1 = -c1; |
c1 = -c1; |
||
qi = c1.toQuaterImaginary(); |
qi = c1.toQuaterImaginary(); |
||
c2 = cast(Complex!double) qi; |
c2 = cast(Complex!double) qi; |
||
writefln("% |
writefln("%4si -> %8s -> %4si", c1.im, qi, c2.im); |
||
} |
} |
||
}</lang> |
}</lang> |
||
Line 877: | Line 879: | ||
16 -> 10000 -> 16 -16 -> 1030000 -> -16 |
16 -> 10000 -> 16 -16 -> 1030000 -> -16 |
||
1i -> 10.2 -> 1i -1i -> 0.2 -> -1i |
|||
2i -> 10.0 -> 2i -2i -> 1030.0 -> -2i |
|||
3i -> 20.2 -> 3i -3i -> 1030.2 -> -3i |
|||
4i -> 20.0 -> 4i -4i -> 1020.0 -> -4i |
|||
5i -> 30.2 -> 5i -5i -> 1020.2 -> -5i |
|||
6i -> 30.0 -> 6i -6i -> 1010.0 -> -6i |
|||
7i -> 103000.2 -> 7i -7i -> 1010.2 -> -7i |
|||
8i -> 103000.0 -> 8i -8i -> 1000.0 -> -8i |
|||
9i -> 103010.2 -> 9i -9i -> 1000.2 -> -9i |
|||
10i -> 103010.0 -> 10i -10i -> 2030.0 -> -10i |
|||
11i -> 103020.2 -> 11i -11i -> 2030.2 -> -11i |
|||
12i -> 103020.0 -> 12i -12i -> 2020.0 -> -12i |
|||
13i -> 103030.2 -> 13i -13i -> 2020.2 -> -13i |
|||
14i -> 103030.0 -> 14i -14i -> 2010.0 -> -14i |
|||
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i |
|||
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |