Roots of a quadratic function: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Undo revision 328301 by Thundergnat (talk) Wrong page contents) Tag: Undo |
Thundergnat (talk | contribs) m (Fix syntax highlighting... with the correct page contents this time) |
||
Line 7: | Line 7: | ||
(For double-precision floats, set <math>b = -10^9</math>.) |
(For double-precision floats, set <math>b = -10^9</math>.) |
||
Consider the following implementation in [[Ada]]: |
Consider the following implementation in [[Ada]]: |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; |
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; |
||
Line 22: | Line 22: | ||
begin |
begin |
||
Put_Line ("X1 =" & Float'Image (R (1)) & " X2 =" & Float'Image (R (2))); |
Put_Line ("X1 =" & Float'Image (R (1)) & " X2 =" & Float'Image (R (2))); |
||
end Quadratic_Equation;</ |
end Quadratic_Equation;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>X1 = 1.00000E+06 X2 = 0.00000E+00</pre> |
<pre>X1 = 1.00000E+06 X2 = 0.00000E+00</pre> |
||
Line 36: | Line 36: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">F quad_roots(a, b, c) |
||
V sqd = Complex(b^2 - 4*a*c) ^ 0.5 |
V sqd = Complex(b^2 - 4*a*c) ^ 0.5 |
||
R ((-b + sqd) / (2 * a), |
R ((-b + sqd) / (2 * a), |
||
Line 50: | Line 50: | ||
V (r1, r2) = quad_roots(a, b, c) |
V (r1, r2) = quad_roots(a, b, c) |
||
print(r1, end' ‘ ’) |
print(r1, end' ‘ ’) |
||
print(r2)</ |
print(r2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 62: | Line 62: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; |
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; |
||
Line 83: | Line 83: | ||
begin |
begin |
||
Put_Line ("X1 =" & Float'Image (R (1)) & " X2 =" & Float'Image (R (2))); |
Put_Line ("X1 =" & Float'Image (R (1)) & " X2 =" & Float'Image (R (2))); |
||
end Quadratic_Equation;</ |
end Quadratic_Equation;</syntaxhighlight> |
||
Here precision loss is prevented by checking signs of operands. On errors, Constraint_Error is propagated on numeric errors and when roots are complex. |
Here precision loss is prevented by checking signs of operands. On errors, Constraint_Error is propagated on numeric errors and when roots are complex. |
||
{{out}} |
{{out}} |
||
Line 97: | Line 97: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - probably need to "USE" the compl ENVIRON}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - probably need to "USE" the compl ENVIRON}} |
||
< |
<syntaxhighlight lang="algol68">quadratic equation: |
||
BEGIN |
BEGIN |
||
Line 151: | Line 151: | ||
ESAC |
ESAC |
||
OD |
OD |
||
END # quadratic_equation #</ |
END # quadratic_equation #</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 175: | Line 175: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?p=276617#276617 discussion] |
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?p=276617#276617 discussion] |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % quadratic(u,v, 1,-3,2) ", " u ", " v |
||
MsgBox % quadratic(u,v, 1,3,2) ", " u ", " v |
MsgBox % quadratic(u,v, 1,3,2) ", " u ", " v |
||
MsgBox % quadratic(u,v, -2,4,-2) ", " u ", " v |
MsgBox % quadratic(u,v, -2,4,-2) ", " u ", " v |
||
Line 197: | Line 197: | ||
x2 := c/a/x1 |
x2 := c/a/x1 |
||
Return 2 |
Return 2 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> FOR test% = 1 TO 7 |
||
READ a$, b$, c$ |
READ a$, b$, c$ |
||
PRINT "For a = " ; a$ ", b = " ; b$ ", c = " ; c$ TAB(32) ; |
PRINT "For a = " ; a$ ", b = " ; b$ ", c = " ; c$ TAB(32) ; |
||
Line 227: | Line 227: | ||
PRINT "the complex roots are " ; -b/2/a " +/- " ; SQR(-d)/2/a "*i" |
PRINT "the complex roots are " ; -b/2/a " +/- " ; SQR(-d)/2/a "*i" |
||
ENDCASE |
ENDCASE |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>For a = 1, b = -1E9, c = 1 the real roots are 1E9 and 1E-9 |
<pre>For a = 1, b = -1E9, c = 1 the real roots are 1E9 and 1E-9 |
||
Line 239: | Line 239: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Code that tries to avoid floating point overflow and other unfortunate loss of precissions: (compiled with <code>gcc -std=c99</code> for <code>complex</code>, though easily adapted to just real numbers) |
Code that tries to avoid floating point overflow and other unfortunate loss of precissions: (compiled with <code>gcc -std=c99</code> for <code>complex</code>, though easily adapted to just real numbers) |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <complex.h> |
#include <complex.h> |
||
Line 299: | Line 299: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}}<pre>(-1e+12 + 0 i), (-1 + 0 i) |
{{out}}<pre>(-1e+12 + 0 i), (-1 + 0 i) |
||
(1.00208e+07 + 0 i), (9.9792e-08 + 0 i)</pre> |
(1.00208e+07 + 0 i), (9.9792e-08 + 0 i)</pre> |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
#include <complex.h> |
#include <complex.h> |
||
Line 314: | Line 314: | ||
x[0] = (-b + csqrt(delta)) / (2.0*a); |
x[0] = (-b + csqrt(delta)) / (2.0*a); |
||
x[1] = (-b - csqrt(delta)) / (2.0*a); |
x[1] = (-b - csqrt(delta)) / (2.0*a); |
||
}</ |
}</syntaxhighlight> |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="c">void roots_quadratic_eq2(double a, double b, double c, complex double *x) |
||
{ |
{ |
||
b /= a; |
b /= a; |
||
Line 330: | Line 330: | ||
x[1] = c/sol; |
x[1] = c/sol; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="c">int main() |
||
{ |
{ |
||
complex double x[2]; |
complex double x[2]; |
||
Line 346: | Line 346: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<pre>x1 = (1.00000000000000000000e+20, 0.00000000000000000000e+00) |
<pre>x1 = (1.00000000000000000000e+20, 0.00000000000000000000e+00) |
||
Line 355: | Line 355: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Numerics; |
using System.Numerics; |
||
Line 370: | Line 370: | ||
Console.WriteLine(Solve(1, -1E20, 1)); |
Console.WriteLine(Solve(1, -1E20, 1)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>((1E+20, 0), (1E-20, 0))</pre> |
<pre>((1E+20, 0), (1E-20, 0))</pre> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <utility> |
#include <utility> |
||
#include <complex> |
#include <complex> |
||
Line 402: | Line 402: | ||
std::pair<complex, complex> result = solve_quadratic_equation(1, -1e20, 1); |
std::pair<complex, complex> result = solve_quadratic_equation(1, -1e20, 1); |
||
std::cout << result.first << ", " << result.second << std::endl; |
std::cout << result.first << ", " << result.second << std::endl; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
(1e+20,0), (1e-20,0) |
(1e+20,0), (1e-20,0) |
||
Line 408: | Line 408: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn quadratic |
||
"Compute the roots of a quadratic in the form ax^2 + bx + c = 1. |
"Compute the roots of a quadratic in the form ax^2 + bx + c = 1. |
||
Returns any of nil, a float, or a vector." |
Returns any of nil, a float, or a vector." |
||
Line 418: | Line 418: | ||
(zero? sq-d) (f +) |
(zero? sq-d) (f +) |
||
(pos? sq-d) [(f +) (f -)] |
(pos? sq-d) [(f +) (f -)] |
||
:else nil))) ; maybe our number ended up as NaN</ |
:else nil))) ; maybe our number ended up as NaN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="clojure">user=> (quadratic 1.0 1.0 1.0) |
||
nil |
nil |
||
user=> (quadratic 1.0 2.0 1.0) |
user=> (quadratic 1.0 2.0 1.0) |
||
Line 427: | Line 427: | ||
user=> (quadratic 1.0 3.0 1.0) |
user=> (quadratic 1.0 3.0 1.0) |
||
[2.618033988749895 0.3819660112501051] |
[2.618033988749895 0.3819660112501051] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun quadratic (a b c) |
||
(list |
(list |
||
(/ (+ (- b) (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a)) |
(/ (+ (- b) (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a)) |
||
(/ (- (- b) (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a))))</ |
(/ (- (- b) (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.math, std.traits; |
||
CommonType!(T1, T2, T3)[] naiveQR(T1, T2, T3) |
CommonType!(T1, T2, T3)[] naiveQR(T1, T2, T3) |
||
Line 482: | Line 482: | ||
writefln(" Naive: [%(%g, %)]", naiveQR(1.0L, -10e5L, 1.0L)); |
writefln(" Naive: [%(%g, %)]", naiveQR(1.0L, -10e5L, 1.0L)); |
||
writefln("Cautious: [%(%g, %)]", cautiQR(1.0L, -10e5L, 1.0L)); |
writefln("Cautious: [%(%g, %)]", cautiQR(1.0L, -10e5L, 1.0L)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>With 32 bit float type: |
<pre>With 32 bit float type: |
||
Line 500: | Line 500: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Quadratic do |
||
def roots(a, b, c) do |
def roots(a, b, c) do |
||
IO.puts "Roots of a quadratic function (#{a}, #{b}, #{c})" |
IO.puts "Roots of a quadratic function (#{a}, #{b}, #{c})" |
||
Line 523: | Line 523: | ||
Quadratic.roots(1, -1.0e10, 1) |
Quadratic.roots(1, -1.0e10, 1) |
||
Quadratic.roots(1, 2, 3) |
Quadratic.roots(1, 2, 3) |
||
Quadratic.roots(2, -1, -6)</ |
Quadratic.roots(2, -1, -6)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 542: | Line 542: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<lang>PROGRAM QUADRATIC |
<syntaxhighlight lang="text">PROGRAM QUADRATIC |
||
PROCEDURE SOLVE_QUADRATIC |
PROCEDURE SOLVE_QUADRATIC |
||
Line 575: | Line 575: | ||
DATA(1,3,2) |
DATA(1,3,2) |
||
DATA(3,4,5) |
DATA(3,4,5) |
||
END PROGRAM</ |
END PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>For a= 1 ,b=-1E+09 ,c= 1 the real roots are 1E+09 and 1E-09 |
<pre>For a= 1 ,b=-1E+09 ,c= 1 the real roots are 1E+09 and 1E-09 |
||
Line 588: | Line 588: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
< |
<syntaxhighlight lang="factor">:: quadratic-equation ( a b c -- x1 x2 ) |
||
b sq a c * 4 * - sqrt :> sd |
b sq a c * 4 * - sqrt :> sd |
||
b 0 < |
b 0 < |
||
[ b neg sd + a 2 * / ] |
[ b neg sd + a 2 * / ] |
||
[ b neg sd - a 2 * / ] if :> x |
[ b neg sd - a 2 * / ] if :> x |
||
x c a x * / ;</ |
x c a x * / ;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="factor">( scratchpad ) 1 -1.e20 1 quadratic-equation |
||
--- Data stack: |
--- Data stack: |
||
1.0e+20 |
1.0e+20 |
||
9.999999999999999e-21</ |
9.999999999999999e-21</syntaxhighlight> |
||
Middlebrook method |
Middlebrook method |
||
< |
<syntaxhighlight lang="factor">:: quadratic-equation2 ( a b c -- x1 x2 ) |
||
a c * sqrt b / :> q |
a c * sqrt b / :> q |
||
1 4 q sq * - sqrt 0.5 * 0.5 + :> f |
1 4 q sq * - sqrt 0.5 * 0.5 + :> f |
||
b neg a / f * c neg b / f / ; |
b neg a / f * c neg b / f / ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="factor">( scratchpad ) 1 -1.e20 1 quadratic-equation |
||
--- Data stack: |
--- Data stack: |
||
1.0e+20 |
1.0e+20 |
||
1.0e-20</ |
1.0e-20</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Without locals: |
Without locals: |
||
< |
<syntaxhighlight lang="forth">: quadratic ( fa fb fc -- r1 r2 ) |
||
frot frot |
frot frot |
||
( c a b ) |
( c a b ) |
||
Line 627: | Line 627: | ||
2e f/ fover f/ |
2e f/ fover f/ |
||
( c a r1 ) |
( c a r1 ) |
||
frot frot f/ fover f/ ;</ |
frot frot f/ fover f/ ;</syntaxhighlight> |
||
With locals: |
With locals: |
||
< |
<syntaxhighlight lang="forth">: quadratic { F: a F: b F: c -- r1 r2 } |
||
b b f* 4e a f* c f* f- |
b b f* 4e a f* c f* f- |
||
fdup f0< if abort" imaginary roots" then |
fdup f0< if abort" imaginary roots" then |
||
Line 638: | Line 638: | ||
\ test |
\ test |
||
1 set-precision |
1 set-precision |
||
1e -1e6 1e quadratic fs. fs. \ 1e-6 1e6</ |
1e -1e6 1e quadratic fs. fs. \ 1e-6 1e6</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
===Fortran 90=== |
===Fortran 90=== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">PROGRAM QUADRATIC |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
Line 676: | Line 676: | ||
WRITE(*,*) "The roots are complex:" |
WRITE(*,*) "The roots are complex:" |
||
WRITE(*,"(2(A,2E23.15,A))") "Root1 = ", croot1, "j ", " Root2 = ", croot2, "j" |
WRITE(*,"(2(A,2E23.15,A))") "Root1 = ", croot1, "j ", " Root2 = ", croot2, "j" |
||
END IF</ |
END IF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Coefficients are: a = 0.300000000000000E+01 b = 0.400000000000000E+01 c = 0.133333333330000E+01 |
Coefficients are: a = 0.300000000000000E+01 b = 0.400000000000000E+01 c = 0.133333333330000E+01 |
||
Line 696: | Line 696: | ||
===Fortran I=== |
===Fortran I=== |
||
Source code written in FORTRAN I (october 1956) for the IBM 704. |
Source code written in FORTRAN I (october 1956) for the IBM 704. |
||
< |
<syntaxhighlight lang="fortran"> |
||
COMPUTE ROOTS OF A QUADRATIC FUNCTION - 1956 |
COMPUTE ROOTS OF A QUADRATIC FUNCTION - 1956 |
||
READ 100,A,B,C |
READ 100,A,B,C |
||
Line 724: | Line 724: | ||
332 FORMAT(3HX1=,E12.5,4H,X2=,E12.5) |
332 FORMAT(3HX1=,E12.5,4H,X2=,E12.5) |
||
999 STOP |
999 STOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{libheader|GMP}} |
{{libheader|GMP}} |
||
< |
<syntaxhighlight lang="freebasic">' version 20-12-2020 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 853: | Line 853: | ||
Data 1, -1e100, 1 |
Data 1, -1e100, 1 |
||
Data 1, -1e200, 1 |
Data 1, -1e200, 1 |
||
Data 1, -1e300, 1</ |
Data 1, -1e300, 1</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: is the naieve way |
<pre>1: is the naieve way |
||
Line 910: | Line 910: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">QuadraticRoots := function(a, b, c) |
||
local d; |
local d; |
||
d := Sqrt(b*b - 4*a*c); |
d := Sqrt(b*b - 4*a*c); |
||
Line 923: | Line 923: | ||
# This works also with floating-point numbers |
# This works also with floating-point numbers |
||
QuadraticRoots(2.0, 2.0, -1.0); |
QuadraticRoots(2.0, 2.0, -1.0); |
||
# [ 0.366025, -1.36603 ]</ |
# [ 0.366025, -1.36603 ]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 982: | Line 982: | ||
test(c[0], c[1], c[2]) |
test(c[0], c[1], c[2]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 993: | Line 993: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Complex (Complex, realPart) |
||
type CD = Complex Double |
type CD = Complex Double |
||
Line 1,019: | Line 1,019: | ||
(1, -10e5, 1), |
(1, -10e5, 1), |
||
(1, -10e9, 1) |
(1, -10e9, 1) |
||
]</ |
]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>((-0.6666666666666666) :+ 0.0,(-0.6666666666666666) :+ 0.0) |
<pre>((-0.6666666666666666) :+ 0.0,(-0.6666666666666666) :+ 0.0) |
||
Line 1,032: | Line 1,032: | ||
Works in both languages. |
Works in both languages. |
||
< |
<syntaxhighlight lang="unicon">procedure main() |
||
solve(1.0, -10.0e5, 1.0) |
solve(1.0, -10.0e5, 1.0) |
||
end |
end |
||
Line 1,041: | Line 1,041: | ||
else [r1 := (-b-d)/(2.0*a), c/(a*r1)] |
else [r1 := (-b-d)/(2.0*a), c/(a*r1)] |
||
write(a,"*x^2 + ",b,"*x + ",c," has roots ",roots[1]," and ",roots[2]) |
write(a,"*x^2 + ",b,"*x + ",c," has roots ",roots[1]," and ",roots[2]) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,051: | Line 1,051: | ||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
< |
<syntaxhighlight lang="idl">compile_OPT IDL2 |
||
print, "input a, press enter, input b, press enter, input c, press enter" |
print, "input a, press enter, input b, press enter, input c, press enter" |
||
Line 1,067: | Line 1,067: | ||
e= y/z |
e= y/z |
||
print, d,e</ |
print, d,e</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
<syntaxhighlight lang="is-basic"> |
|||
<lang IS-BASIC> |
|||
100 PROGRAM "Quadratic.bas" |
100 PROGRAM "Quadratic.bas" |
||
110 PRINT "Enter coefficients a, b and c:":INPUT PROMPT "a= ,b= ,c= ":A,B,C |
110 PRINT "Enter coefficients a, b and c:":INPUT PROMPT "a= ,b= ,c= ":A,B,C |
||
Line 1,085: | Line 1,085: | ||
220 PRINT "The complex roots are ";-B/2/A;"+/- ";STR$(SQR(-D)/2/A);"*i" |
220 PRINT "The complex roots are ";-B/2/A;"+/- ";STR$(SQR(-D)/2/A);"*i" |
||
230 END SELECT |
230 END SELECT |
||
240 END IF</ |
240 END IF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 1,095: | Line 1,095: | ||
'''Example''' using inputs from other solutions and the unstable example from the task description: |
'''Example''' using inputs from other solutions and the unstable example from the task description: |
||
< |
<syntaxhighlight lang="j"> coeff =. _3 |.\ 3 4 4r3 3 2 _1 3 2 1 1 _1e6 1 1 _1e9 1 |
||
> {:"1 p. coeff |
> {:"1 p. coeff |
||
_0.666667 _0.666667 |
_0.666667 _0.666667 |
||
Line 1,101: | Line 1,101: | ||
_0.333333j0.471405 _0.333333j_0.471405 |
_0.333333j0.471405 _0.333333j_0.471405 |
||
1e6 1e_6 |
1e6 1e_6 |
||
1e9 1e_9</ |
1e9 1e_9</syntaxhighlight> |
||
Of course <code>p.</code> generalizes to polynomials of arbitrary order (which isn't as great as that might sound, because of practical limitations). Given the coefficients <code>p.</code> returns the multiplier and roots of the polynomial. Given the multiplier and roots it returns the coefficients. For example using the cubic <math>0 + 16x - 12x^2 + 2x^3</math>: |
Of course <code>p.</code> generalizes to polynomials of arbitrary order (which isn't as great as that might sound, because of practical limitations). Given the coefficients <code>p.</code> returns the multiplier and roots of the polynomial. Given the multiplier and roots it returns the coefficients. For example using the cubic <math>0 + 16x - 12x^2 + 2x^3</math>: |
||
< |
<syntaxhighlight lang="j"> p. 0 16 _12 2 NB. return multiplier ; roots |
||
+-+-----+ |
+-+-----+ |
||
|2|4 2 0| |
|2|4 2 0| |
||
+-+-----+ |
+-+-----+ |
||
p. 2 ; 4 2 0 NB. return coefficients |
p. 2 ; 4 2 0 NB. return coefficients |
||
0 16 _12 2</ |
0 16 _12 2</syntaxhighlight> |
||
Exploring the limits of precision: |
Exploring the limits of precision: |
||
< |
<syntaxhighlight lang="j"> 1{::p. 1 _1e5 1 NB. display roots |
||
100000 1e_5 |
100000 1e_5 |
||
1 _1e5 1 p. 1{::p. 1 _1e5 1 NB. test roots |
1 _1e5 1 p. 1{::p. 1 _1e5 1 NB. test roots |
||
Line 1,122: | Line 1,122: | ||
1e_5 _1e_15 |
1e_5 _1e_15 |
||
1 _1e5 1 p. 1e5 1e_5-1e_5 _1e_15 NB. test displayed roots with adjustment |
1 _1e5 1 p. 1e5 1e_5-1e_5 _1e_15 NB. test displayed roots with adjustment |
||
_3.38436e_7 0</ |
_3.38436e_7 0</syntaxhighlight> |
||
When these "roots" are plugged back into the original polynomial, the results are nowhere near zero. However, double precision floating point does not have enough bits to represent the (extremely close) answers that would give a zero result. |
When these "roots" are plugged back into the original polynomial, the results are nowhere near zero. However, double precision floating point does not have enough bits to represent the (extremely close) answers that would give a zero result. |
||
Line 1,128: | Line 1,128: | ||
Middlebrook formula implemented in J |
Middlebrook formula implemented in J |
||
< |
<syntaxhighlight lang="j">q_r=: verb define |
||
'a b c' =. y |
'a b c' =. y |
||
q=. b %~ %: a * c |
q=. b %~ %: a * c |
||
Line 1,136: | Line 1,136: | ||
q_r 1 _1e6 1 |
q_r 1 _1e6 1 |
||
1e6 1e_6</ |
1e6 1e_6</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class QuadraticRoots { |
||
private static class Complex { |
private static class Complex { |
||
double re, im; |
double re, im; |
||
Line 1,212: | Line 1,212: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,250: | Line 1,250: | ||
'''Section 1''': Complex numbers (scrolling window) |
'''Section 1''': Complex numbers (scrolling window) |
||
<div style="overflow:scroll; height:200px;"> |
<div style="overflow:scroll; height:200px;"> |
||
< |
<syntaxhighlight lang="jq"># Complex numbers as points [x,y] in the Cartesian plane |
||
def real(z): if (z|type) == "number" then z else z[0] end; |
def real(z): if (z|type) == "number" then z else z[0] end; |
||
Line 1,315: | Line 1,315: | ||
| [ ($r * cos), ($r * sin)] |
| [ ($r * cos), ($r * sin)] |
||
end |
end |
||
end ;</ |
end ;</syntaxhighlight></div> |
||
'''Section 2:''' quadratic_roots(a;b;c) |
'''Section 2:''' quadratic_roots(a;b;c) |
||
< |
<syntaxhighlight lang="jq"># If there are infinitely many solutions, emit true; |
||
# if none, emit empty; |
# if none, emit empty; |
||
# otherwise always emit two. |
# otherwise always emit two. |
||
Line 1,333: | Line 1,333: | ||
negate(divide(c; multiply(b; $f))) |
negate(divide(c; multiply(b; $f))) |
||
end |
end |
||
;</ |
;</syntaxhighlight> |
||
'''Section 3''': |
'''Section 3''': |
||
Produce a table showing [i, error, solution] for solutions to x^2 - 10^i + 1 = 0 |
Produce a table showing [i, error, solution] for solutions to x^2 - 10^i + 1 = 0 |
||
< |
<syntaxhighlight lang="jq">def example: |
||
def pow(i): . as $in | reduce range(0;i) as $i (1; . * $in); |
def pow(i): . as $in | reduce range(0;i) as $i (1; . * $in); |
||
def poly(a;b;c): plus( plus( multiply(a; multiply(.;.)); multiply(b;.)); c); |
def poly(a;b;c): plus( plus( multiply(a; multiply(.;.)); multiply(b;.)); c); |
||
Line 1,354: | Line 1,354: | ||
; |
; |
||
example</ |
example</syntaxhighlight> |
||
{{Out}} (scrolling window) |
{{Out}} (scrolling window) |
||
<div style="overflow:scroll; height:200px;"> |
<div style="overflow:scroll; height:200px;"> |
||
<syntaxhighlight lang="sh"> |
|||
<lang sh> |
|||
$ jq -M -r -n -f Roots_of_a_quadratic_function.jq |
$ jq -M -r -n -f Roots_of_a_quadratic_function.jq |
||
0: error = +0 x=[0.5,0.8660254037844386] |
0: error = +0 x=[0.5,0.8660254037844386] |
||
Line 1,384: | Line 1,384: | ||
11: error = +0 x=[1e-11,-0] |
11: error = +0 x=[1e-11,-0] |
||
12: error = 1 x=[1000000000000,0] |
12: error = 1 x=[1000000000000,0] |
||
12: error = +0 x=[1e-12,-0]</ |
12: error = +0 x=[1e-12,-0]</syntaxhighlight></div> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 1,391: | Line 1,391: | ||
Alternative solutions might make use of Julia's Polynomials or Roots packages. |
Alternative solutions might make use of Julia's Polynomials or Roots packages. |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
function quadroots(x::Real, y::Real, z::Real) |
function quadroots(x::Real, y::Real, z::Real) |
||
Line 1,415: | Line 1,415: | ||
for (x, y, z) in zip(a, b, c) |
for (x, y, z) in zip(a, b, c) |
||
@printf "The roots of %.2fx² + %.2fx + %.2f\n\tx₀ = (%s)\n" x y z join(round.(quadroots(x, y, z), 2), ", ") |
@printf "The roots of %.2fx² + %.2fx + %.2f\n\tx₀ = (%s)\n" x y z join(round.(quadroots(x, y, z), 2), ", ") |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,429: | Line 1,429: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">import java.lang.Math.* |
||
data class Equation(val a: Double, val b: Double, val c: Double) { |
data class Equation(val a: Double, val b: Double, val c: Double) { |
||
Line 1,472: | Line 1,472: | ||
equations.forEach { println("$it\n" + it.quadraticRoots) } |
equations.forEach { println("$it\n" + it.quadraticRoots) } |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Equation(a=1.0, b=22.0, c=-1323.0) |
<pre>Equation(a=1.0, b=22.0, c=-1323.0) |
||
Line 1,488: | Line 1,488: | ||
=={{header|lambdatalk}}== |
=={{header|lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
1) using lambdas: |
1) using lambdas: |
||
Line 1,550: | Line 1,550: | ||
x1 = 1 |
x1 = 1 |
||
x2 = 1 |
x2 = 1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">a=1:b=2:c=3 |
||
'assume a<>0 |
'assume a<>0 |
||
print quad$(a,b,c) |
print quad$(a,b,c) |
||
Line 1,566: | Line 1,566: | ||
quad$=str$(x/(2*a)+sqr(D)/(2*a));" , ";str$(x/(2*a)-sqr(D)/(2*a)) |
quad$=str$(x/(2*a)+sqr(D)/(2*a));" , ";str$(x/(2*a)-sqr(D)/(2*a)) |
||
end if |
end if |
||
end function</ |
end function</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">to quadratic :a :b :c |
||
localmake "d sqrt (:b*:b - 4*:a*:c) |
localmake "d sqrt (:b*:b - 4*:a*:c) |
||
if :b < 0 [make "d minus :d] |
if :b < 0 [make "d minus :d] |
||
output list (:d-:b)/(2*:a) (2*:c)/(:d-:b) |
output list (:d-:b)/(2*:a) (2*:c)/(:d-:b) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 1,579: | Line 1,579: | ||
like that from the Complex Numbers article. However, this should be enough to demonstrate its accuracy: |
like that from the Complex Numbers article. However, this should be enough to demonstrate its accuracy: |
||
< |
<syntaxhighlight lang="lua">function qsolve(a, b, c) |
||
if b < 0 then return qsolve(-a, -b, -c) end |
if b < 0 then return qsolve(-a, -b, -c) end |
||
val = b + (b^2 - 4*a*c)^(1/2) --this never exhibits instability if b > 0 |
val = b + (b^2 - 4*a*c)^(1/2) --this never exhibits instability if b > 0 |
||
Line 1,587: | Line 1,587: | ||
for i = 1, 12 do |
for i = 1, 12 do |
||
print(qsolve(1, 0-10^i, 1)) |
print(qsolve(1, 0-10^i, 1)) |
||
end</ |
end</syntaxhighlight> |
||
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. |
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}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">solve(a*x^2+b*x+c,x); |
||
solve(1.0*x^2-10.0^9*x+1.0,x,explicit,allsolutions); |
solve(1.0*x^2-10.0^9*x+1.0,x,explicit,allsolutions); |
||
fsolve(x^2-10^9*x+1,x,complex);</ |
fsolve(x^2-10^9*x+1,x,complex);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> (1/2) (1/2) |
<pre> (1/2) (1/2) |
||
Line 1,611: | Line 1,611: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Possible ways to do this are (symbolic and numeric examples): |
Possible ways to do this are (symbolic and numeric examples): |
||
< |
<syntaxhighlight lang="mathematica">Solve[a x^2 + b x + c == 0, x] |
||
Solve[x^2 - 10^5 x + 1 == 0, x] |
Solve[x^2 - 10^5 x + 1 == 0, x] |
||
Root[#1^2 - 10^5 #1 + 1 &, 1] |
Root[#1^2 - 10^5 #1 + 1 &, 1] |
||
Line 1,619: | Line 1,619: | ||
FindInstance[x^2 - 10^5 x + 1 == 0, x, Reals, 2] |
FindInstance[x^2 - 10^5 x + 1 == 0, x, Reals, 2] |
||
FindRoot[x^2 - 10^5 x + 1 == 0, {x, 0}] |
FindRoot[x^2 - 10^5 x + 1 == 0, {x, 0}] |
||
FindRoot[x^2 - 10^5 x + 1 == 0, {x, 10^6}]</ |
FindRoot[x^2 - 10^5 x + 1 == 0, {x, 10^6}]</syntaxhighlight> |
||
gives back: |
gives back: |
||
Line 1,659: | Line 1,659: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">roots([1 -3 2]) % coefficients in decreasing order of power e.g. [x^n ... x^2 x^1 x^0]</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">solve(a*x^2 + b*x + c = 0, x); |
||
/* 2 2 |
/* 2 2 |
||
Line 1,677: | Line 1,677: | ||
bfloat(%); |
bfloat(%); |
||
/* [x = 1.0000000000000000009999920675269450501b-9, |
/* [x = 1.0000000000000000009999920675269450501b-9, |
||
x = 9.99999999999999998999999999999999999b8] */</ |
x = 9.99999999999999998999999999999999999b8] */</syntaxhighlight> |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>П2 С/П /-/ <-> / 2 / П3 x^2 С/П |
<syntaxhighlight lang="text">П2 С/П /-/ <-> / 2 / П3 x^2 С/П |
||
ИП2 / - Вx <-> КвКор НОП x>=0 28 ИП3 |
ИП2 / - Вx <-> КвКор НОП x>=0 28 ИП3 |
||
x<0 24 <-> /-/ + / Вx С/П /-/ КвКор |
x<0 24 <-> /-/ + / Вx С/П /-/ КвКор |
||
ИП3 С/П</ |
ИП3 С/П</syntaxhighlight> |
||
''Input:'' a С/П b С/П c С/П |
''Input:'' a С/П b С/П c С/П |
||
Line 1,691: | Line 1,691: | ||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
< |
<syntaxhighlight lang="modula3">MODULE Quad EXPORTS Main; |
||
IMPORT IO, Fmt, Math; |
IMPORT IO, Fmt, Math; |
||
Line 1,715: | Line 1,715: | ||
r := Solve(1.0D0, -10.0D5, 1.0D0); |
r := Solve(1.0D0, -10.0D5, 1.0D0); |
||
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.</ |
END Quad.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import math, complex, strformat |
||
const Epsilon = 1e-15 |
const Epsilon = 1e-15 |
||
Line 1,774: | Line 1,774: | ||
let roots = quadRoots(a, b, c) |
let roots = quadRoots(a, b, c) |
||
let plural = if roots.kind == solDouble: "" else: "s" |
let plural = if roots.kind == solDouble: "" else: "s" |
||
echo &" root{plural}: {roots}"</ |
echo &" root{plural}: {roots}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,790: | Line 1,790: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">type quadroots = |
||
| RealRoots of float * float |
| RealRoots of float * float |
||
| ComplexRoots of Complex.t * Complex.t ;; |
| ComplexRoots of Complex.t * Complex.t ;; |
||
Line 1,809: | Line 1,809: | ||
in |
in |
||
RealRoots (r, c /. (r *. a)) |
RealRoots (r, c /. (r *. a)) |
||
;;</ |
;;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="ocaml"># quadsolve 1.0 0.0 (-2.0) ;; |
||
- : quadroots = RealRoots (-1.4142135623730951, 1.4142135623730949) |
- : quadroots = RealRoots (-1.4142135623730951, 1.4142135623730949) |
||
Line 1,821: | Line 1,821: | ||
# quadsolve 1.0 (-1.0e5) 1.0 ;; |
# quadsolve 1.0 (-1.0e5) 1.0 ;; |
||
- : quadroots = RealRoots (99999.99999, 1.0000000001000001e-005)</ |
- : quadroots = RealRoots (99999.99999, 1.0000000001000001e-005)</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
Line 1,828: | Line 1,828: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
{{works with|PARI/GP|2.8.0+}} |
{{works with|PARI/GP|2.8.0+}} |
||
< |
<syntaxhighlight lang="parigp">roots(a,b,c)=polrootsreal(Pol([a,b,c]))</syntaxhighlight> |
||
{{trans|C}} |
{{trans|C}} |
||
Otherwise, coding directly: |
Otherwise, coding directly: |
||
< |
<syntaxhighlight lang="parigp">roots(a,b,c)={ |
||
b /= a; |
b /= a; |
||
c /= a; |
c /= a; |
||
Line 1,842: | Line 1,842: | ||
[sol,c/sol] |
[sol,c/sol] |
||
) |
) |
||
};</ |
};</syntaxhighlight> |
||
Either way, |
Either way, |
||
<lang |
<syntaxhighlight lang="parigp">roots(1,-1e9,1)</syntaxhighlight> |
||
gives one root around 0.000000001000000000000000001 and one root around 999999999.999999999. |
gives one root around 0.000000001000000000000000001 and one root around 999999999.999999999. |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
some parts translated from Modula2 |
some parts translated from Modula2 |
||
< |
<syntaxhighlight lang="pascal">Program QuadraticRoots; |
||
var |
var |
||
Line 1,878: | Line 1,878: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,889: | Line 1,889: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
When using [http://perldoc.perl.org/Math/Complex.html Math::Complex] perl automatically convert numbers when necessary. |
When using [http://perldoc.perl.org/Math/Complex.html Math::Complex] perl automatically convert numbers when necessary. |
||
< |
<syntaxhighlight lang="perl">use Math::Complex; |
||
($x1,$x2) = solveQuad(1,2,3); |
($x1,$x2) = solveQuad(1,2,3); |
||
Line 1,900: | Line 1,900: | ||
my $root = sqrt($b**2 - 4*$a*$c); |
my $root = sqrt($b**2 - 4*$a*$c); |
||
return ( -$b + $root )/(2*$a), ( -$b - $root )/(2*$a); |
return ( -$b + $root )/(2*$a), ( -$b - $root )/(2*$a); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|ERRE}} |
{{trans|ERRE}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve_quadratic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">t3</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve_quadratic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">t3</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span> |
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span> |
||
Line 1,932: | Line 1,932: | ||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve_quadratic</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve_quadratic</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
<pre> |
<pre> |
||
for a=1,b=-1e+9,c=1 the real roots are 1e+9 and 1e-9 |
for a=1,b=-1e+9,c=1 the real roots are 1e+9 and 1e-9 |
||
Line 1,944: | Line 1,944: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(scl 40) |
||
(de solveQuad (A B C) |
(de solveQuad (A B C) |
||
Line 1,958: | Line 1,958: | ||
(mapcar round |
(mapcar round |
||
(solveQuad 1.0 -1000000.0 1.0) |
(solveQuad 1.0 -1000000.0 1.0) |
||
(6 .) )</ |
(6 .) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>-> ("999,999.999999" "0.000001")</pre> |
<pre>-> ("999,999.999999" "0.000001")</pre> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
declare (c1, c2) float complex, |
declare (c1, c2) float complex, |
||
(a, b, c, x1, x2) float; |
(a, b, c, x1, x2) float; |
||
Line 1,980: | Line 1,980: | ||
put data (x1, x2); |
put data (x1, x2); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{libheader|NumPy}} |
{{libheader|NumPy}} |
||
This solution compares the naïve method with three "better" methods. |
This solution compares the naïve method with three "better" methods. |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python3 |
||
import math |
import math |
||
Line 2,057: | Line 2,057: | ||
print('\nNumpy:') |
print('\nNumpy:') |
||
for c in testcases: |
for c in testcases: |
||
print(("{:.5} "*2).format(*numpy.roots(c)))</ |
print(("{:.5} "*2).format(*numpy.roots(c)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,098: | Line 2,098: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">qroots <- function(a, b, c) { |
||
r <- sqrt(b * b - 4 * a * c + 0i) |
r <- sqrt(b * b - 4 * a * c + 0i) |
||
if (abs(b - r) > abs(b + r)) { |
if (abs(b - r) > abs(b + r)) { |
||
Line 2,112: | Line 2,112: | ||
qroots(1, -1e9, 1) |
qroots(1, -1e9, 1) |
||
[1] 1e+09+0i 1e-09+0i</ |
[1] 1e+09+0i 1e-09+0i</syntaxhighlight> |
||
Using the builtin '''polyroot''' function (note the order of coefficients is reversed): |
Using the builtin '''polyroot''' function (note the order of coefficients is reversed): |
||
< |
<syntaxhighlight lang="r">polyroot(c(2i, 0, 1)) |
||
[1] -1+1i 1-1i |
[1] -1+1i 1-1i |
||
polyroot(c(1, -1e9, 1)) |
polyroot(c(1, -1e9, 1)) |
||
[1] 1e-09+0i 1e+09+0i</ |
[1] 1e-09+0i 1e+09+0i</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (quadratic a b c) |
(define (quadratic a b c) |
||
(let* ((-b (- b)) |
(let* ((-b (- b)) |
||
Line 2,135: | Line 2,135: | ||
;'(0.99999999999995 -1.00000000000005) |
;'(0.99999999999995 -1.00000000000005) |
||
;(quadratic 1 0.0000000000001 1) |
;(quadratic 1 0.0000000000001 1) |
||
;'(-5e-014+1.0i -5e-014-1.0i)</ |
;'(-5e-014+1.0i -5e-014-1.0i)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,142: | Line 2,142: | ||
Raku has complex number handling built in. |
Raku has complex number handling built in. |
||
<lang |
<syntaxhighlight lang="raku" line>for |
||
[1, 2, 1], |
[1, 2, 1], |
||
[1, 2, 3], |
[1, 2, 3], |
||
Line 2,158: | Line 2,158: | ||
given |
given |
||
($b ** 2 - 4 * $a * $c ).Complex.sqrt.narrow |
($b ** 2 - 4 * $a * $c ).Complex.sqrt.narrow |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Roots for 1, 2, 1 => (-1, -1) |
<pre>Roots for 1, 2, 1 => (-1, -1) |
||
Line 2,176: | Line 2,176: | ||
This REXX version supports ''complex numbers'' for the result. |
This REXX version supports ''complex numbers'' for the result. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds the roots (which may be complex) of a quadratic function. */ |
||
parse arg a b c . /*obtain the specified arguments: A B C*/ |
parse arg a b c . /*obtain the specified arguments: A B C*/ |
||
call quad a,b,c /*solve quadratic function via the sub.*/ |
call quad a,b,c /*solve quadratic function via the sub.*/ |
||
Line 2,200: | Line 2,200: | ||
g=g*.5'e'_%2; do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/ |
g=g*.5'e'_%2; do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/ |
||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/ |
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/ |
||
numeric digits d; return (g/1)left('i', ox<0) /*make complex if OX<0. */</ |
numeric digits d; return (g/1)left('i', ox<0) /*make complex if OX<0. */</syntaxhighlight> |
||
{{out|output|text= when using the input of: <tt> 1 -10e5 1 </tt>}} |
{{out|output|text= when using the input of: <tt> 1 -10e5 1 </tt>}} |
||
<pre> |
<pre> |
||
Line 2,252: | Line 2,252: | ||
=== Version 2 === |
=== Version 2 === |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* 26.07.2913 Walter Pachl |
* 26.07.2913 Walter Pachl |
||
**********************************************************************/ |
**********************************************************************/ |
||
Line 2,315: | Line 2,315: | ||
Return (r+0) |
Return (r+0) |
||
exit: Say arg(1) |
exit: Say arg(1) |
||
Exit</ |
Exit</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,333: | Line 2,333: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
x1 = 0 |
x1 = 0 |
||
x2 = 0 |
x2 = 0 |
||
Line 2,350: | Line 2,350: | ||
x2 = (-b - sqrtDiscriminant) / (2.0*a) |
x2 = (-b - sqrtDiscriminant) / (2.0*a) |
||
return [x1, x2] |
return [x1, x2] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.9.3+}} |
{{works with|Ruby|1.9.3+}} |
||
The CMath#sqrt method will return a Complex instance if necessary. |
The CMath#sqrt method will return a Complex instance if necessary. |
||
< |
<syntaxhighlight lang="ruby">require 'cmath' |
||
def quadratic(a, b, c) |
def quadratic(a, b, c) |
||
Line 2,369: | Line 2,369: | ||
p quadratic(-2, 7, 15) # [-3/2, 5] |
p quadratic(-2, 7, 15) # [-3/2, 5] |
||
p quadratic(1, -2, 1) # [1] |
p quadratic(1, -2, 1) # [1] |
||
p quadratic(1, 3, 3) # [(-3 + sqrt(3)i)/2), (-3 - sqrt(3)i)/2)]</ |
p quadratic(1, 3, 3) # [(-3 + sqrt(3)i)/2), (-3 - sqrt(3)i)/2)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,383: | Line 2,383: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">print "FOR 1,2,3 => ";quad$(1,2,3) |
||
print "FOR 4,5,6 => ";quad$(4,5,6) |
print "FOR 4,5,6 => ";quad$(4,5,6) |
||
Line 2,394: | Line 2,394: | ||
quad$ = str$(x/(2*a)+sqr(d)/(2*a))+" , "+str$(x/(2*a)-sqr(d)/(2*a)) |
quad$ = str$(x/(2*a)+sqr(d)/(2*a))+" , "+str$(x/(2*a)-sqr(d)/(2*a)) |
||
end if |
end if |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight><pre>FOR 1,2,3 => -1 +i1.41421356 , -1 -i1.41421356 |
||
FOR 4,5,6 => -0.625 +i1.05326872 , -0.625 -i1.05326872</pre> |
FOR 4,5,6 => -0.625 +i1.05326872 , -0.625 -i1.05326872</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Using [[Arithmetic/Complex#Scala|Complex]] class from task Arithmetic/Complex. |
Using [[Arithmetic/Complex#Scala|Complex]] class from task Arithmetic/Complex. |
||
< |
<syntaxhighlight lang="scala">import ArithmeticComplex._ |
||
object QuadraticRoots { |
object QuadraticRoots { |
||
def solve(a:Double, b:Double, c:Double)={ |
def solve(a:Double, b:Double, c:Double)={ |
||
Line 2,415: | Line 2,415: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Usage: |
Usage: |
||
< |
<syntaxhighlight lang="scala">val equations=Array( |
||
(1.0, 22.0, -1323.0), // two distinct real roots |
(1.0, 22.0, -1323.0), // two distinct real roots |
||
(6.0, -23.0, 20.0), // with a != 1.0 |
(6.0, -23.0, 20.0), // with a != 1.0 |
||
Line 2,433: | Line 2,433: | ||
if(roots._1 != roots._2) println("x2="+roots._2) |
if(roots._1 != roots._2) println("x2="+roots._2) |
||
println |
println |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>a=1.00000 b=22.0000 c=-1323.00 |
<pre>a=1.00000 b=22.0000 c=-1323.00 |
||
Line 2,459: | Line 2,459: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define (quadratic a b c) |
||
(if (= a 0) |
(if (= a 0) |
||
(if (= b 0) 'fail (- (/ c b))) |
(if (= b 0) 'fail (- (/ c b))) |
||
Line 2,498: | Line 2,498: | ||
(quadratic 1 -1e5 1) |
(quadratic 1 -1e5 1) |
||
; (99999.99999 1.0000000001000001e-05)</ |
; (99999.99999 1.0000000001000001e-05)</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
include "math.s7i"; |
include "math.s7i"; |
||
Line 2,536: | Line 2,536: | ||
r := solve(1.0, -10.0E5, 1.0); |
r := solve(1.0, -10.0E5, 1.0); |
||
writeln("X1 = " <& r.x1 digits 6 <& " X2 = " <& r.x2 digits 6); |
writeln("X1 = " <& r.x1 digits 6 <& " X2 = " <& r.x2 digits 6); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,544: | Line 2,544: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var sets = [ |
||
[1, 2, 1], |
[1, 2, 1], |
||
[1, 2, 3], |
[1, 2, 3], |
||
Line 2,562: | Line 2,562: | ||
say ("Roots for #{coefficients}", |
say ("Roots for #{coefficients}", |
||
"=> (#{quadroots(coefficients...).join(', ')})") |
"=> (#{quadroots(coefficients...).join(', ')})") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,573: | Line 2,573: | ||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">mata |
||
: polyroots((-2,0,1)) |
: polyroots((-2,0,1)) |
||
1 2 |
1 2 |
||
Line 2,584: | Line 2,584: | ||
+-------------------------------+ |
+-------------------------------+ |
||
1 | -1.41421356i 1.41421356i | |
1 | -1.41421356i 1.41421356i | |
||
+-------------------------------+</ |
+-------------------------------+</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{tcllib|math::complexnumbers}} |
{{tcllib|math::complexnumbers}} |
||
< |
<syntaxhighlight lang="tcl">package require math::complexnumbers |
||
namespace import math::complexnumbers::complex math::complexnumbers::tostring |
namespace import math::complexnumbers::complex math::complexnumbers::tostring |
||
Line 2,624: | Line 2,624: | ||
report_quad -2 7 15 ;# {5, -3/2} |
report_quad -2 7 15 ;# {5, -3/2} |
||
report_quad 1 -2 1 ;# {1} |
report_quad 1 -2 1 ;# {1} |
||
report_quad 1 3 3 ;# {(-3 - sqrt(3)i)/2), (-3 + sqrt(3)i)/2)}</ |
report_quad 1 3 3 ;# {(-3 - sqrt(3)i)/2), (-3 + sqrt(3)i)/2)}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3x**2 + 4x + 1.3333333333333333 = 0 |
<pre>3x**2 + 4x + 1.3333333333333333 = 0 |
||
Line 2,652: | Line 2,652: | ||
TI-89 BASIC has built-in numeric and algebraic solvers. |
TI-89 BASIC has built-in numeric and algebraic solvers. |
||
<lang>solve(x^2-1E9x+1.0)</ |
<syntaxhighlight lang="text">solve(x^2-1E9x+1.0)</syntaxhighlight> |
||
returns |
returns |
||
<pre>x=1.E-9 or x=1.E9</pre> |
<pre>x=1.E-9 or x=1.E9</pre> |
||
Line 2,659: | Line 2,659: | ||
{{trans|Go}} |
{{trans|Go}} |
||
{{libheader|Wren-complex}} |
{{libheader|Wren-complex}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/complex" for Complex |
||
var quadratic = Fn.new { |a, b, c| |
var quadratic = Fn.new { |a, b, c| |
||
Line 2,702: | Line 2,702: | ||
] |
] |
||
for (c in coeffs) test.call(c[0], c[1], c[2])</ |
for (c in coeffs) test.call(c[0], c[1], c[2])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,716: | Line 2,716: | ||
zkl doesn't have a complex number package. |
zkl doesn't have a complex number package. |
||
{{trans|Elixir}} |
{{trans|Elixir}} |
||
< |
<syntaxhighlight lang="zkl">fcn quadratic(a,b,c){ b=b.toFloat(); |
||
println("Roots of a quadratic function %s, %s, %s".fmt(a,b,c)); |
println("Roots of a quadratic function %s, %s, %s".fmt(a,b,c)); |
||
d,a2:=(b*b - 4*a*c), a+a; |
d,a2:=(b*b - 4*a*c), a+a; |
||
Line 2,728: | Line 2,728: | ||
println(" the complex roots are %s and \U00B1;%si".fmt(-b/a2,sd/a2)); |
println(" the complex roots are %s and \U00B1;%si".fmt(-b/a2,sd/a2)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">foreach a,b,c in (T( T(1,-2,1), T(1,-3,2), T(1,0,1), T(1,-1.0e10,1), T(1,2,3), T(2,-1,-6)) ){ |
||
quadratic(a,b,c) |
quadratic(a,b,c) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |