Imaginary base numbers: Difference between revisions
Content deleted Content added
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)
import Data.
import Data.List (elemIndex, delete)
import Data.
base :: Complex Float
base = 0 :+ 2
quotRemPositive
:: (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
| otherwise = (q, r)
where
digitToIntQI :: Char -> Int
digitToIntQI c
| isDigit c = digitToInt c
| otherwise = ord c - ord 'a' + 10
shiftRight
shiftRight n
if (l == '0') then h else h ++ "." ++ [l]▼
| l == '0' = h
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 + fromIntegral (digitToIntQI x) * (b ^^ (dot - indx)), indx + 1))
(delete '.' num)
euclidEr
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 =
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
qizip :: Complex Int -> [Int]
qizip c =
let m = min (length r) (length i) in▼
in let
take (length i - m) i ++
reverse (zipWith (+) (take m (reverse r)) (take m (reverse i)))
fromComplexToQI
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip
main :: IO ()
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang>
{{out}}
<pre>"121003.2"
0.0 :+ 1.0
</pre>
Line 1,138 ⟶ 1,166:
<pre>
"3z.8"
0.0 :+ 7.75</pre>
=={{header|Java}}==
|