Imaginary base numbers: Difference between revisions

Added 11l
m (→‎{{header|Perl}}: further simplified use of 'reverse')
(Added 11l)
Line 310:
</tr>
</table>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F inv(c)
V denom = c.real * c.real + c.imag * c.imag
R Complex(c.real / denom, -c.imag / denom)
 
V QuaterImaginary_twoI = Complex(0, 2)
V QuaterImaginary_invTwoI = inv(QuaterImaginary_twoI)
 
T QuaterImaginary
String b2i
 
F (str)
I !re:‘[0123.]+’.match(str) | str.count(‘.’) > 1
assert(0B, ‘Invalid base 2i number’)
.b2i = str
 
F toComplex()
V pointPos = .b2i.findi(‘.’)
V posLen = I (pointPos < 0) {.b2i.len} E pointPos
V sum = Complex(0, 0)
V prod = Complex(1, 0)
L(j) 0 .< posLen
V k = Int(.b2i[posLen - 1 - j])
I k > 0
sum += prod * k
prod *= QuaterImaginary_twoI
I pointPos != -1
prod = QuaterImaginary_invTwoI
L(j) posLen + 1 .< .b2i.len
V k = Int(.b2i[j])
I k > 0
sum += prod * k
prod *= QuaterImaginary_invTwoI
R sum
 
F String()
R String(.b2i)
 
F toQuaterImaginary(c)
I c.real == 0.0 & c.imag == 0.0
R QuaterImaginary(‘0’)
 
V re = Int(c.real)
V im = Int(c.imag)
V fi = -1
V ss = ‘’
L re != 0
(re, V rem) = divmod(re, -4)
I rem < 0
rem += 4
re++
ss ‘’= String(rem)‘0’
I im != 0
V f = c.imag / 2
im = Int(ceil(f))
f = -4 * (f - im)
V index = 1
L im != 0
(im, V rem) = divmod(im, -4)
I rem < 0
rem += 4
im++
I index < ss.len
assert(0B)
E
ss ‘’= ‘0’String(rem)
index = index + 2
fi = Int(f)
ss = reversed(ss)
I fi != -1
ss ‘’= ‘.’String(fi)
ss = ss.ltrim(‘0’)
I ss[0] == ‘.’
ss = ‘0’ss
R QuaterImaginary(ss)
 
L(i) 1..16
V c1 = Complex(i, 0)
V qi = toQuaterImaginary(c1)
V c2 = qi.toComplex()
print(‘#8 -> #8 -> #8 ’.format(c1, qi, c2), end' ‘ ’)
 
c1 = -c1
qi = toQuaterImaginary(c1)
c2 = qi.toComplex()
print(‘#8 -> #8 -> #8’.format(c1, qi, c2))
print()
 
L(i) 1..16
V c1 = Complex(0, i)
V qi = toQuaterImaginary(c1)
V c2 = qi.toComplex()
print(‘#8 -> #8 -> #8 ’.format(c1, qi, c2), end' ‘ ’)
 
c1 = -c1
qi = toQuaterImaginary(c1)
c2 = qi.toComplex()
print(‘#8 -> #8 -> #8’.format(c1, qi, c2))
 
print(‘done’)</lang>
 
{{out}}
<pre>
1 -> 1 -> 1 -1 -> 103 -> -1
2 -> 2 -> 2 -2 -> 102 -> -2
3 -> 3 -> 3 -3 -> 101 -> -3
4 -> 10300 -> 4 -4 -> 100 -> -4
5 -> 10301 -> 5 -5 -> 203 -> -5
6 -> 10302 -> 6 -6 -> 202 -> -6
7 -> 10303 -> 7 -7 -> 201 -> -7
8 -> 10200 -> 8 -8 -> 200 -> -8
9 -> 10201 -> 9 -9 -> 303 -> -9
10 -> 10202 -> 10 -10 -> 302 -> -10
11 -> 10203 -> 11 -11 -> 301 -> -11
12 -> 10100 -> 12 -12 -> 300 -> -12
13 -> 10101 -> 13 -13 -> 1030003 -> -13
14 -> 10102 -> 14 -14 -> 1030002 -> -14
15 -> 10103 -> 15 -15 -> 1030001 -> -15
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
done
</pre>
 
=={{header|C}}==
1,463

edits