Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module BiSquare
- open System
- open System.Diagnostics
- type SquareRootResult =
- | NoRoots
- | OneSemiRoot of double
- | TwoSemiRoots of double * double
- type BiSquareRootResult =
- | FullComplexRoots
- | Zero
- | TwoRoots of double * double
- | FourRoots of double * double * double * double
- | FourRootsWithComplex of double * double
- let CalculateSemiRoots(a: double, b: double, c: double):SquareRootResult =
- let D = b*b - 4.0*a*c;
- if D < 0.0 then NoRoots
- else if D = 0.0 then
- let rt = -b / (2.0 * a)
- OneSemiRoot rt
- else
- let sqrtD = Math.Sqrt(D)
- let rt1 = (-b + sqrtD) / (2.0 * a)
- let rt2 = (-b - sqrtD) / (2.0 * a)
- TwoSemiRoots (rt1, rt2)
- let CalculateBiSquareRoots(semiroot: SquareRootResult): BiSquareRootResult =
- match semiroot with
- | NoRoots -> FullComplexRoots
- | OneSemiRoot(rt) when rt = 0.0 -> Zero
- | OneSemiRoot(rt) when rt > 0.0 -> TwoRoots(sqrt(rt),-sqrt(rt))
- | OneSemiRoot(rt) -> FullComplexRoots
- | TwoSemiRoots(rt1, rt2) when rt1 < 0.0 && rt2 < 0.0 -> FullComplexRoots
- | TwoSemiRoots(rt1, rt2) when rt1 < 0.0 || rt2 < 0.0 -> if rt1>0.0 then FourRootsWithComplex(sqrt(rt1), -sqrt(rt1)) else FourRootsWithComplex(sqrt(rt2), -sqrt(rt2))
- | TwoSemiRoots(rt1, rt2) -> FourRoots(sqrt(rt1), -sqrt(rt1), sqrt(rt2), -sqrt(rt2))
- let PrintRoots(a: double, b: double, c: double):unit =
- printf "Коэффициенты: a=%A, b=%A, c=%A. " a b c
- let root = CalculateBiSquareRoots(CalculateSemiRoots(a,b,c))
- let textResult =
- match root with
- | FullComplexRoots -> "Корни лежат в комплексной плоскости"
- | Zero -> "Просто " + "0"
- | TwoRoots(rt1, rt2) -> "Два корня " + rt1.ToString() + " и " + rt2.ToString()
- | FourRootsWithComplex(rt1, rt2) -> "Два корня " + rt1.ToString() + " и " + rt2.ToString() + " и еще два корня лежащие в комплексной плоскости"
- | FourRoots(rt1, rt2, rt3, rt4) -> "Четыре корня " + rt1.ToString() + " и " + rt2.ToString() + rt3.ToString() + " и " + rt4.ToString()
- printfn "%s" textResult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement