Roots of a quadratic function: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 804:
(999999.999999 :+ 0.0,1.000000000001e-6 :+ 0.0)
(1.0e10 :+ 0.0,1.0e-10 :+ 0.0)</pre>
 
=={{header|IDL}}==
<lang idl>compile_OPT IDL2
 
print, "input a, press enter, input b, press enter, input c, press enter"
read,a,b,c
Promt='Enter values of a,b,c and hit enter'
 
a0=0.0
b0=0.0
c0=0.0 ;make them floating point variables
 
x=-b+sqrt((b^2)-4*a*c)
y=-b-sqrt((b^2)-4*a*c)
z=2*a
d= x/z
e= y/z
 
print, d,e</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 846 ⟶ 827:
->
</pre>
 
=={{header|IDL}}==
<lang idl>compile_OPT IDL2
 
print, "input a, press enter, input b, press enter, input c, press enter"
read,a,b,c
Promt='Enter values of a,b,c and hit enter'
 
a0=0.0
b0=0.0
c0=0.0 ;make them floating point variables
 
x=-b+sqrt((b^2)-4*a*c)
y=-b-sqrt((b^2)-4*a*c)
z=2*a
d= x/z
e= y/z
 
print, d,e</lang>
 
=={{header|IS-BASIC}}==
Line 1,364:
end</lang>
The "trick" lies in avoiding subtracting large values that differ by a small amount, which is the source of instability in the "normal" formula. It is trivial to prove that 2c/(b + sqrt(b^2-4ac)) = (b - sqrt(b^2-4ac))/2a.
 
 
=={{header|Maple}}==
Line 1,606 ⟶ 1,605:
return ( -$b + $root )/(2*$a), ( -$b - $root )/(2*$a);
}</lang>
 
=={{header|Perl 6}}==
 
Perl 6 has complex number handling built in.
 
<lang perl6>for
[1, 2, 1],
[1, 2, 3],
[1, -2, 1],
[1, 0, -4],
[1, -10**6, 1]
-> @coefficients {
printf "Roots for %d, %d, %d\t=> (%s, %s)\n",
|@coefficients, |quadroots(@coefficients);
 
sub quadroots (*[$a, $b, $c]) {
( -$b + $_ ) / (2 * $a),
( -$b - $_ ) / (2 * $a)
given
($b ** 2 - 4 * $a * $c ).Complex.sqrt.narrow
}</lang>
{{out}}
<pre>Roots for 1, 2, 1 => (-1, -1)
Roots for 1, 2, 3 => (-1+1.4142135623731i, -1-1.4142135623731i)
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|Phix}}==
Line 1,869 ⟶ 1,840:
;(quadratic 1 0.0000000000001 1)
;'(-5e-014+1.0i -5e-014-1.0i)</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
 
Perl 6 has complex number handling built in.
 
<lang perl6>for
[1, 2, 1],
[1, 2, 3],
[1, -2, 1],
[1, 0, -4],
[1, -10**6, 1]
-> @coefficients {
printf "Roots for %d, %d, %d\t=> (%s, %s)\n",
|@coefficients, |quadroots(@coefficients);
 
sub quadroots (*[$a, $b, $c]) {
( -$b + $_ ) / (2 * $a),
( -$b - $_ ) / (2 * $a)
given
($b ** 2 - 4 * $a * $c ).Complex.sqrt.narrow
}</lang>
{{out}}
<pre>Roots for 1, 2, 1 => (-1, -1)
Roots for 1, 2, 3 => (-1+1.4142135623731i, -1-1.4142135623731i)
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,291:
1 | -1.41421356i 1.41421356i |
+-------------------------------+</lang>
 
=={{header|Tcl}}==
{{tcllib|math::complexnumbers}}
10,327

edits