큰 숫자를 정밀도로 계산할 수 있도록 새로운 유형을 만들고 있습니다. 기본적으로는 Double과 Integer를 사용하여 숫자를 Double * 10^Integer
으로 나타냅니다. 이제는 프로그램을 만들기 시작했고 새로운 번호의 인스턴스를 만들려고 할 때까지 "ok"가되어 간단 번호 +
을 사용하여 새 번호를 추가 할 수있었습니다. 이렇게하면 기존 프로그램에서 더 쉽게 사용할 수 있습니다. 하지만 난 그냥 내 코드에서 "추론 할 수 없다"라인을 따라 오류가 발생했습니다 (아래 예제를 게시 할 예정입니다). 나는 약간의 오류를 이해하지만, 나는 문제를 해결할 수있는 것처럼 보일 수있다. 코드를 컴파일하려면 4 행과 5 행을 주석 처리하십시오.연산자 오버로딩 :: HaskellNewbie
저는 몇 시간 동안이 작업을하고 있으며 "죽이기"입니다. 어떤 도움이 많이 감사합니다
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)'
: 여기
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
는 예를 들어 오류입니다!
"하지만 난 그냥 ge 내 코드에서 오류가 발생했습니다. " 당신은 오류를 읽으려고 노력 했습니까? 그리고 그 문제를 핵심으로 줄였습니까? 두 오류 모두 5 행을 참조하십시오. –
오류가 게시되면 도움이됩니다. 'Num' typeclass에 대한 최소한의 완전한 정의를 구현하는 것을 무시한 즉각적인 문제를 볼 수 있습니다. 당신은 또한'+','-'또는'negate','abs' 및'signum'을 필요로합니다. (모든 것이다고 생각하지만, 문서를 확인하십시오) – bheklilr
[과학적] (http : // hackage. haskell.org/package/scientific-0.1.0.0) 패키지가 대신 유용할까요? –