Imaginary base numbers: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(15 intermediate revisions by 9 users not shown)
Line 310:
</tr>
</table>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F inv(c)
V denom = c.real * c.real + c.imag * c.imag
R Complex(c.real / denom, -c.imag / denom)
 
T QuaterImaginary
:twoI = Complex(0, 2)
:invTwoI = inv(.:twoI)
 
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 *= .:twoI
I pointPos != -1
prod = .:invTwoI
L(j) posLen + 1 .< .b2i.len
V k = Int(.b2i[j])
I k > 0
sum += prod * k
prod *= .: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’)</syntaxhighlight>
 
{{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}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <string.h>
Line 581 ⟶ 722:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>( 1 + 0i) -> 1 -> ( 1 + 0i) ( -1 + -0i) -> 103 -> ( -1 + 0i)
Line 618 ⟶ 759:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Text;
Line 794 ⟶ 935:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 832 ⟶ 973:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <complex>
#include <iomanip>
Line 976 ⟶ 1,117:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> (1,0) -> 1 -> (1,0) (-1,-0) -> 103 -> (-1,0)
Line 1,014 ⟶ 1,155:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.array;
import std.complex;
Line 1,166 ⟶ 1,307:
writefln("%4si -> %8s -> %4si", c1.im, qi, c2.im);
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 1,201 ⟶ 1,342:
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i</pre>
 
=={{header|FreeBASIC}}==
{{trans|Modula-2}}
<syntaxhighlight lang="vbnet">#define ceil(x) (-((-x*2.0-0.5) Shr 1))
 
Type Complex
real As Double
imag As Double
End Type
 
Type QuaterImaginary
b2i As String
End Type
 
Dim Shared As Complex c1, c2
 
Function StrReverse(Byval txt As String) As String
Dim result As String
For i As Integer = Len(txt) To 1 Step -1
result &= Mid(txt, i, 1)
Next i
Return result
End Function
 
Function ToChar(n As Integer) As String
Return Chr(n + Asc("0"))
End Function
 
Function ComplexMul(lhs As Complex, rhs As Complex) As Complex
Dim As Complex result
result.real = rhs.real * lhs.real - rhs.imag * lhs.imag
result.imag = rhs.real * lhs.imag + rhs.imag * lhs.real
Return result
End Function
 
Function ComplexMulR(lhs As Complex, rhs As Double) As Complex
Dim As Complex result
result.real = lhs.real * rhs
result.imag = lhs.imag * rhs
Return result
End Function
 
Function ComplexInv(c As Complex) As Complex
Dim As Double denom
Dim As Complex result
denom = c.real * c.real + c.imag * c.imag
result.real = c.real / denom
result.imag = -c.imag / denom
Return result
End Function
 
Function ComplexDiv(lhs As Complex, rhs As Complex) As Complex
Return ComplexMul(lhs, ComplexInv(rhs))
End Function
 
Function ComplexNeg(c As Complex) As Complex
Dim As Complex result
result.real = -c.real
result.imag = -c.imag
Return result
End Function
 
Function ComplexSum(lhs As Complex, rhs As Complex) As Complex
Dim As Complex result
result.real = lhs.real + rhs.real
result.imag = lhs.imag + rhs.imag
Return result
End Function
 
Function ToQuaterImaginary(c As Complex) As QuaterImaginary
Dim As Integer re, im, fi, rem_, index
Dim As Double f
Dim As Complex t
Dim As QuaterImaginary result
Dim As String sb
re = Int(c.real)
im = Int(c.imag)
fi = -1
While re <> 0
rem_ = (re Mod -4)
re = re \ (-4)
If rem_ < 0 Then
rem_ = 4 + rem_
re += 1
End If
sb &= ToChar(rem_) & "0"
Wend
If im <> 0 Then
t = ComplexDiv(Type<Complex>(0.0, c.imag), Type<Complex>(0.0, 2.0))
f = t.real
im = Ceil(f)
f = -4.0 * (f - Cdbl(im))
index = 1
While im <> 0
rem_ = im Mod -4
im \= -4
If rem_ < 0 Then
rem_ = 4 + rem_
im += 1
End If
If index < Len(sb) Then
Mid(sb, index + 1, 1) = ToChar(rem_)
Else
sb &= "0" & ToChar(rem_)
End If
index += 2
Wend
fi = Int(f)
End If
sb = StrReverse(sb)
If fi <> -1 Then sb &= "." & ToChar(fi)
sb = Ltrim(sb, "0")
If Left(sb, 1) = "." Then sb = "0" & sb
result.b2i = sb
Return result
End Function
 
Function ToComplex(qi As QuaterImaginary) As Complex
Dim As Integer j, pointPos, posLen, b2iLen
Dim As Double k
Dim As Complex sum, prod
pointPos = Instr(qi.b2i, ".")
posLen = Iif(pointPos = 0, Len(qi.b2i), pointPos - 1)
sum.real = 0.0
sum.imag = 0.0
prod.real = 1.0
prod.imag = 0.0
For j = 0 To posLen - 1
k = Val(Mid(qi.b2i, posLen - j, 1))
If k > 0.0 Then sum = ComplexSum(sum, ComplexMulR(prod, k))
prod = ComplexMul(prod, Type<Complex>(0.0, 2.0))
Next
If pointPos <> 0 Then
prod = ComplexInv(Type<Complex>(0.0, 2.0))
b2iLen = Len(qi.b2i)
For j = posLen + 1 To b2iLen - 1
k = Val(Mid(qi.b2i, j + 1, 1))
If k > 0.0 Then sum = ComplexSum(sum, ComplexMulR(prod, k))
prod = ComplexMul(prod, ComplexInv(Type<Complex>(0.0, 2.0)))
Next
End If
Return sum
End Function
 
Dim As QuaterImaginary qi
Dim As Integer i
For i = 1 To 16
c1.real = Cdbl(i)
c1.imag = 0.0
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.real; "i -> "; qi.b2i; " -> "; c2.real; "i";
c1 = ComplexNeg(c1)
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.real; "i -> "; qi.b2i; " -> "; c2.real; "i"
Next
Print
For i = 1 To 16
c1.real = 0.0
c1.imag = Cdbl(i)
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.imag; "i -> "; qi.b2i; " -> "; c2.imag; "i";
c1 = ComplexNeg(c1)
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.imag; "i -> "; qi.b2i; " -> "; c2.imag; "i"
Next
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Modula-2 entry.</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
... though a bit shorter as Go has support for complex numbers built into the language.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,351 ⟶ 1,665:
fmt.Printf("%3.0fi -> %8s -> %3.0fi\n", imag(c1), qi, imag(c2))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,391 ⟶ 1,705:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.Char (chr, digitToInt, intToDigit, isDigit, ord)
import Data.Complex (Complex (..), imagPart, realPart)
import Data.List (elemIndexdelete, deleteelemIndex)
import Data.Maybe (fromMaybe)
 
base :: Complex Float
base = 0 :+ 2
 
quotRemPositive :: Int -> Int -> (Int, Int)
quotRemPositive a b
Line 1,406 ⟶ 1,719:
where
(q, r) = quotRem a b
 
digitToIntQI :: Char -> Int
digitToIntQI c
| isDigit c = digitToInt c
| otherwise = ord c - ord 'a' + 10
 
shiftRight :: String -> String
shiftRight n
| l == '0' = h
| otherwise = h ++<> "('."' ++: [l])
where
(l, h) = (last n, init n)
 
intToDigitQI :: Int -> Char
intToDigitQI i
| i `elem` [0 .. 9] = intToDigit i
| otherwise = chr (i - 10 + ord 'a')
 
fromQItoComplex :: String -> Complex Float -> Complex Float
fromQItoComplex num b =
let dot = fromMaybe (length num) (elemIndex '.' num)
in fst $
foldl
( \(a, indx) x ->
( a + fromIntegral (digitToIntQI x) * (b ^^ (dot - indx)), indx + 1))
* (0,b 1^^ (dot - indx)),
(delete '.' num) indx + 1
)
)
(0, 1)
(delete '.' num)
 
