rootOfFunction :: (Floating n, Ord n) => (n -> n) -> (n, n) -> n rootOfFunction f (a, b) | sa == sb = error "Maybe, no roots on this segment" | otherwise = rootOfFunction' (sa, sb, a, b) f where sa = if f a >= 0 then 1 else (-1) sb = if f b >= 0 then 1 else (-1) rootOfFunction' :: (Floating n, Ord n) => (Int, Int, n, n) -> (n -> n) -> n rootOfFunction' (sa, sb, a, b) f | abs (b - a) <= 1e-14 = c | sc == sa = rootOfFunction' (sc, sb, c, b) f | otherwise = rootOfFunction' (sa, sc, a, c) f where c = (a + b)/2 sc = if f c >= 0 then 1 else (-1) loga :: (Floating n, Ord n) => n -> n -> n loga 0 x = error "Illegal base of logarithm" loga 1 x = error "Illegal base of logarithm" loga a x = let b = if a < 1 then 1/a else a l = if a < 0 then error "Illegal base of logarithm" else loga' (0, x, 1) b in if a < 1 then (-l) else l loga' :: (Floating n, Ord n) => (n, n, n) -> n -> n loga' (y, z, t) a | 1/a <= z && z <= a && abs(t) <= 1e-16 = y | z < 1/a = loga' (y-t, z*a, t) a | z > a = loga' (y+t, z/a, t) a | otherwise = loga' (y, z*z, t/2) a mySin :: (Floating n, Ord n, RealFrac n) => n -> n mySin x = mySin' (0, x1, 1) x1 where x1 = x - (2*pi)*fromIntegral (floor(x/(2*pi))) mySin' :: (Floating n, Ord n, RealFrac n) => (n, n, n) -> n -> n mySin' (y, a, k) x | abs(a) <= 1e-15 = y | otherwise = mySin' (y + a, (-a)*x*x/((k+1)*(k+2)), k+2) x arctg :: Double -> Double arctg x = let z = (sqrt(x*x + 1) - 1)/x atanz = arctg' (0, z, 1) z in 2*atanz arctg' :: (Double, Double, Double) -> Double -> Double arctg' (s, xn, n) x | abs(xn/n) <= 1e-14 = s | otherwise = arctg' (s + xn/n, (-xn)*x*x, n+2) x mySqrt :: (Floating n, Ord n) => n -> n mySqrt a | a < 0 = error "square root of negative number" | a == 0 = 0 | otherwise = mySqrt' (2 + a, 1 + a) a mySqrt' :: (Floating n, Ord n) => (n, n) -> n -> n mySqrt' (x0, x1) a | abs(x0 - x1) <= 1e-16 = x1 | otherwise = mySqrt' (x1, (x1 + a/x1)/2) a