
(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 5 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 (/ (* c 2.0) (- (- b) (sqrt (fma -4.0 (* c a) (pow b 2.0))))))
double code(double a, double b, double c) {
return (c * 2.0) / (-b - sqrt(fma(-4.0, (c * a), pow(b, 2.0))));
}
function code(a, b, c) return Float64(Float64(c * 2.0) / Float64(Float64(-b) - sqrt(fma(-4.0, Float64(c * a), (b ^ 2.0))))) end
code[a_, b_, c_] := N[(N[(c * 2.0), $MachinePrecision] / N[((-b) - N[Sqrt[N[(-4.0 * N[(c * a), $MachinePrecision] + N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot 2}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, c \cdot a, {b}^{2}\right)}}
\end{array}
Initial program 30.7%
*-commutative30.7%
Simplified30.7%
neg-sub030.7%
flip--30.7%
metadata-eval30.7%
pow230.7%
add-sqr-sqrt30.8%
sqrt-prod30.7%
sqr-neg30.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod30.7%
sqr-neg30.7%
sqrt-prod30.8%
add-sqr-sqrt30.7%
Applied egg-rr30.7%
neg-sub030.7%
Simplified30.7%
flip-+30.7%
Applied egg-rr31.6%
associate--r-99.4%
unpow299.4%
sqr-neg99.4%
unpow299.4%
Simplified99.4%
div-inv99.2%
+-commutative99.2%
fma-define99.2%
+-inverses99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*l/99.4%
*-commutative99.4%
associate-/r*99.4%
metadata-eval99.4%
*-commutative99.4%
*-commutative99.4%
associate-*r*99.4%
cancel-sign-sub-inv99.4%
metadata-eval99.4%
+-commutative99.4%
fma-define99.4%
Simplified99.4%
Taylor expanded in c around 0 99.8%
*-commutative99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (a b c) :precision binary64 (/ (/ (* (* c a) 4.0) (- (- b) (sqrt (fma b b (* c (* a (- 4.0))))))) (* 2.0 a)))
double code(double a, double b, double c) {
return (((c * a) * 4.0) / (-b - sqrt(fma(b, b, (c * (a * -4.0)))))) / (2.0 * a);
}
function code(a, b, c) return Float64(Float64(Float64(Float64(c * a) * 4.0) / Float64(Float64(-b) - sqrt(fma(b, b, Float64(c * Float64(a * Float64(-4.0))))))) / Float64(2.0 * a)) end
code[a_, b_, c_] := N[(N[(N[(N[(c * a), $MachinePrecision] * 4.0), $MachinePrecision] / N[((-b) - N[Sqrt[N[(b * b + N[(c * N[(a * (-4.0)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(c \cdot a\right) \cdot 4}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot \left(-4\right)\right)\right)}}}{2 \cdot a}
\end{array}
Initial program 30.7%
*-commutative30.7%
Simplified30.7%
neg-sub030.7%
flip--30.7%
metadata-eval30.7%
pow230.7%
add-sqr-sqrt30.8%
sqrt-prod30.7%
sqr-neg30.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod30.7%
sqr-neg30.7%
sqrt-prod30.8%
add-sqr-sqrt30.7%
Applied egg-rr30.7%
neg-sub030.7%
Simplified30.7%
flip-+30.7%
Applied egg-rr31.6%
associate--r-99.4%
unpow299.4%
sqr-neg99.4%
unpow299.4%
Simplified99.4%
cancel-sign-sub-inv99.4%
unpow299.4%
fma-define99.4%
Applied egg-rr99.4%
Taylor expanded in b around 0 99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (/ (/ (* (* c a) 4.0) (- (- b) (sqrt (- (pow b 2.0) (* c (* a 4.0)))))) (* 2.0 a)))
double code(double a, double b, double c) {
return (((c * a) * 4.0) / (-b - sqrt((pow(b, 2.0) - (c * (a * 4.0)))))) / (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 = (((c * a) * 4.0d0) / (-b - sqrt(((b ** 2.0d0) - (c * (a * 4.0d0)))))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (((c * a) * 4.0) / (-b - Math.sqrt((Math.pow(b, 2.0) - (c * (a * 4.0)))))) / (2.0 * a);
}
def code(a, b, c): return (((c * a) * 4.0) / (-b - math.sqrt((math.pow(b, 2.0) - (c * (a * 4.0)))))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(Float64(c * a) * 4.0) / Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) - Float64(c * Float64(a * 4.0)))))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (((c * a) * 4.0) / (-b - sqrt(((b ^ 2.0) - (c * (a * 4.0)))))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[(N[(N[(c * a), $MachinePrecision] * 4.0), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(c \cdot a\right) \cdot 4}{\left(-b\right) - \sqrt{{b}^{2} - c \cdot \left(a \cdot 4\right)}}}{2 \cdot a}
\end{array}
Initial program 30.7%
*-commutative30.7%
Simplified30.7%
neg-sub030.7%
flip--30.7%
metadata-eval30.7%
pow230.7%
add-sqr-sqrt30.8%
sqrt-prod30.7%
sqr-neg30.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod30.7%
sqr-neg30.7%
sqrt-prod30.8%
add-sqr-sqrt30.7%
Applied egg-rr30.7%
neg-sub030.7%
Simplified30.7%
flip-+30.7%
Applied egg-rr31.6%
associate--r-99.4%
unpow299.4%
sqr-neg99.4%
unpow299.4%
Simplified99.4%
Taylor expanded in b around 0 99.4%
Final simplification99.4%
(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.7%
*-commutative30.7%
Simplified30.7%
neg-sub030.7%
flip--30.7%
metadata-eval30.7%
pow230.7%
add-sqr-sqrt30.8%
sqrt-prod30.7%
sqr-neg30.7%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod30.7%
sqr-neg30.7%
sqrt-prod30.8%
add-sqr-sqrt30.7%
Applied egg-rr30.7%
neg-sub030.7%
Simplified30.7%
clear-num30.7%
inv-pow30.7%
pow230.7%
distribute-frac-neg30.7%
pow230.7%
pow130.7%
pow-div30.7%
metadata-eval30.7%
pow130.7%
pow230.7%
*-commutative30.7%
*-commutative30.7%
Applied egg-rr30.7%
unpow-130.7%
associate-/l*30.7%
Simplified30.7%
Taylor expanded in a around 0 91.4%
Final simplification91.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(c / Float64(-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.7%
*-commutative30.7%
Simplified30.7%
Taylor expanded in a around 0 81.8%
associate-*r/81.8%
mul-1-neg81.8%
Simplified81.8%
Final simplification81.8%
herbie shell --seed 2024137
(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)))