euclidEr :: Int -> Int -> [Int] -> [Int]
euclidEr a b l
| a == 0 = l
| otherwise =
let (q, r) = quotRemPositive a b
in euclidEr q b (0 : r : l)
 
fromIntToQI :: Int -> [Int]
fromIntToQI 0 = [0]
fromIntToQI x = tail (euclidEr x (floor $ realPart (base ^^ 2)) [])
tail
( euclidEr
x
(floor $ realPart (base ^^ 2))
[]
)
 
getCuid :: Complex Int -> Int
getCuid c = imagPart c * floor (imagPart (-base))
 
qizip :: Complex Int -> [Int]
qizip c =
let (r, i) =
let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c))
in let m = min (length r)fromIntToQI (lengthrealPart ic) <> [0],
in take (length r - m)fromIntToQI r(getCuid ++c)
take (length i - m) i ++
in let m = reversemin (zipWith (+) (take m (reverselength r)) (take m (reverselength i)))
in take (length r - m) r
<> take (length i - m) i
<> reverse
( zipWith
(+)
(take m (reverse r))
(take m (reverse i))
)
 
fromComplexToQI :: Complex Int -> String
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip
 
main :: IO ()
main =
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang>
putStrLn (fromComplexToQI (35 :+ 23))
>> print (fromQItoComplex "10.2" base)</syntaxhighlight>
{{out}}
<pre>"121003.2"
0.0 :+ 1.0</pre>
</pre>
With base = 8i (you may choose any base):
<pre>3z.8
"3z.8"
0.0 :+ 7.75</pre>
 
=={{header|J}}==
 
Implementation:
 
<syntaxhighlight lang="j">
ibdec=: {{
0j2 ibdec y
:
digits=. 0,".,~&'36b'@> tolower y -.'. '
(x #. digits) % x^#(}.~ 1+i.&'.')y-.' '
}}"1
 
ibenc=: {{
0j2 ibenc y
:
if.0=y do.,'0' return.end.
sq=.*:x assert. 17 > sq
step=. }.,~(1,|sq) +^:(0>{:@]) (0,sq) #: {.
seq=. step^:(0~:{.)^:_"0
're im0'=.+.y
'im imf'=.(sign,1)*(0,|x)#:im0*sign=.*im0
frac=. ,hfd (imf*|x)-.0 if.#frac do.frac=.'.',frac end.
frac,~(}.~0 i.~_1}.'0'=]) }:,hfd|:0 1|."0 1 seq re,im
}}"0
</syntaxhighlight>
 
This ibdec can decode numbers from complex bases up to 0j6, but this ibenc can only represent digits in complex bases up to 0j4.
 
Examples:
 
<syntaxhighlight lang="j">
(ibenc i:16),.' ',.ibenc j.i:16
1030000 2000
1030001 2010.2
1030002 2010
1030003 2020.2
300 2020
301 2030.2
302 2030
303 1000.2
200 1000
201 1010.2
202 1010
203 1020.2
100 1020
101 1030.2
102 1030
103 0.2
0 0
1 0.2
2 10
3 10.2
10300 20
10301 20.2
10302 30
10303 30.2
10200 103000
10201 103000.2
10202 103010
10203 103010.2
10100 103020
10101 103020.2
10102 103030
10103 103030.2
10000 102000
(ibdec ibenc i:16),:ibdec ibenc j.i:16
_16 _15 _14 _13 _12 _11 _10 _9 _8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0j_16 0j_15 0j_14 0j_13 0j_12 0j_11 0j_10 0j_9 0j_8 0j_7 0j_6 0j_5 0j_4 0j_3 0j_2 0j_1 0 0j_1 0j2 0j1 0j4 0j3 0j6 0j5 0j8 0j7 0j10 0j9 0j12 0j11 0j14 0j13 0j16
0j4 ibenc 42
10e0a
0j4 ibdec 0j4 ibenc 42
42
</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class ImaginaryBaseNumber {
private static class Complex {
private Double real, imag;
Line 1,649 ⟶ 2,054:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 1,687 ⟶ 2,092:
=={{header|Julia}}==
{{trans|C#}}
<langsyntaxhighlight lang="julia">import Base.show, Base.parse, Base.+, Base.-, Base.*, Base./, Base.^
 
function inbase4(charvec::Vector)
Line 1,855 ⟶ 2,260:
 
testquim()
</langsyntaxhighlight>{{output}}<pre>
1 -> 1 -> 1 -1 -> 103 -> -1
2 -> 2 -> 2 -2 -> 102 -> -2
Line 1,895 ⟶ 2,300:
 
As the JDK lacks a complex number class, I've included a very basic one in the program.
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import kotlin.math.ceil
Line 2,040 ⟶ 2,445:
println(fmt.format(c1, qi, c2))
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,081 ⟶ 2,486:
=={{header|Modula-2}}==
{{trans|C#}}
<langsyntaxhighlight lang="modula2">MODULE ImaginaryBase;
FROM FormatString IMPORT FormatString;
FROM RealMath IMPORT round;
Line 2,417 ⟶ 2,822:
 
ReadChar
END ImaginaryBase.</langsyntaxhighlight>
{{out}}
<pre>1 -> 1 -> 1 -1 -> 103 -> -1
Line 2,452 ⟶ 2,857:
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
This is a fairly faithful translation of the Kotlin program except that we had not to define a Complex type as Nim provides the module “complex” in its standard library. We had only to define a function “toString” for the “Complex[float]” type, function to use in place of “$” in order to get a more pleasant output.
<syntaxhighlight lang="nim">import algorithm, complex, math, strformat, strutils
 
const
TwoI = complex(0.0, 2.0)
InvTwoI = inv(TwoI)
 
type QuaterImaginery = object
b2i: string
 
# Conversions between digit character and digit value.
template digitChar(n: range[0..9]): range['0'..'9'] = chr(n + ord('0'))
template digitValue(c: range['0'..'9']): range[0..9] = ord(c) - ord('0')
 
 
####################################################################################################
# Quater imaginary functions.
 
func initQuaterImaginary(s: string): QuaterImaginery =
## Create a Quater imaginary number.
if s.len == 0 or not s.allCharsInSet({'0'..'3', '.'}) or s.count('.') > 1:
raise newException(ValueError, "invalid base 2i number.")
result = QuaterImaginery(b2i: s)
 
#---------------------------------------------------------------------------------------------------
 
func toComplex(q: QuaterImaginery): Complex[float] =
## Convert a Quater imaginary number to a complex.
 
let pointPos = q.b2i.find('.')
let posLen = if pointPos != -1: pointPos else: q.b2i.len
var prod = complex(1.0)
 
for j in 0..<posLen:
let k = float(q.b2i[posLen - 1 - j].digitValue)
if k > 0: result += prod * k
prod *= TwoI
 
if pointPos != -1:
prod = InvTwoI
for j in (posLen + 1)..q.b2i.high:
let k = float(q.b2i[j].digitValue)
if k > 0: result += prod * k
prod *= InvTwoI
 
#---------------------------------------------------------------------------------------------------
 
func `$`(q: QuaterImaginery): string =
## Convert a Quater imaginary number to a string.
q.b2i
 
 
####################################################################################################
# Supplementary functions for complex numbers.
 
func toQuaterImaginary(c: Complex): QuaterImaginery =
## Convert a complex number to a Quater imaginary number.
 
if c.re == 0 and c.im == 0: return initQuaterImaginary("0")
 
var re = c.re.toInt
var im = c.im.toInt
var fi = -1
 
while re != 0:
var rem = re mod -4
re = re div -4
if rem < 0:
inc rem, 4
inc re
result.b2i.add rem.digitChar
result.b2i.add '0'
 
if im != 0:
var f = (complex(0.0, c.im) / TwoI).re
im = f.ceil.toInt
f = -4 * (f - im.toFloat)
var index = 1
while im != 0:
var rem = im mod -4
im = im div -4
if rem < 0:
inc rem, 4
inc im
if index < result.b2i.len:
result.b2i[index] = rem.digitChar
else:
result.b2i.add '0'
result.b2i.add rem.digitChar
inc index, 2
fi = f.toInt
 
result.b2i.reverse()
if fi != -1: result.b2i.add "." & $fi
result.b2i = result.b2i.strip(leading = true, trailing = false, {'0'})
if result.b2i.startsWith('.'): result.b2i = '0' & result.b2i
 
#---------------------------------------------------------------------------------------------------
 
func toString(c: Complex[float]): string =
## Convert a complex number to a string.
## This function is used in place of `$`.
 
let real = if c.re.classify == fcNegZero: 0.0 else: c.re
let imag = if c.im.classify == fcNegZero: 0.0 else: c.im
result = if imag >= 0: fmt"{real} + {imag}i" else: fmt"{real} - {-imag}i"
result = result.replace(".0 ", " ").replace(".0i", "i").replace(" + 0i", "")
if result.startsWith("0 + "): result = result[4..^1]
if result.startsWith("0 - "): result = '-' & result[4..^1]
 
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
when isMainModule:
 
for i in 1..16:
var c1 = complex(i.toFloat)
var qi = c1.toQuaterImaginary
var c2 = qi.toComplex
stdout.write fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s} "
c1 = -c1
qi = c1.toQuaterImaginary
c2 = qi.toComplex
echo fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s}"
 
echo ""
 
for i in 1..16:
var c1 = complex(0.0, i.toFloat)
var qi = c1.toQuaterImaginary
var c2 = qi.toComplex
stdout.write fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s} "
c1 = -c1
qi = c1.toQuaterImaginary
c2 = qi.toComplex
echo fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s}"</syntaxhighlight>
 
{{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</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,505 ⟶ 3,084:
$_ //= '' for $re_fr, $im_fr;
 
my $whole = join '', reverse split '', zip join('',scalar reverse split '', ($re_wh), join('',scalar reverse split '', ($im_wh);
my $fraction = zip $im_fr, $re_fr;
$fraction eq 0 ? "$whole" : "$whole.$fraction"
Line 2,537 ⟶ 3,116:
say '';
say 'base( 6i): 31432.6219135802-2898.5266203704*i => ' .
base_c(31432.6219135802-2898.5266203704*i, 0+6*i, -3);</langsyntaxhighlight>
{{out}}
<pre>base( 2i): 0 => 0 => 0
Line 2,562 ⟶ 3,141:
=={{header|Phix}}==
{{trans|Sidef}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include complex.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">include</span> <span style="color: #004080;">complex</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
function base2(atom num, integer radix, precision = -8)
if radix<-36 or radix>-2 then throw("radix out of range (-2..-36)") end if
<span style="color: #008080;">function</span> <span style="color: #000000;">base2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">precision</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
sequence result
<span style="color: #008080;">if</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;"><-</span><span style="color: #000000;">36</span> <span style="color: #008080;">or</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;">>-</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #008080;">throw</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"radix out of range (-2..-36)"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if num=0 then
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span>
result = {"0",""}
<span style="color: #008080;">if</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">}</span>
integer place = 0
<span style="color: #008080;">else</span>
result = ""
<span style="color: #004080;">integer</span> <span style="color: #000000;">place</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
atom v = num
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
atom upper_bound = 1/(1-radix),
<span style="color: #004080;">atom</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">num</span>
lower_bound = radix*upper_bound
<span style="color: #004080;">atom</span> <span style="color: #000000;">upper_bound</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">),</span>
while not(lower_bound <= v) or not(v < upper_bound) do
<span style="color: #000000;">lower_bound</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;">*</span><span style="color: #000000;">upper_bound</span>
place += 1
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lower_bound</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #008080;">not</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">upper_bound</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
v = num/power(radix,place)
<span style="color: #000000;">place</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end while
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">,</span><span style="color: #000000;">place</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
while (v or place > 0) and (place > precision) do
integer digit = floor(radix*v - lower_bound)
<span style="color: #008080;">while</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">v</span> <span style="color: #008080;">or</span> <span style="color: #000000;">place</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">place</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">precision</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
v = (radix*v - digit)
<span style="color: #004080;">integer</span> <span style="color: #000000;">digit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">*</span><span style="color: #000000;">v</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">lower_bound</span><span style="color: #0000FF;">)</span>
if place=0 and not find('.',result) then result &= '.' end if
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">*</span><span style="color: #000000;">v</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">)</span>
result &= digit+iff(digit>9?'a'-10:'0')
<span style="color: #008080;">if</span> <span style="color: #000000;">place</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'.'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
place -= 1
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">+</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">></span><span style="color: #000000;">9</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #000000;">place</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
integer dot = find('.',result)
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
if dot then
<span style="color: #004080;">integer</span> <span style="color: #000000;">dot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">result</span><span style="color: #0000FF;">)</span>
result = trim_tail(result,'0')
<span style="color: #008080;">if</span> <span style="color: #000000;">dot</span> <span style="color: #008080;">then</span>
result = {result[1..dot-1],result[dot+1..$]}
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
else
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]}</span>
result = {result,""}
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">}</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return result
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function zip(string a, string b)
integer ld = length(a)-length(b)
<span style="color: #008080;">function</span> <span style="color: #000000;">zip</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
if ld!=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">ld</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
if ld>0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">ld</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
b &= repeat('0',ld)
<span style="color: #008080;">if</span> <span style="color: #000000;">ld</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">b</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ld</span><span style="color: #0000FF;">)</span>
a &= repeat('0',abs(ld))
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ld</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
string res = ""
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=1 to length(a) do
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
res &= a[i]&b[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
res = trim_tail(res,'0')
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if res="" then res = "0" end if
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
return res
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function base(complexn num, integer radix, precision = -8)
<span style="color: #008080;">function</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">complexn</span> <span style="color: #000000;">num</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">precision</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
integer absrad = abs(radix),
radix2 = -power(radix,2)
<span style="color: #004080;">integer</span> <span style="color: #000000;">absrad</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">),</span>
if absrad<2 or absrad>6 then throw("base radix out of range") end if
<span style="color: #000000;">radix2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">absrad</span><span style="color: #0000FF;"><</span><span style="color: #000000;">2</span> <span style="color: #008080;">or</span> <span style="color: #000000;">absrad</span><span style="color: #0000FF;">></span><span style="color: #000000;">6</span> <span style="color: #008080;">then</span> <span style="color: #008080;">throw</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"base radix out of range"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
atom {re, im} = {complex_real(num), complex_imag(num)}
string {re_wh, re_fr} = base2(re, radix2, precision),
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">re</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">im</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">complex_real</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num</span><span style="color: #0000FF;">),</span> <span style="color: #7060A8;">complex_imag</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num</span><span style="color: #0000FF;">)}</span>
{im_wh, im_fr} = base2(im/radix, radix2, precision)
<span style="color: #004080;">string</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">re_wh</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">re_fr</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">re</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">radix2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">precision</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">im_wh</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">im_fr</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base2</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im</span><span style="color: #0000FF;">/</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">radix2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">precision</span><span style="color: #0000FF;">)</span>
string whole = reverse(zip(reverse(re_wh), reverse(im_wh))),
fraction = zip(im_fr, re_fr)
<span style="color: #004080;">string</span> <span style="color: #000000;">whole</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zip</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">re_wh</span><span style="color: #0000FF;">),</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im_wh</span><span style="color: #0000FF;">))),</span>
if fraction!="0" then whole &= '.'&fraction end if
<span style="color: #000000;">fraction</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zip</span><span style="color: #0000FF;">(</span><span style="color: #000000;">im_fr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">re_fr</span><span style="color: #0000FF;">)</span>
return whole
<span style="color: #008080;">if</span> <span style="color: #000000;">fraction</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">"0"</span> <span style="color: #008080;">then</span> <span style="color: #000000;">whole</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'.'</span><span style="color: #0000FF;">&</span><span style="color: #000000;">fraction</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">whole</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function parse_base(string str, integer radix)
 
<span style="color: #008080;">function</span> <span style="color: #000000;">parse_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">str</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">radix</span><span style="color: #0000FF;">)</span>
complexn fraction = 0
 
<span style="color: #000000;">complexn</span> <span style="color: #000000;">fraction</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
integer dot = find('.',str)
if dot then
<span style="color: #004080;">integer</span> <span style="color: #000000;">dot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">str</span><span style="color: #0000FF;">)</span>
string fr = str[dot+1..$]
<span style="color: #008080;">if</span> <span style="color: #000000;">dot</span> <span style="color: #008080;">then</span>
for i=1 to length(fr) do
<span style="color: #004080;">string</span> <span style="color: #000000;">fr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">str</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
integer c = fr[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fr</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
c -= iff(c>='a'?'a'-10:'0')
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
fraction = complex_add(fraction,complex_mul(c,complex_power({0,radix},-i)))
<span style="color: #000000;">c</span> <span style="color: #0000FF;">-=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">fraction</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fraction</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">complex_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">complex_power</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">},-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)))</span>
str = str[1..dot-1]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #000000;">str</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">str</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">dot</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
str = reverse(str)
for i=1 to length(str) do
<span style="color: #000000;">str</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">str</span><span style="color: #0000FF;">)</span>
integer c = str[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">str</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
c -= iff(c>='a'?'a'-10:'0')
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">str</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
fraction = complex_add(fraction,complex_mul(c,complex_power({0,radix},(i-1))))
<span style="color: #000000;">c</span> <span style="color: #0000FF;">-=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">fraction</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fraction</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">complex_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">complex_power</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">},(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))))</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return fraction
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">fraction</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant tests = {{0,2},{1,2},{5,2},{-13,2},{{0,9},2},{{0,-3},2},{{7.75,-7.5}, 2},{.25, 2}, -- base 2i tests
{{5,5}, 2},{{5,5}, 3},{{5,5}, 4},{{5,5}, 5},{{5,5}, 6}, -- same value, positive imaginary bases
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">7.75</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">7.5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">},{.</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- base 2i tests</span>
{{5,5},-2},{{5,5},-3},{{5,5},-4},{{5,5},-5},{{5,5},-6}, -- same value, negative imaginary bases
<span style="color: #0000FF;">{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- same value, positive imaginary bases</span>
{{227.65625,10.859375},4}, -- larger test value
<span style="color: #0000FF;">{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- same value, negative imaginary bases</span>
{{-579.8225308641975744,-5296.406378600824},6}} -- phix.rules
<span style="color: #0000FF;">{{</span><span style="color: #000000;">227.65625</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10.859375</span><span style="color: #0000FF;">},</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- larger test value</span>
 
<span style="color: #0000FF;">{{-</span><span style="color: #000000;">579.8225308641975744</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5296.406378600824</span><span style="color: #0000FF;">},</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- phix.rules
-- matches output of Sidef and Raku:
for t=1 to length(tests) do
-- matches output of Sidef and Raku:</span>
{complexn v, integer r} = tests[t]
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string ibase = base(v,r),
<span style="color: #0000FF;">{</span><span style="color: #000000;">complexn</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">]</span>
strv = complex_sprint(v),
<span style="color: #004080;">string</span> <span style="color: #000000;">ibase</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span>
strb = complex_sprint(parse_base(ibase, r))
<span style="color: #000000;">strv</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">),</span>
printf(1,"base(%20s, %2di) = %-10s : parse_base(%12s, %2di) = %s\n",
<span style="color: #000000;">strb</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parse_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ibase</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">))</span>
{strv, r, ibase, '"'&ibase&'"', r, strb})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"base(%20s, %2di) = %-10s : parse_base(%12s, %2di) = %s\n"</span><span style="color: #0000FF;">,</span>
end for
<span style="color: #0000FF;">{</span><span style="color: #000000;">strv</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ibase</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">'"'</span><span style="color: #0000FF;">&</span><span style="color: #000000;">ibase</span><span style="color: #0000FF;">&</span><span style="color: #008000;">'"'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">strb</span><span style="color: #0000FF;">})</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
-- matches output of Kotlin, Java, Go, D, and C#:
for ri=1 to 2 do -- real then imag
<span style="color: #000080;font-style:italic;">-- matches output of Kotlin, Java, Go, D, and C#:</span>
for i=1 to 16 do
<span style="color: #008080;">for</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- real then imag</span>
complexn c = iff(ri=1?i:{0,i}),
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">16</span> <span style="color: #008080;">do</span>
nc = complex_neg(c)
<span style="color: #000000;">complexn</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">i</span><span style="color: #0000FF;">:{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}),</span>
string sc = complex_sprint(c),
<span style="color: #000000;">nc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_neg</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
snc = complex_sprint(nc),
<span style="color: #004080;">string</span> <span style="color: #000000;">sc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span>
ib = base(c,2),
<span style="color: #000000;">snc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nc</span><span style="color: #0000FF;">),</span>
inb = base(nc,2),
<span style="color: #000000;">ib</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
rc = complex_sprint(parse_base(ib,2)),
<span style="color: #000000;">inb</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
rnc = complex_sprint(parse_base(inb,2))
<span style="color: #000000;">rc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parse_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)),</span>
printf(1,"%4s -> %8s -> %4s %4s -> %8s -> %4s\n",
<span style="color: #000000;">rnc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parse_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">inb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
{sc, ib, rc, snc, inb, rnc })
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4s -&gt; %8s -&gt; %4s %4s -&gt; %8s -&gt; %4s\n"</span><span style="color: #0000FF;">,</span>
end for
<span style="color: #0000FF;">{</span><span style="color: #000000;">sc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ib</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">snc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">inb</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rnc</span> <span style="color: #0000FF;">})</span>
puts(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for</lang>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
Matches the output of Sidef and Raku, except for the final line:
Line 2,706 ⟶ 3,288:
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang="python">import math
import re
 
Line 2,806 ⟶ 3,388:
 
print "done"
</syntaxhighlight>
</lang>
{{out}}
<pre> (1+0j) -> 1 -> (1+0j) (-1-0j) -> 103 -> (-1+0j)
Line 2,851 ⟶ 3,433:
Implements minimum, extra kudos and stretch goals.
 
<syntaxhighlight lang="raku" perl6line>multi sub base ( Real $num, Int $radix where -37 < * < -1, :$precision = -15 ) {
return '0' unless $num;
my $value = $num;
Line 2,906 ⟶ 3,488:
printf "%33s.&base\(%2si\) = %-11s : %13s.&parse-base\(%2si\) = %s\n",
$v, $r.im, $ibase, "'$ibase'", $r.im, $ibase.&parse-base($r).round(1e-10).narrow;
}</langsyntaxhighlight>
{{out}}
<pre> 0.&base( 2i) = 0 : '0'.&parse-base( 2i) = 0
Line 2,938 ⟶ 3,520:
Doing pretty much the same tests as the explicit version.
 
<syntaxhighlight lang="raku" perl6line>use Base::Any;
 
# TESTING
Line 2,951 ⟶ 3,533:
printf "%33s.&to-base\(%3si\) = %-11s : %13s.&from-base\(%3si\) = %s\n",
$v, $r.im, $ibase, "'$ibase'", $r.im, $ibase.&from-base($r).round(1e-10).narrow;
}</langsyntaxhighlight>
{{out}}
<pre> 0.&to-base( 2i) = 0 : '0'.&from-base( 2i) = 0
Line 2,974 ⟶ 3,556:
31433.3487654321-2902.4480452675i.&to-base( 6i) = PERL6.ROCKS : 'PERL6.ROCKS'.&from-base( 6i) = 31433.3487654321-2902.4480452675i
-3544.29+26541.468i.&to-base(-10i) = Raku.FTW : 'Raku.FTW'.&from-base(-10i) = -3544.29+26541.468i</pre>
=={{header|Ruby}}==
{{works with|Ruby|2.3}}
'''The Functions'''
<syntaxhighlight lang="ruby"># Convert a quarter-imaginary base value (as a string) into a base 10 Gaussian integer.
 
