Imaginary base numbers: Difference between revisions
Content deleted Content added
Line 1,082:
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i
</pre>
=={{header|Haskell}}==
<lang Haskell>import Data.Maybe
import Data.List
import Data.Complex
import Data.Char
base = 0 :+ 2
quotRemPositive :: (Integral a) => a -> a -> (a, a)
quotRemPositive a b=
let (q, r) = quotRem a b in
if (r < 0) then (1 + q, (floor $ realPart (- base^^2)) + r) else (q, r)
digitToIntQI :: Char -> Int
digitToIntQI c = if (isDigit c) then (digitToInt c) else ((ord c) - (ord 'a') + 10)
shiftRight :: [Char] -> [Char]
shiftRight n = let (l, h) = (last n, init n) in
if (l == '0') then h else h ++ "." ++ [l]
intToDigitQI :: Int -> Char
intToDigitQI i = if (elem i [0..9]) then (intToDigit i) else (chr (i - 10 + (ord 'a')))
fromQItoComplex :: (RealFloat a) => [Char] -> Complex a -> Complex a
fromQItoComplex num b =
let dot = fromMaybe (length num) (elemIndex '.' num) in
fst $ foldl
(\(acc, indx) x ->
(acc + (fromIntegral $ digitToIntQI x)*(b^^(dot - indx)), indx + 1))
(0, 1)
(delete '.' num)
euclidEr :: Int -> Int -> [Int] -> [Int]
euclidEr a b l =
if (a == 0) then l else 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)) [])
getCuid :: Integral a => Complex a -> a
getCuid c = (imagPart c)*(floor $ imagPart (-base))
qizip :: Complex Int -> [Int]
qizip c = let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c)) in
let m = min (length r) (length 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 -> [Char]
fromComplexToQI c = shiftRight (map intToDigitQI (qizip c))
main = print (fromComplexToQI (35 :+ 23)) >>
print (fromQItoComplex "10.2" base)
</lang>
{{out}}
<pre>
"121003.2"
0.0 :+ 1.0
</pre>
With base = 8i (you may choose any base):
<pre>
"3z.8"
0.0 :+ 7.75
</pre>
|