Roots of a quadratic function: Difference between revisions

m
m (Fix syntax highlighting... with the correct page contents this time)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 4 users not shown)
Line 1,426:
The roots of 10.00x² + 1.00x + 1.00
x₀ = (-0.05 + 0.31im, -0.05 - 0.31im)</pre>
 
=={{header|K}}==
===K6===
{{works with|ngn/k}}
<syntaxhighlight lang="k"> / naive method
/ sqr[x] and sqrt[x] must be provided
quf:{[a;b;c]; s:sqrt[sqr[b]-4*a*c];(-b+s;-b-s)%2*a}
 
quf[0.5;-2.5;2]
1.0 4.0
quf[1;8;15]
-5.0 -3.0
quf[1;10;1]
-9.898979485566356 -0.10102051443364424
</syntaxhighlight>
 
=={{header|Kotlin}}==
Line 2,139 ⟶ 2,154:
=={{header|Raku}}==
(formerly Perl 6)
 
{{Works with|Rakudo|2022.12}}
 
''Works with previous versions also but will return slightly less precise results.''
 
Raku has complex number handling built in.
Line 2,147 ⟶ 2,166:
[1, -2, 1],
[1, 0, -4],
[1, -10**610⁶, 1]
-> @coefficients {
printf "Roots for %d, %d, %d\t=> (%s, %s)\n",
Line 2,154 ⟶ 2,173:
 
sub quadroots (*[$a, $b, $c]) {
( -$b + $_ ) / (2 *× $a),
( -$b - $_ ) / (2 *× $a)
given
($b ** 2² - 4 *× $a *× $c ).Complex.sqrt.narrow
}</syntaxhighlight>
{{out}}
<pre>Roots for 1, 2, 1 => (-1, -1)
Roots for 1, 2, 3 => (-1+1.4142135623731i4142135623730951i, -1-1.4142135623731i4142135623730951i)
Roots for 1, -2, 1 => (1, 1)
Roots for 1, 0, -4 => (2, -2)
Roots for 1, -1000000, 1 => (999999.999999, 1.00000761449337e-06)</pre>
 
=={{header|REXX}}==
Line 2,351 ⟶ 2,370:
return [x1, x2]
</syntaxhighlight>
 
=={{header|RPL}}==
RPL can solve quadratic functions directly :
'x^2-1E9*x+1' 'x' QUAD
returns
1: '(1000000000+s1*1000000000)/2'
which can then be turned into roots by storing 1 or -1 in the <code>s1</code> variable and evaluating the formula:
DUP 1 's1' STO EVAL SWAP -1 's1' STO EVAL
hence returning
2: 1000000000
1: 0
So let's implement the algorithm proposed by the task:
{| class="wikitable"
! RPL code
! Comment
|-
|
'''IF''' DUP TYPE 1 == '''THEN'''
'''IF''' DUP IM NOT '''THEN''' RE '''END END'''
≫ '<span style="color:blue">REALZ</span>' STO
.
≪ → a b c
≪ '''IF''' b NOT '''THEN''' c a / NEG √ DUP NEG '''ELSE'''
a c * √ b /
1 SWAP SQ 4 * - √ 2 / 0.5 +
b * NEG
DUP a / <span style="color:blue">REALZ</span>
c ROT / <span style="color:blue">REALZ</span> '''END'''
≫ ≫ '<span style="color:blue">QROOT</span>' STO
|
<span style="color:blue">REALZ</span> ''( number → number )''
if number is a complex
with no imaginary part, then turn it into a real
<span style="color:blue">QROOT</span> ''( a b c → r1 r2 ) ''
if b=0 then roots are obvious, else
q = sqrt(a*c)/b
f = 1/2+sqrt(1-4*q^2)/2
get -b*f
root1 = -b/a*f
root2 = -c/(b*f)
|}
1 -1E9 1 <span style="color:blue">QROOT</span>
actually returns a more correct answer:
2: 1000000000
1: .000000001
 
=={{header|Ruby}}==
Line 2,659 ⟶ 2,727:
{{trans|Go}}
{{libheader|Wren-complex}}
<syntaxhighlight lang="ecmascriptwren">import "./complex" for Complex
 
var quadratic = Fn.new { |a, b, c|
Line 2,711 ⟶ 2,779:
coefficients: 1, -1000, 1 -> two real roots: 999.998999999 and 0.001000001000002
coefficients: 1, -1000000000, 1 -> two real roots: 1000000000 and 1e-09
</pre>
 
=={{header|XPL0}}==
{{trans|Go}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
func real QuadRoots(A, B, C); \Return roots of quadratic equation
real A, B, C;
real D, E, R;
[R:= [0., 0., 0.];
R(0):= 0.; R(1):= 0.; R(2):= 0.;
D:= B*B - 4.*A*C;
case of
D = 0.: [R(0):= -B / (2.*A); \single root
R(1):= R(0);
];
D > 0.: [if B < 0. then \two real roots
E:= sqrt(D) - B
else E:= -sqrt(D) - B;
R(0):= E / (2.*A);
R(1):= 2. * C / E;
];
D < 0.: [R(0):= -B / (2.*A); \real
R(2):= sqrt(-D) /(2.*A); \imaginary
]
other []; \D overflowed or a coefficient was NaN
return R;
];
 
func Test(A, B, C);
real A, B, C;
real R;
[Print("coefficients: %g, %g, %g -> ", A, B, C);
R:= QuadRoots(A, B, C);
if R(2) # 0. then
Print("two complex roots: %g+%gi, %g-%gi\n", R(0), R(2), R(0), R(2))
else [if R(0) = R(1) then
Print("one real root: %g\n", R(0))
else Print("two real roots: %15.15g, %15.15g\n", R(0), R(1));
];
];
 
real C; int I;
[C:= [ [1., -2., 1.],
[1., 0., 1.],
[1., -10., 1.],
[1., -1000., 1.],
[1., -1e9, 1.],
[1., -4., 6.] ];
for I:= 0 to 5 do
Test(C(I,0), C(I,1), C(I,2));
]</syntaxhighlight>
{{out}}
<pre>
coefficients: 1, -2, 1 -> one real root: 1
coefficients: 1, 0, 1 -> two complex roots: 0+1i, 0-1i
coefficients: 1, -10, 1 -> two real roots: 9.89897948556636, 0.101020514433644
coefficients: 1, -1000, 1 -> two real roots: 999.998999999, 0.001000001000002
coefficients: 1, -1e9, 1 -> two real roots: 1000000000, 0.000000001
coefficients: 1, -4, 6 -> two complex roots: 2+1.41421i, 2-1.41421i
</pre>
 
9,476

edits