def base2i_decode(qi)
return 0 if qi == '0'
md = qi.match(/^(?<int>[0-3]+)(?:\.(?<frc>[0-3]+))?$/)
raise 'ill-formed quarter-imaginary base value' if !md
ls_pow = md[:frc] ? -(md[:frc].length) : 0
value = 0
(md[:int] + (md[:frc] ? md[:frc] : '')).reverse.each_char.with_index do |dig, inx|
value += dig.to_i * (2i)**(inx + ls_pow)
end
return value
end
 
# Convert a base 10 Gaussian integer into a quarter-imaginary base value (as a string).
 
def base2i_encode(gi)
odd = gi.imag.to_i.odd?
frac = (gi.imag.to_i != 0)
real = gi.real.to_i
imag = (gi.imag.to_i + 1) / 2
value = ''
phase_real = true
while (real != 0) || (imag != 0)
if phase_real
real, rem = real.divmod(4)
real = -real
else
imag, rem = imag.divmod(4)
imag = -imag
end
value.prepend(rem.to_s)
phase_real = !phase_real
end
value = '0' if value == ''
value.concat(odd ? '.2' : '.0') if frac
return value
end</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="ruby"># Extend class Integer with a string conveter.
 
class Integer
def as_str()
return to_s()
end
end
 
# Extend class Complex with a string conveter (works only with Gaussian integers).
 
class Complex
def as_str()
return '0' if self == 0
return real.to_i.to_s if imag == 0
return imag.to_i.to_s + 'i' if real == 0
return real.to_i.to_s + '+' + imag.to_i.to_s + 'i' if imag >= 0
return real.to_i.to_s + '-' + (-(imag.to_i)).to_s + 'i'
end
end
 
# Emit various tables of conversions.
 
1.step(16) do |gi|
puts(" %4s -> %8s -> %4s %4s -> %8s -> %4s" %
[gi.as_str, base2i_encode(gi), base2i_decode(base2i_encode(gi)).as_str,
(-gi).as_str, base2i_encode(-gi), base2i_decode(base2i_encode(-gi)).as_str])
end
puts
1.step(16) do |gi|
gi *= 0+1i
puts(" %4s -> %8s -> %4s %4s -> %8s -> %4s" %
[gi.as_str, base2i_encode(gi), base2i_decode(base2i_encode(gi)).as_str,
(-gi).as_str, base2i_encode(-gi), base2i_decode(base2i_encode(-gi)).as_str])
end
puts
0.step(3) do |m|
0.step(3) do |l|
0.step(3) do |h|
qi = (100 * h + 10 * m + l).to_s
gi = base2i_decode(qi)
md = base2i_encode(gi).match(/^(?<num>[0-3]+)(?:\.0)?$/)
print(" %4s -> %6s -> %4s" % [qi, gi.as_str, md[:num]])
end
puts
end
end</syntaxhighlight>
{{out}}
Conversions given in the task.
<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</pre>
{{out}}
From the OEIS Wiki [http://oeis.org/wiki/Quater-imaginary_base].
<pre> 0 -> 0 -> 0 100 -> -4 -> 100 200 -> -8 -> 200 300 -> -12 -> 300
1 -> 1 -> 1 101 -> -3 -> 101 201 -> -7 -> 201 301 -> -11 -> 301
2 -> 2 -> 2 102 -> -2 -> 102 202 -> -6 -> 202 302 -> -10 -> 302
3 -> 3 -> 3 103 -> -1 -> 103 203 -> -5 -> 203 303 -> -9 -> 303
10 -> 2i -> 10 110 -> -4+2i -> 110 210 -> -8+2i -> 210 310 -> -12+2i -> 310
11 -> 1+2i -> 11 111 -> -3+2i -> 111 211 -> -7+2i -> 211 311 -> -11+2i -> 311
12 -> 2+2i -> 12 112 -> -2+2i -> 112 212 -> -6+2i -> 212 312 -> -10+2i -> 312
13 -> 3+2i -> 13 113 -> -1+2i -> 113 213 -> -5+2i -> 213 313 -> -9+2i -> 313
20 -> 4i -> 20 120 -> -4+4i -> 120 220 -> -8+4i -> 220 320 -> -12+4i -> 320
21 -> 1+4i -> 21 121 -> -3+4i -> 121 221 -> -7+4i -> 221 321 -> -11+4i -> 321
22 -> 2+4i -> 22 122 -> -2+4i -> 122 222 -> -6+4i -> 222 322 -> -10+4i -> 322
23 -> 3+4i -> 23 123 -> -1+4i -> 123 223 -> -5+4i -> 223 323 -> -9+4i -> 323
30 -> 6i -> 30 130 -> -4+6i -> 130 230 -> -8+6i -> 230 330 -> -12+6i -> 330
31 -> 1+6i -> 31 131 -> -3+6i -> 131 231 -> -7+6i -> 231 331 -> -11+6i -> 331
32 -> 2+6i -> 32 132 -> -2+6i -> 132 232 -> -6+6i -> 232 332 -> -10+6i -> 332
33 -> 3+6i -> 33 133 -> -1+6i -> 133 233 -> -5+6i -> 233 333 -> -9+6i -> 333</pre>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func base (Number num, Number radix { _ ~~ (-36 .. -2) }, precision = -15) -> String {
num || return '0'
 
Line 3,047 ⟶ 3,770:
printf("base(%20s, %2si) = %-10s : parse_base(%12s, %2si) = %s\n",
v, r.im, ibase, "'#{ibase}'", r.im, parse_base(ibase, r).round(-8))
})</langsyntaxhighlight>
{{out}}
<pre>
Line 3,073 ⟶ 3,796:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 3,260 ⟶ 3,983:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 3,295 ⟶ 4,018:
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-complex}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./complex" for Complex
import "./fmt" for Fmt
 
