data Tick = Tick
{ _price :: Int
, _timestamp :: TimeOfDay
} deriving (Eq, Ord)
instance Num Tick where
(-) (Tick a b) (Tick a' _) = Tick (a - a') b
calcAxis :: High -> Low -> Int
calcAxis (Just (Tick a _)) (Just (Tick b _)) = (fromIntegral (a - b)) / 2
I understand dividing Int
's will produce a Float
, so I need to cast the Int
's using fromIntegral
before using (/)
however I still get error:
• No instance for (Fractional Int) arising from a use of ‘/’
• In the expression: (fromIntegral (a - b)) / 2
In an equation for ‘calcAxis’:
calcAxis (Just (Tick a _)) (Just (Tick b _))
= (fromIntegral (a - b)) / 2
I dont really understand what its asking me to do. Surely I don't have to define an instance for Fractional Int
?
I assume I'm doing something wrong.
(fromIntegral (a - b)) / 2
— this is Fractional, but you've asked (via the type signature ofcalcAxis
) for it to beInt
. – ShreevatsaRcalcAxis
to return aFloat
it typechecks ok. – ktecFloat
and don’t remove the division. – Ry-Float
!! thanks – ktecFloat
is pretty unusual in Haskell, normally the higher-precisionDouble
or exactRational
are prefered. – leftaroundabout