Imaginary base numbers: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Suggestion (OP may prefer to revert): signatures restored, hlint and hindent applied, imports specified.) |
|||
Line 1,085: | Line 1,085: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang Haskell>import Data.Char (chr, digitToInt, intToDigit, isDigit, ord) |
|||
<lang Haskell> |
|||
import Data. |
import Data.Complex (Complex(..), imagPart, realPart) |
||
import Data.List |
import Data.List (elemIndex, delete) |
||
import Data. |
import Data.Maybe (fromMaybe) |
||
import Data.Char |
|||
base :: Complex Float |
|||
base = 0 :+ 2 |
base = 0 :+ 2 |
||
quotRemPositive |
quotRemPositive |
||
:: (Integral a) |
|||
⚫ | |||
=> a -> a -> (a, a) |
|||
⚫ | |||
quotRemPositive a b |
|||
⚫ | |||
| otherwise = (q, r) |
|||
where |
|||
⚫ | |||
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 |
shiftRight :: String -> String |
||
shiftRight n |
|||
⚫ | |||
| l == '0' = h |
|||
⚫ | |||
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 = |
fromQItoComplex num b = |
||
let dot = fromMaybe (length num) (elemIndex '.' num) |
let dot = fromMaybe (length num) (elemIndex '.' num) |
||
fst $ |
in fst $ |
||
foldl |
|||
(\(acc, indx) x -> |
|||
(\(a, indx) x -> |
|||
(a + fromIntegral (digitToIntQI x) * (b ^^ (dot - indx)), indx + 1)) |
|||
(0, 1) |
|||
(0, 1) |
|||
(delete '.' num) |
|||
euclidEr |
euclidEr :: Int -> Int -> [Int] -> [Int] |
||
euclidEr a b l |
|||
⚫ | |||
| a == 0 = l |
|||
| otherwise = |
|||
⚫ | |||
in euclidEr q b (0 : r : l) |
|||
fromIntToQI :: Int -> [Int] |
|||
fromIntToQI 0 = [0] |
fromIntToQI 0 = [0] |
||
fromIntToQI x = tail (euclidEr x (floor $ realPart (base^^2)) []) |
fromIntToQI x = tail (euclidEr x (floor $ realPart (base ^^ 2)) []) |
||
getCuid |
|||
⚫ | |||
:: Integral a |
|||
=> Complex a -> a |
|||
⚫ | |||
qizip :: Complex Int -> [Int] |
|||
qizip c = let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c)) in |
|||
qizip c = |
|||
⚫ | |||
let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c)) |
|||
in let m = min (length r) (length i) |
|||
⚫ | |||
take (length i - m) i ++ |
|||
reverse (zipWith (+) (take m (reverse r)) (take m (reverse i))) |
|||
fromComplexToQI |
fromComplexToQI :: Complex Int -> String |
||
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip |
|||
main :: IO () |
|||
main = print (fromComplexToQI (35 :+ 23)) >> |
|||
print (fromQItoComplex "10.2" base) |
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang> |
||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre>"121003.2" |
||
"121003.2" |
|||
0.0 :+ 1.0 |
0.0 :+ 1.0 |
||
</pre> |
</pre> |
||
Line 1,138: | Line 1,166: | ||
<pre> |
<pre> |
||
"3z.8" |
"3z.8" |
||
0.0 :+ 7.75 |
0.0 :+ 7.75</pre> |
||
</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |