Write a function or program that outputs Quater-imaginary base displayed as binary digits. The number base is 2i, where i is the square root of -1. See Complex Number for more details on i. Each digit position can go from 0 to 3 (quaternary), as each real and imaginary part is -4 times as large as the previous real and imaginary part. The quaternary digits in binary are as follows: 0: 00
, 1: 01
, 2: 10
& 3: 11
.
Breakdown of digit positions:
re im 16 -8i -4 2i 1 -0.5i, etc.
4 0 1 0 3 0 0 (quaternary representation)
01 00 11 00 00 (binary representation)
The number 100110000
is 1x16 + 3x-4 = 16 + -12 = 4.
re im 16 -8i -4 2i 1 -0.5i, etc.
0 5 0 0 0 3 0 2 (quaternary representation)
00 00 00 11 00 .10 (binary representation)
The number 1100.1
is 3x2i + 2x-0.5i = 6i + -i = 5i.
Your code will take a pair of numbers, which could be integer or floating point, and will output the complex number as a string of binary digits. The first number will be real, the second input number will be the imaginary value. A binary point must only be printed if there are non-zero number positions below 1 (i.e. if any of the positions for -0.5i, -0.25, 0.125i, etc. have a non-zero digit). Leading and trailing zeros are not allowed, except for a single zero digit immediately before the binary point if there are no other digits. The output must not start with a binary point (*00.1
- wrong, 0.1
- right, *.1
- wrong, *0.10
- wrong). You may assume that all input numbers will have finite binary representations.
Test numbers:
re im output
0 0 0
1 0 1
2 0 10
3 0 11
4 0 100110000
-1 0 10011
-2 0 10010
-3 0 10001
0 1 100.1
0 2 100
0 3 1000.1
0 4 1000
0 -1 0.1
0 -2 1001100
0 -3 1001100.1
3 4 1011
4 3 100111000.1
6 -9 101110010.1
-6 9 10011100110.1
-9 -6 1110111
0.5 14.125 10011001101.001001
Note: The output of all integer values will end in .1
if the imaginary part is odd.
Standard code-golf.
0 → 00, 1 → 01, 2 → 10, 3 → 11
. \$\endgroup\$