
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b + sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b + sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(/
(* -2.0 c)
(+
b
(/
(sqrt (+ (pow b 4.0) (* -16.0 (* (* c a) (* c a)))))
(hypot b (sqrt (* c (* 4.0 a))))))))
double code(double a, double b, double c) {
return (-2.0 * c) / (b + (sqrt((pow(b, 4.0) + (-16.0 * ((c * a) * (c * a))))) / hypot(b, sqrt((c * (4.0 * a))))));
}
public static double code(double a, double b, double c) {
return (-2.0 * c) / (b + (Math.sqrt((Math.pow(b, 4.0) + (-16.0 * ((c * a) * (c * a))))) / Math.hypot(b, Math.sqrt((c * (4.0 * a))))));
}
def code(a, b, c): return (-2.0 * c) / (b + (math.sqrt((math.pow(b, 4.0) + (-16.0 * ((c * a) * (c * a))))) / math.hypot(b, math.sqrt((c * (4.0 * a))))))
function code(a, b, c) return Float64(Float64(-2.0 * c) / Float64(b + Float64(sqrt(Float64((b ^ 4.0) + Float64(-16.0 * Float64(Float64(c * a) * Float64(c * a))))) / hypot(b, sqrt(Float64(c * Float64(4.0 * a))))))) end
function tmp = code(a, b, c) tmp = (-2.0 * c) / (b + (sqrt(((b ^ 4.0) + (-16.0 * ((c * a) * (c * a))))) / hypot(b, sqrt((c * (4.0 * a)))))); end
code[a_, b_, c_] := N[(N[(-2.0 * c), $MachinePrecision] / N[(b + N[(N[Sqrt[N[(N[Power[b, 4.0], $MachinePrecision] + N[(-16.0 * N[(N[(c * a), $MachinePrecision] * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[Sqrt[b ^ 2 + N[Sqrt[N[(c * N[(4.0 * a), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2 \cdot c}{b + \frac{\sqrt{{b}^{4} + -16 \cdot \left(\left(c \cdot a\right) \cdot \left(c \cdot a\right)\right)}}{\mathsf{hypot}\left(b, \sqrt{c \cdot \left(4 \cdot a\right)}\right)}}
\end{array}
Initial program 30.4%
*-commutative30.4%
+-commutative30.4%
sqr-neg30.4%
unsub-neg30.4%
sqr-neg30.4%
fma-neg30.4%
distribute-lft-neg-in30.4%
*-commutative30.4%
*-commutative30.4%
distribute-rgt-neg-in30.4%
metadata-eval30.4%
Simplified30.4%
*-commutative30.4%
metadata-eval30.4%
distribute-lft-neg-in30.4%
distribute-rgt-neg-in30.4%
*-commutative30.4%
fma-neg30.4%
flip--30.2%
div-sub30.2%
pow230.2%
pow230.2%
pow-prod-up30.4%
metadata-eval30.4%
fma-define30.4%
associate-*l*30.4%
pow230.4%
associate-*l*30.4%
fma-define30.4%
associate-*l*30.4%
Applied egg-rr30.4%
flip--30.2%
Applied egg-rr31.7%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Applied egg-rr99.2%
*-commutative99.2%
times-frac99.3%
*-lft-identity99.3%
associate-/r*99.5%
associate-*r*99.5%
*-commutative99.5%
*-commutative99.5%
times-frac99.5%
metadata-eval99.5%
associate-*r/99.5%
Simplified99.5%
unpow299.5%
Applied egg-rr99.5%
Taylor expanded in a around 0 99.7%
Final simplification99.7%
(FPCore (a b c) :precision binary64 (/ (/ (* (* c a) -4.0) (+ b (sqrt (* a (+ (* c -4.0) (/ (pow b 2.0) a)))))) (* a 2.0)))
double code(double a, double b, double c) {
return (((c * a) * -4.0) / (b + sqrt((a * ((c * -4.0) + (pow(b, 2.0) / a)))))) / (a * 2.0);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (((c * a) * (-4.0d0)) / (b + sqrt((a * ((c * (-4.0d0)) + ((b ** 2.0d0) / a)))))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return (((c * a) * -4.0) / (b + Math.sqrt((a * ((c * -4.0) + (Math.pow(b, 2.0) / a)))))) / (a * 2.0);
}
def code(a, b, c): return (((c * a) * -4.0) / (b + math.sqrt((a * ((c * -4.0) + (math.pow(b, 2.0) / a)))))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(Float64(c * a) * -4.0) / Float64(b + sqrt(Float64(a * Float64(Float64(c * -4.0) + Float64((b ^ 2.0) / a)))))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = (((c * a) * -4.0) / (b + sqrt((a * ((c * -4.0) + ((b ^ 2.0) / a)))))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision] / N[(b + N[Sqrt[N[(a * N[(N[(c * -4.0), $MachinePrecision] + N[(N[Power[b, 2.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(c \cdot a\right) \cdot -4}{b + \sqrt{a \cdot \left(c \cdot -4 + \frac{{b}^{2}}{a}\right)}}}{a \cdot 2}
\end{array}
Initial program 30.4%
*-commutative30.4%
+-commutative30.4%
sqr-neg30.4%
unsub-neg30.4%
sqr-neg30.4%
fma-neg30.4%
distribute-lft-neg-in30.4%
*-commutative30.4%
*-commutative30.4%
distribute-rgt-neg-in30.4%
metadata-eval30.4%
Simplified30.4%
*-commutative30.4%
metadata-eval30.4%
distribute-lft-neg-in30.4%
distribute-rgt-neg-in30.4%
*-commutative30.4%
fma-neg30.4%
flip--30.2%
div-sub30.2%
pow230.2%
pow230.2%
pow-prod-up30.4%
metadata-eval30.4%
fma-define30.4%
associate-*l*30.4%
pow230.4%
associate-*l*30.4%
fma-define30.4%
associate-*l*30.4%
Applied egg-rr30.4%
flip--30.2%
Applied egg-rr31.7%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in a around inf 99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (/ (/ (* (* c a) -4.0) (+ b (sqrt (fma b b (* a (* c -4.0)))))) (* a 2.0)))
double code(double a, double b, double c) {
return (((c * a) * -4.0) / (b + sqrt(fma(b, b, (a * (c * -4.0)))))) / (a * 2.0);
}
function code(a, b, c) return Float64(Float64(Float64(Float64(c * a) * -4.0) / Float64(b + sqrt(fma(b, b, Float64(a * Float64(c * -4.0)))))) / Float64(a * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision] / N[(b + N[Sqrt[N[(b * b + N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(c \cdot a\right) \cdot -4}{b + \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -4\right)\right)}}}{a \cdot 2}
\end{array}
Initial program 30.4%
*-commutative30.4%
+-commutative30.4%
sqr-neg30.4%
unsub-neg30.4%
sqr-neg30.4%
fma-neg30.4%
distribute-lft-neg-in30.4%
*-commutative30.4%
*-commutative30.4%
distribute-rgt-neg-in30.4%
metadata-eval30.4%
Simplified30.4%
*-commutative30.4%
metadata-eval30.4%
distribute-lft-neg-in30.4%
distribute-rgt-neg-in30.4%
*-commutative30.4%
fma-neg30.4%
flip--30.2%
div-sub30.2%
pow230.2%
pow230.2%
pow-prod-up30.4%
metadata-eval30.4%
fma-define30.4%
associate-*l*30.4%
pow230.4%
associate-*l*30.4%
fma-define30.4%
associate-*l*30.4%
Applied egg-rr30.4%
flip--30.2%
Applied egg-rr31.7%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in b around 0 99.3%
add-sqr-sqrt99.3%
+-commutative99.3%
unpow299.3%
*-commutative99.3%
add-sqr-sqrt0.0%
hypot-define0.0%
associate-*l*0.0%
+-commutative0.0%
unpow20.0%
*-commutative0.0%
add-sqr-sqrt0.0%
hypot-define0.0%
associate-*l*0.0%
Applied egg-rr0.0%
hypot-undefine0.0%
hypot-undefine0.0%
rem-square-sqrt0.0%
rem-square-sqrt99.3%
fma-undefine99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (/ (* -2.0 c) (+ b (+ b (* -2.0 (/ (* c a) b))))))
double code(double a, double b, double c) {
return (-2.0 * c) / (b + (b + (-2.0 * ((c * a) / b))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((-2.0d0) * c) / (b + (b + ((-2.0d0) * ((c * a) / b))))
end function
public static double code(double a, double b, double c) {
return (-2.0 * c) / (b + (b + (-2.0 * ((c * a) / b))));
}
def code(a, b, c): return (-2.0 * c) / (b + (b + (-2.0 * ((c * a) / b))))
function code(a, b, c) return Float64(Float64(-2.0 * c) / Float64(b + Float64(b + Float64(-2.0 * Float64(Float64(c * a) / b))))) end
function tmp = code(a, b, c) tmp = (-2.0 * c) / (b + (b + (-2.0 * ((c * a) / b)))); end
code[a_, b_, c_] := N[(N[(-2.0 * c), $MachinePrecision] / N[(b + N[(b + N[(-2.0 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2 \cdot c}{b + \left(b + -2 \cdot \frac{c \cdot a}{b}\right)}
\end{array}
Initial program 30.4%
*-commutative30.4%
+-commutative30.4%
sqr-neg30.4%
unsub-neg30.4%
sqr-neg30.4%
fma-neg30.4%
distribute-lft-neg-in30.4%
*-commutative30.4%
*-commutative30.4%
distribute-rgt-neg-in30.4%
metadata-eval30.4%
Simplified30.4%
*-commutative30.4%
metadata-eval30.4%
distribute-lft-neg-in30.4%
distribute-rgt-neg-in30.4%
*-commutative30.4%
fma-neg30.4%
flip--30.2%
div-sub30.2%
pow230.2%
pow230.2%
pow-prod-up30.4%
metadata-eval30.4%
fma-define30.4%
associate-*l*30.4%
pow230.4%
associate-*l*30.4%
fma-define30.4%
associate-*l*30.4%
Applied egg-rr30.4%
flip--30.2%
Applied egg-rr31.7%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Applied egg-rr99.2%
*-commutative99.2%
times-frac99.3%
*-lft-identity99.3%
associate-/r*99.5%
associate-*r*99.5%
*-commutative99.5%
*-commutative99.5%
times-frac99.5%
metadata-eval99.5%
associate-*r/99.5%
Simplified99.5%
Taylor expanded in a around 0 91.5%
Taylor expanded in a around 0 91.7%
Final simplification91.7%
(FPCore (a b c) :precision binary64 (/ 1.0 (- (/ a b) (/ b c))))
double code(double a, double b, double c) {
return 1.0 / ((a / b) - (b / c));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 1.0d0 / ((a / b) - (b / c))
end function
public static double code(double a, double b, double c) {
return 1.0 / ((a / b) - (b / c));
}
def code(a, b, c): return 1.0 / ((a / b) - (b / c))
function code(a, b, c) return Float64(1.0 / Float64(Float64(a / b) - Float64(b / c))) end
function tmp = code(a, b, c) tmp = 1.0 / ((a / b) - (b / c)); end
code[a_, b_, c_] := N[(1.0 / N[(N[(a / b), $MachinePrecision] - N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{a}{b} - \frac{b}{c}}
\end{array}
Initial program 30.4%
*-commutative30.4%
Simplified30.4%
Taylor expanded in a around 0 91.3%
mul-1-neg91.3%
unsub-neg91.3%
mul-1-neg91.3%
distribute-neg-frac291.3%
associate-/l*91.3%
Simplified91.3%
Taylor expanded in b around inf 91.3%
distribute-lft-out91.3%
associate-*r/91.3%
mul-1-neg91.3%
distribute-neg-frac291.3%
+-commutative91.3%
associate-/l*91.3%
fma-define91.3%
unpow291.3%
unpow291.3%
times-frac91.3%
sqr-neg91.3%
distribute-frac-neg291.3%
distribute-frac-neg291.3%
unpow291.3%
Simplified91.3%
clear-num91.1%
inv-pow91.1%
distribute-frac-neg291.1%
Applied egg-rr91.1%
unpow-191.1%
distribute-neg-frac291.1%
Simplified91.1%
Taylor expanded in a around 0 91.4%
+-commutative91.4%
mul-1-neg91.4%
unsub-neg91.4%
Simplified91.4%
(FPCore (a b c) :precision binary64 (/ (- c) b))
double code(double a, double b, double c) {
return -c / b;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = -c / b
end function
public static double code(double a, double b, double c) {
return -c / b;
}
def code(a, b, c): return -c / b
function code(a, b, c) return Float64(Float64(-c) / b) end
function tmp = code(a, b, c) tmp = -c / b; end
code[a_, b_, c_] := N[((-c) / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b}
\end{array}
Initial program 30.4%
*-commutative30.4%
Simplified30.4%
Taylor expanded in b around inf 82.1%
associate-*r/82.1%
mul-1-neg82.1%
Simplified82.1%
(FPCore (a b c) :precision binary64 (/ b a))
double code(double a, double b, double c) {
return b / a;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = b / a
end function
public static double code(double a, double b, double c) {
return b / a;
}
def code(a, b, c): return b / a
function code(a, b, c) return Float64(b / a) end
function tmp = code(a, b, c) tmp = b / a; end
code[a_, b_, c_] := N[(b / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{a}
\end{array}
Initial program 30.4%
*-commutative30.4%
+-commutative30.4%
sqr-neg30.4%
unsub-neg30.4%
sqr-neg30.4%
fma-neg30.4%
distribute-lft-neg-in30.4%
*-commutative30.4%
*-commutative30.4%
distribute-rgt-neg-in30.4%
metadata-eval30.4%
Simplified30.4%
*-commutative30.4%
metadata-eval30.4%
distribute-lft-neg-in30.4%
distribute-rgt-neg-in30.4%
*-commutative30.4%
fma-neg30.4%
flip--30.2%
div-sub30.2%
pow230.2%
pow230.2%
pow-prod-up30.4%
metadata-eval30.4%
fma-define30.4%
associate-*l*30.4%
pow230.4%
associate-*l*30.4%
fma-define30.4%
associate-*l*30.4%
Applied egg-rr30.4%
flip--30.2%
Applied egg-rr31.7%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Applied egg-rr99.2%
*-commutative99.2%
times-frac99.3%
*-lft-identity99.3%
associate-/r*99.5%
associate-*r*99.5%
*-commutative99.5%
*-commutative99.5%
times-frac99.5%
metadata-eval99.5%
associate-*r/99.5%
Simplified99.5%
Taylor expanded in a around 0 91.5%
Taylor expanded in a around inf 1.6%
herbie shell --seed 2024107
(FPCore (a b c)
:name "Quadratic roots, medium range"
:precision binary64
:pre (and (and (and (< 1.1102230246251565e-16 a) (< a 9007199254740992.0)) (and (< 1.1102230246251565e-16 b) (< b 9007199254740992.0))) (and (< 1.1102230246251565e-16 c) (< c 9007199254740992.0)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))