I'm making a new type so I can calculate big numbers with some precision. Basically its a Double and a Integer to represent a number as Double * 10 ^ Integer. Now I began to make the program and it was going "ok" till I tried to make a instance of my new number so I could simple use + to add my new numbers up. This makes it easier for me to use in my existing programs. But I'm just getting a errors along the lines of "Could not deduce" from my code (I'll post an example below). I somewhat understand the error, but I can seem to get around the problem. If you wish to compile the code, comment lines 4 and 5.
I'v been working on this for hours and is "killing" me.
newtype Sci f p = Sci (f ,p) deriving (Eq,Show)
instance (Floating a,Integral b) => Num (Sci a b) where
Sci (a,b) * Sci (c,d) = fixSci( Sci(a*c,b*d) )
mulSci :: Sci Double Integer -> Sci Double Integer -> Sci Double Integer
mulSci (Sci(a,b)) (Sci(c,d)) = fixSci (Sci(a*c,b*d))
mkSci :: Double -> Sci Double Integer
mkSci 0 = Sci(0, 0)
mkSci n = let lg = (floor ((log10 . abs) n)) in Sci((n/(10**(fromIntegral lg))), if lg > 0 then lg else 0)
fixSci :: Sci Double Integer -> Sci Double Integer
fixSci (Sci(a,b)) = let n = mkSci a in (\(Sci(c,d)) -> Sci(c,b+d)) n
fromSci (Sci(a,b)) = a*10**(fromIntegral b)
showSci (Sci(a,b)) = (show a)++"e"++(show b)
lx :: Double
lx = log 10
log10 :: Double -> Double
log10 y = log y / lx
-- ~ main = putStrLn $ showSci $ fixSci $ Sci(95,0)
main = putStrLn $ showSci $ mkSci 95
Here is an example error:
sci.hs:5:40:
Could not deduce (a ~ Double)
from the context (Floating a, Integral b)
bound by the instance declaration at sci.hs:4:10-49
`a' is a rigid type variable bound by
the instance declaration at sci.hs:4:20
In the first argument of `(*)', namely `a'
In the expression: a * c
In the first argument of `Sci', namely `(a * c, b * d)'
sci.hs:5:44:
Could not deduce (b ~ Integer)
from the context (Floating a, Integral b)
bound by the instance declaration at sci.hs:4:10-49
`b' is a rigid type variable bound by
the instance declaration at sci.hs:4:31
In the first argument of `(*)', namely `b'
In the expression: b * d
In the first argument of `Sci', namely `(a * c, b * d)'
Any help is much appreciated!
Numtypeclass. You also need+,-ornegate,abs, andsignum(I think that's all, but check the docs to be sure)(Int64, Int64)seems cleaner than(Double, Integer)to store your components