class QuaterImaginary {
construct new(b2i) {
if (b2i.type != String || b2i == "" || !b2i.all { |d| "0123.".contains(d) } ||
b2i.count { |d| d == "." } > 1) Fiber.abort("Invalid Base 2i number.")
_b2i = b2i
}
 
// only works properly if 'c.real' and 'c.imag' are both integral
static fromComplex(c) {
if (c.real == 0 && c.imag == 0) return QuaterImaginary.new("0")
var re = c.real.truncate
var im = c.imag.truncate
var fi = -1
var sb = ""
while (re != 0) {
var rem = re % (-4)
re = (re/(-4)).truncate
if (rem < 0) {
rem = 4 + rem
re = re + 1
}
if (rem == -0) rem = 0 // get rid of minus zero
sb = sb + rem.toString + "0"
}
if (im != 0) {
var f = (Complex.new(0, c.imag) / Complex.imagTwo).real
im = f.ceil
f = -4 * (f - im)
var index = 1
while (im != 0) {
var rem = im % (-4)
im = (im/(-4)).truncate
if (rem < 0) {
rem = 4 + rem
im = im + 1
}
if (index < sb.count) {
var sbl = sb.toList
sbl[index] = String.fromByte(rem + 48)
sb = sbl.join()
} else {
if (rem == -0) rem = 0 // get rid of minus zero
sb = sb + "0" + rem.toString
}
index = index + 2
}
fi = f.truncate
}
if (sb.count > 0) sb = sb[-1..0]
if (fi != -1) {
if (fi == -0) fi = 0 // get rid of minus zero
sb = sb + ".%(fi)"
}
sb = sb.trimStart("0")
if (sb.startsWith(".")) sb = "0" + sb
return QuaterImaginary.new(sb)
}
 
toComplex {
var pointPos = _b2i.indexOf(".")
var posLen = (pointPos != -1) ? pointPos : _b2i.count
var sum = Complex.zero
var prod = Complex.one
for (j in 0...posLen) {
var k = _b2i.bytes[posLen-1-j] - 48
if (k > 0) sum = sum + prod * k
prod = prod * Complex.imagTwo
}
if (pointPos != -1) {
prod = Complex.imagTwo.inverse
var j = posLen + 1
while (j < _b2i.count) {
var k = _b2i.bytes[j] - 48
if (k > 0) sum = sum + prod * k
prod = prod / Complex.imagTwo
j = j + 1
}
}
return sum
}
 
toString { _b2i }
}
 
var imagOnly = Fn.new { |c| c.imag.toString + "i" }
 
var fmt = "$4s -> $8s -> $4s"
Complex.showAsReal = true
for (i in 1..16) {
var c1 = Complex.new(i, 0)
var qi = QuaterImaginary.fromComplex(c1)
var c2 = qi.toComplex
Fmt.write("%(fmt) ", c1, qi, c2)
c1 = -c1
qi = QuaterImaginary.fromComplex(c1)
c2 = qi.toComplex
Fmt.print(fmt, c1, qi, c2)
}
System.print()
for (i in 1..16) {
var c1 = Complex.new(0, i)
var qi = QuaterImaginary.fromComplex(c1)
var c2 = qi.toComplex
Fmt.write("%(fmt) ", imagOnly.call(c1), qi, imagOnly.call(c2))
c1 = -c1
qi = QuaterImaginary.fromComplex(c1)
c2 = qi.toComplex
Fmt.print(fmt, imagOnly.call(c1), qi, imagOnly.call(c2))
}</syntaxhighlight>
 
{{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
</pre>
2,122

edits