Jump to content

Imaginary base numbers: Difference between revisions

m
Line 1,085:
 
=={{header|Haskell}}==
<lang Haskell>import Data.Maybe
import Data.Maybe
import Data.List
import Data.Complex
Line 1,092 ⟶ 1,093:
base = 0 :+ 2
 
quotRemPositive ::a (Integral a)b => 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
Line 1,116 ⟶ 1,112:
(delete '.' num)
 
euclidEr ::0 Intb ->l Int= -> [Int] -> [Int]l
euclidEr a b l = let (q, r) = quotRemPositive a b in euclidEr q b (0:r: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
Line 1,133 ⟶ 1,125:
++ reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))
 
fromComplexToQI :: Complex Int -> [Char]
fromComplexToQI c = shiftRight (map intToDigitQI (qizip c))
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.