Advertisement
Guest User

bisquare

a guest
Oct 11th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.24 KB | None | 0 0
  1. module BiSquare
  2. open System
  3. open System.Diagnostics
  4.  
  5.  
  6.  
  7. type SquareRootResult =
  8.     | NoRoots
  9.     | OneSemiRoot of double
  10.     | TwoSemiRoots of double * double
  11.  
  12. type BiSquareRootResult =
  13.     | FullComplexRoots
  14.     | Zero
  15.     | TwoRoots of double * double
  16.     | FourRoots of double * double * double * double
  17.     | FourRootsWithComplex of double * double
  18.    
  19.  
  20. let CalculateSemiRoots(a: double, b: double, c: double):SquareRootResult =
  21.     let D = b*b - 4.0*a*c;
  22.     if D < 0.0 then NoRoots
  23.     else if D = 0.0 then
  24.         let rt = -b / (2.0 * a)
  25.         OneSemiRoot rt
  26.     else
  27.         let sqrtD = Math.Sqrt(D)
  28.         let rt1 = (-b + sqrtD) / (2.0 * a)
  29.         let rt2 = (-b - sqrtD) / (2.0 * a)
  30.         TwoSemiRoots (rt1, rt2)
  31.            
  32. let CalculateBiSquareRoots(semiroot: SquareRootResult): BiSquareRootResult =    
  33.     match semiroot with
  34.     | NoRoots -> FullComplexRoots
  35.     | OneSemiRoot(rt) when rt = 0.0 -> Zero
  36.     | OneSemiRoot(rt) when rt > 0.0 -> TwoRoots(sqrt(rt),-sqrt(rt))
  37.     | OneSemiRoot(rt) -> FullComplexRoots
  38.     | TwoSemiRoots(rt1, rt2) when rt1 < 0.0 && rt2 < 0.0 -> FullComplexRoots
  39.     | 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))
  40.     | TwoSemiRoots(rt1, rt2) -> FourRoots(sqrt(rt1), -sqrt(rt1), sqrt(rt2), -sqrt(rt2))
  41.            
  42.    
  43. let PrintRoots(a: double, b: double, c: double):unit =
  44.     printf "Коэффициенты: a=%A, b=%A, c=%A. " a b c
  45.     let root = CalculateBiSquareRoots(CalculateSemiRoots(a,b,c))
  46.     let textResult =
  47.         match root with
  48.         | FullComplexRoots -> "Корни лежат в комплексной плоскости"
  49.         | Zero -> "Просто " + "0"
  50.         | TwoRoots(rt1, rt2) -> "Два корня " + rt1.ToString() + " и " + rt2.ToString()
  51.         | FourRootsWithComplex(rt1, rt2) -> "Два корня " + rt1.ToString() + " и " + rt2.ToString() + " и еще два корня лежащие в комплексной плоскости"
  52.         | FourRoots(rt1, rt2, rt3, rt4) -> "Четыре корня " + rt1.ToString() + " и " + rt2.ToString() + rt3.ToString() + " и " + rt4.ToString()
  53.     printfn "%s" textResult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement