aboutsummaryrefslogtreecommitdiff
path: root/src/equation.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-10 16:10:34 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-10 16:10:34 +0100
commit62065753a52f66eb8234deb2e4d09f83d870080c (patch)
tree30d87fdc17756c3600dc3c4222310323ef1f5cd1 /src/equation.hs
parentdf5d3f05ead68a6cdecfeccecadce45fd4b574fd (diff)
downloadcomputorv1-62065753a52f66eb8234deb2e4d09f83d870080c.tar.gz
computorv1-62065753a52f66eb8234deb2e4d09f83d870080c.tar.bz2
computorv1-62065753a52f66eb8234deb2e4d09f83d870080c.zip
Parser refactoring and replaced stdlib sqrt function with mySqrtHEADmaster
Diffstat (limited to 'src/equation.hs')
-rw-r--r--src/equation.hs15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/equation.hs b/src/equation.hs
index 040812f..f332131 100644
--- a/src/equation.hs
+++ b/src/equation.hs
@@ -69,8 +69,8 @@ solveDegree2 :: Float -> Float -> Float -> [Float]
solveDegree2 a b c
| phi < 0 = []
| phi == 0 = [(-b) / (2.0 * a)]
- | phi > 0 = [ (-b + sqrt phi) / (2.0 * a) -- not alowed
- , (-b - sqrt phi) / (2.0 * a)
+ | phi > 0 = [ (-b + mySqrt phi) / (2.0 * a) -- not alowed
+ , (-b - mySqrt phi) / (2.0 * a)
]
where phi = b * b - 4.0 * a * c
@@ -82,3 +82,14 @@ solve [t0] = []
solve [t0, t1] = [solveDegree1 (coefficient t1) (coefficient t0)]
solve [t0, t1, t2] = solveDegree2 (coefficient t2) (coefficient t1) (coefficient t0)
solve _ = undefined
+
+mySqrt :: Float -> Float
+mySqrt n
+ | n < 0 = undefined
+ | otherwise = mySqrt' (n / 2)
+ where mySqrt' x = if abs (x * x - n) < 0.01
+ then x
+ else mySqrt' xn
+ where xn = b - (a * a) / (2 * b)
+ where a = (n - x * x) / (2 * x)
+ b = x + a