Roots of a quadratic function: Difference between revisions
Content added Content deleted
m (→version 1: added clarification to a comment in the REXX section header about roots close to zero.) |
m (Added the Sidef language) |
||
Line 1,952: | Line 1,952: | ||
<pre> |
<pre> |
||
X1 = 1000000.000000 X2 = 0.000001 |
X1 = 1000000.000000 X2 = 0.000001 |
||
</pre> |
|||
=={{header|Sidef}}== |
|||
<lang ruby>var sets = [ |
|||
[1, 2, 1], |
|||
[1, 2, 3], |
|||
[1, -2, 1], |
|||
[1, 0, -4], |
|||
[1, -1e6, 1], |
|||
]; |
|||
func quadroots(a, b, c) { |
|||
var root = ( |
|||
(b**2 - 4*a*c) -> complex.sqrt |
|||
); |
|||
a.complex!; |
|||
b.complex!; |
|||
[(-b + root) / (2 * a), |
|||
(-b - root) / (2 * a)]; |
|||
} |
|||
sets.each { |coefficients| |
|||
say ("Roots for #{coefficients.dump}", |
|||
"=> (#{quadroots(coefficients...).join(', ')})"); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Roots for [1, 2, 1]=> (-1, -1) |
|||
Roots for [1, 2, 3]=> (-1+1.41421356237309504880168872420969807857i, -1-1.41421356237309504880168872420969807857i) |
|||
Roots for [1, -2, 1]=> (1, 1) |
|||
Roots for [1, 0, -4]=> (2, -2) |
|||
Roots for [1, -1000000, 1]=> (999999.999998999999999998999999999998, 0.000001000000000001000000000002) |
|||
</pre> |
</pre> |
||