Roots of a quadratic function: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring the hard way) |
|||
Line 1,713: | Line 1,713: | ||
IO.Put("X1 = " & Fmt.LongReal(r[1]) & " X2 = " & Fmt.LongReal(r[2]) & "\n"); |
IO.Put("X1 = " & Fmt.LongReal(r[1]) & " X2 = " & Fmt.LongReal(r[2]) & "\n"); |
||
END Quad.</lang> |
END Quad.</lang> |
||
=={{header|Nim}}== |
|||
<lang Nim>import math, complex, strformat |
|||
const Epsilon = 1e-15 |
|||
type |
|||
SolKind = enum solDouble, solFloat, solComplex |
|||
Roots = object |
|||
case kind: SolKind |
|||
of solDouble: |
|||
fvalue: float |
|||
of solFloat: |
|||
fvalues: (float, float) |
|||
of solComplex: |
|||
cvalues: (Complex64, Complex64) |
|||
func quadRoots(a, b, c: float): Roots = |
|||
if a == 0: |
|||
raise newException(ValueError, "first coefficient cannot be null.") |
|||
let den = a * 2 |
|||
let Δ = b * b - a * c * 4 |
|||
if abs(Δ) < Epsilon: |
|||
result = Roots(kind: solDouble, fvalue: -b / den) |
|||
elif Δ < 0: |
|||
let r = -b / den |
|||
let i = sqrt(-Δ) / den |
|||
result = Roots(kind: solComplex, cvalues: (complex64(r, i), complex64(r, -i))) |
|||
else: |
|||
let r = (if b < 0: -b + sqrt(Δ) else: -b - sqrt(Δ)) / den |
|||
result = Roots(kind: solFloat, fvalues: (r, c / (a * r))) |
|||
func `$`(r: Roots): string = |
|||
case r.kind |
|||
of solDouble: |
|||
result = $r.fvalue |
|||
of solFloat: |
|||
result = &"{r.fvalues[0]}, {r.fvalues[1]}" |
|||
of solComplex: |
|||
result = &"{r.cvalues[0].re} + {r.cvalues[0].im}i, {r.cvalues[1].re} + {r.cvalues[1].im}i" |
|||
when isMainModule: |
|||
const Equations = [(1.0, -2.0, 1.0), |
|||
(10.0, 1.0, 1.0), |
|||
(1.0, -10.0, 1.0), |
|||
(1.0, -1000.0, 1.0), |
|||
(1.0, -1e9, 1.0)] |
|||
for (a, b, c) in Equations: |
|||
echo &"Equation: {a=}, {b=}, {c=}" |
|||
let roots = quadRoots(a, b, c) |
|||
let plural = if roots.kind == solDouble: "" else: "s" |
|||
echo &" root{plural}: {roots}"</lang> |
|||
{{out}} |
|||
<pre>Equation: a=1.0, b=-2.0, c=1.0 |
|||
root: 1.0 |
|||
Equation: a=10.0, b=1.0, c=1.0 |
|||
roots: -0.05 + 0.3122498999199199i, -0.05 + -0.3122498999199199i |
|||
Equation: a=1.0, b=-10.0, c=1.0 |
|||
roots: 9.898979485566356, 0.1010205144336438 |
|||
Equation: a=1.0, b=-1000.0, c=1.0 |
|||
roots: 999.998999999, 0.001000001000002 |
|||
Equation: a=1.0, b=-1000000000.0, c=1.0 |
|||
roots: 1000000000.0, 1e-09</pre> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |