Imaginary base numbers: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
m →‎{{header|Haskell}}: Suggestion (OP may prefer to revert): signatures restored, hlint and hindent applied, imports specified.
Line 1,085:
 
=={{header|Haskell}}==
<lang Haskell>import Data.Char (chr, digitToInt, intToDigit, isDigit, ord)
<lang Haskell>
import Data.MaybeComplex (Complex(..), imagPart, realPart)
import Data.List (elemIndex, delete)
import Data.ComplexMaybe (fromMaybe)
import Data.Char
 
 
base :: Complex Float
base = 0 :+ 2
 
quotRemPositive a b =
:: (Integral a)
let (q, r) = quotRem a b in
=> a -> a -> (a, a)
if (r < 0) then (1 + q, (floor $ realPart (- base^^2)) + r) else (q, r)
quotRemPositive a b
| if (r < 0) then= (1 + q, (floor $ (realPart (-base base^^ 2)) + r) else (q, r)
| otherwise = (q, r)
where
let (q, r) = quotRem a b in
 
digitToIntQI :: Char -> Int
digitToIntQI c = if (isDigit c) then (digitToInt c) else ((ord c) - (ord 'a') + 10)
digitToIntQI c
| isDigit c = digitToInt c
| otherwise = ord c - ord 'a' + 10
 
shiftRight n:: =String let-> (l, h) = (last n, init n) inString
shiftRight n
if (l == '0') then h else h ++ "." ++ [l]
| l == '0' = h
if| (lotherwise == '0') then h else h ++ "." ++ [l]
where
(l, h) = (last n, init n)
 
intToDigitQI :: Int -> Char
intToDigitQI i = if (elem i [0..9]) then (intToDigit i) else (chr (i - 10 + (ord 'a')))
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
in fst $ foldl
foldl
(\(acc, indx) x ->
(acc + (fromIntegral $ digitToIntQI x)*(b^^\(dot -a, indx)), indxx + 1))->
(a + fromIntegral (digitToIntQI x) * (b ^^ (dot - indx)), indx + 1))
(0, 1)
(delete '.' num (0, 1)
(delete '.' num)
 
euclidEr 0:: bInt l-> =Int l-> [Int] -> [Int]
euclidEr a b l
euclidEr a b l = let (q, r) = quotRemPositive a b in euclidEr q b (0:r:l)
| a == 0 = l
| otherwise =
euclidEr a b l = let (q, r) = quotRemPositive a b in euclidEr q b (0:r:l)
in euclidEr q b (0 : r : l)
 
fromIntToQI :: Int -> [Int]
fromIntToQI 0 = [0]
fromIntToQI x = tail (euclidEr x (floor $ realPart (base ^^ 2)) [])
 
getCuid
getCuid c = (imagPart c)*(floor $ imagPart (-base))
:: 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
qizip c =
let m = min (length r) (length i) in
let (taker, ((length ri) -= m)(fromIntToQI (realPart rc) ++ (take[0], fromIntToQI ((lengthgetCuid ic) - m) i)
in let ++m reverse= (zipWithmin (+) (take m (reverselength r)) (take m (reverselength i)))
let m = minin take (length r) (length- im) inr ++
take (length i - m) i ++
reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))
 
fromComplexToQI c:: =Complex shiftRightInt (map-> intToDigitQI (qizip c))String
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip
 
main :: IO ()
main = print (fromComplexToQI (35 :+ 23)) >>
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang>
</lang>
{{out}}
<pre>"121003.2"
"121003.2"
0.0 :+ 1.0
</pre>
Line 1,138 ⟶ 1,166:
<pre>
"3z.8"
0.0 :+ 7.75</pre>
</pre>
 
=={{header|Java}}==