Roots of a quadratic function: Difference between revisions
Content added Content deleted
m (→version 1: simplified expressions in the subroutine and also the function, added an output case to show different formatting for another REXX.) |
|||
Line 1,554: | Line 1,554: | ||
Roots for 1, 0, -4 => (2, -2) |
Roots for 1, 0, -4 => (2, -2) |
||
Roots for 1, -1000000, 1 => (999999.999999, 1.00000761449337e-06)</pre> |
Roots for 1, -1000000, 1 => (999999.999999, 1.00000761449337e-06)</pre> |
||
=={{header|Phix}}== |
|||
{{trans|ERRE}} |
|||
<lang Phix>procedure solve_quadratic(sequence t3) |
|||
atom {a,b,c} = t3 |
|||
atom d = b*b-4*a*c, f |
|||
string s = sprintf("for a=%g,b=%g,c=%g",t3), t |
|||
sequence u |
|||
if abs(d)<1e-6 then d=0 end if |
|||
switch sign(d) do |
|||
case 0: t = "single root is %g" |
|||
u = {-b/2/a} |
|||
case 1: t = "real roots are %g and %g" |
|||
f = (1+sqrt(1-4*a*c/(b*b)))/2 |
|||
u = {-f*b/a,-c/b/f} |
|||
case-1: t = "complex roots are %g +/- %g*i" |
|||
u = {-b/2/a,sqrt(-d)/2/a} |
|||
end switch |
|||
printf(1,"%-25s the %s\n",{s,sprintf(t,u)}) |
|||
end procedure |
|||
constant tests = {{1,-1E9,1}, |
|||
{1,0,1}, |
|||
{2,-1,-6}, |
|||
{1,2,-2}, |
|||
{0.5,1.4142135,1}, |
|||
{1,3,2}, |
|||
{3,4,5}} |
|||
for i=1 to length(tests) do |
|||
solve_quadratic(tests[i]) |
|||
end for</lang> |
|||
<pre> |
|||
for a=1,b=-1e+9,c=1 the real roots are 1e+9 and 1e-9 |
|||
for a=1,b=0,c=1 the complex roots are 0 +/- 1*i |
|||
for a=2,b=-1,c=-6 the real roots are 2 and -1.5 |
|||
for a=1,b=2,c=-2 the real roots are -2.73205 and 0.732051 |
|||
for a=0.5,b=1.41421,c=1 the single root is -1.41421 |
|||
for a=1,b=3,c=2 the real roots are -2 and -1 |
|||
for a=3,b=4,c=5 the complex roots are -0.666667 +/- 1.10554*i |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |