
(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 6 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 (/ (* 0.5 (* 4.0 c)) (- (- b) (sqrt (fma b b (* c (* a (- 4.0))))))))
double code(double a, double b, double c) {
return (0.5 * (4.0 * c)) / (-b - sqrt(fma(b, b, (c * (a * -4.0)))));
}
function code(a, b, c) return Float64(Float64(0.5 * Float64(4.0 * c)) / Float64(Float64(-b) - sqrt(fma(b, b, Float64(c * Float64(a * Float64(-4.0))))))) end
code[a_, b_, c_] := N[(N[(0.5 * N[(4.0 * c), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(b * b + N[(c * N[(a * (-4.0)), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \left(4 \cdot c\right)}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot \left(-4\right)\right)\right)}}
\end{array}
Initial program 33.3%
flip-+33.3%
pow233.3%
add-sqr-sqrt34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
Applied egg-rr34.2%
div-inv34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
Applied egg-rr34.2%
associate-*l/34.2%
Simplified99.6%
Taylor expanded in c around 0 99.7%
Final simplification99.7%
(FPCore (a b c) :precision binary64 (if (<= (/ (- (sqrt (- (* b b) (* c (* 4.0 a)))) b) (* a 2.0)) -1.0) (/ (- (sqrt (+ (* b b) (* -4.0 (* c a)))) b) (* a 2.0)) (/ (* 0.5 (* 4.0 c)) (+ (* 2.0 (/ (* c a) b)) (* b -2.0)))))
double code(double a, double b, double c) {
double tmp;
if (((sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -1.0) {
tmp = (sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0));
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: tmp
if (((sqrt(((b * b) - (c * (4.0d0 * a)))) - b) / (a * 2.0d0)) <= (-1.0d0)) then
tmp = (sqrt(((b * b) + ((-4.0d0) * (c * a)))) - b) / (a * 2.0d0)
else
tmp = (0.5d0 * (4.0d0 * c)) / ((2.0d0 * ((c * a) / b)) + (b * (-2.0d0)))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (((Math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -1.0) {
tmp = (Math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0));
}
return tmp;
}
def code(a, b, c): tmp = 0 if ((math.sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -1.0: tmp = (math.sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0) else: tmp = (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0)) return tmp
function code(a, b, c) tmp = 0.0 if (Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(4.0 * a)))) - b) / Float64(a * 2.0)) <= -1.0) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(-4.0 * Float64(c * a)))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(0.5 * Float64(4.0 * c)) / Float64(Float64(2.0 * Float64(Float64(c * a) / b)) + Float64(b * -2.0))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (((sqrt(((b * b) - (c * (4.0 * a)))) - b) / (a * 2.0)) <= -1.0) tmp = (sqrt(((b * b) + (-4.0 * (c * a)))) - b) / (a * 2.0); else tmp = (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(4.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], -1.0], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * N[(4.0 * c), $MachinePrecision]), $MachinePrecision] / N[(N[(2.0 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] + N[(b * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\sqrt{b \cdot b - c \cdot \left(4 \cdot a\right)} - b}{a \cdot 2} \leq -1:\\
\;\;\;\;\frac{\sqrt{b \cdot b + -4 \cdot \left(c \cdot a\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot \left(4 \cdot c\right)}{2 \cdot \frac{c \cdot a}{b} + b \cdot -2}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) < -1Initial program 79.7%
*-commutative79.7%
+-commutative79.7%
unsub-neg79.7%
fma-neg80.2%
associate-*l*80.2%
*-commutative80.2%
distribute-rgt-neg-in80.2%
metadata-eval80.2%
Simplified80.2%
fma-udef79.7%
*-commutative79.7%
Applied egg-rr79.7%
if -1 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) Initial program 26.4%
flip-+26.5%
pow226.5%
add-sqr-sqrt27.3%
*-commutative27.3%
*-commutative27.3%
*-commutative27.3%
*-commutative27.3%
Applied egg-rr27.3%
div-inv27.3%
*-commutative27.3%
*-commutative27.3%
*-commutative27.3%
Applied egg-rr27.3%
associate-*l/27.3%
Simplified99.6%
Taylor expanded in c around 0 99.8%
Taylor expanded in b around inf 92.8%
Final simplification91.1%
(FPCore (a b c) :precision binary64 (/ (* 0.5 (* 4.0 c)) (- (- b) (sqrt (+ (* b b) (* c (* a -4.0)))))))
double code(double a, double b, double c) {
return (0.5 * (4.0 * c)) / (-b - sqrt(((b * b) + (c * (a * -4.0)))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (0.5d0 * (4.0d0 * c)) / (-b - sqrt(((b * b) + (c * (a * (-4.0d0))))))
end function
public static double code(double a, double b, double c) {
return (0.5 * (4.0 * c)) / (-b - Math.sqrt(((b * b) + (c * (a * -4.0)))));
}
def code(a, b, c): return (0.5 * (4.0 * c)) / (-b - math.sqrt(((b * b) + (c * (a * -4.0)))))
function code(a, b, c) return Float64(Float64(0.5 * Float64(4.0 * c)) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(c * Float64(a * -4.0)))))) end
function tmp = code(a, b, c) tmp = (0.5 * (4.0 * c)) / (-b - sqrt(((b * b) + (c * (a * -4.0))))); end
code[a_, b_, c_] := N[(N[(0.5 * N[(4.0 * c), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \left(4 \cdot c\right)}{\left(-b\right) - \sqrt{b \cdot b + c \cdot \left(a \cdot -4\right)}}
\end{array}
Initial program 33.3%
flip-+33.3%
pow233.3%
add-sqr-sqrt34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
Applied egg-rr34.2%
div-inv34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
Applied egg-rr34.2%
associate-*l/34.2%
Simplified99.6%
Taylor expanded in c around 0 99.7%
fma-udef99.7%
distribute-rgt-neg-in99.7%
metadata-eval99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (a b c) :precision binary64 (/ (* 0.5 (* 4.0 c)) (+ (* 2.0 (/ (* c a) b)) (* b -2.0))))
double code(double a, double b, double c) {
return (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -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 = (0.5d0 * (4.0d0 * c)) / ((2.0d0 * ((c * a) / b)) + (b * (-2.0d0)))
end function
public static double code(double a, double b, double c) {
return (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0));
}
def code(a, b, c): return (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0))
function code(a, b, c) return Float64(Float64(0.5 * Float64(4.0 * c)) / Float64(Float64(2.0 * Float64(Float64(c * a) / b)) + Float64(b * -2.0))) end
function tmp = code(a, b, c) tmp = (0.5 * (4.0 * c)) / ((2.0 * ((c * a) / b)) + (b * -2.0)); end
code[a_, b_, c_] := N[(N[(0.5 * N[(4.0 * c), $MachinePrecision]), $MachinePrecision] / N[(N[(2.0 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] + N[(b * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5 \cdot \left(4 \cdot c\right)}{2 \cdot \frac{c \cdot a}{b} + b \cdot -2}
\end{array}
Initial program 33.3%
flip-+33.3%
pow233.3%
add-sqr-sqrt34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
Applied egg-rr34.2%
div-inv34.2%
*-commutative34.2%
*-commutative34.2%
*-commutative34.2%
Applied egg-rr34.2%
associate-*l/34.2%
Simplified99.6%
Taylor expanded in c around 0 99.7%
Taylor expanded in b around inf 88.5%
Final simplification88.5%
(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 33.3%
neg-sub033.3%
associate-+l-33.3%
sub0-neg33.3%
neg-mul-133.3%
associate-*l/33.3%
*-commutative33.3%
associate-/r*33.3%
/-rgt-identity33.3%
metadata-eval33.3%
Simplified33.3%
Taylor expanded in b around inf 79.3%
associate-*r/79.3%
neg-mul-179.3%
Simplified79.3%
Final simplification79.3%
(FPCore (a b c) :precision binary64 (/ 0.0 a))
double code(double a, double b, double c) {
return 0.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 = 0.0d0 / a
end function
public static double code(double a, double b, double c) {
return 0.0 / a;
}
def code(a, b, c): return 0.0 / a
function code(a, b, c) return Float64(0.0 / a) end
function tmp = code(a, b, c) tmp = 0.0 / a; end
code[a_, b_, c_] := N[(0.0 / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{0}{a}
\end{array}
Initial program 33.3%
add-cube-cbrt33.2%
pow333.2%
neg-mul-133.2%
fma-def33.2%
*-commutative33.2%
*-commutative33.2%
*-commutative33.2%
Applied egg-rr33.2%
Taylor expanded in c around 0 3.2%
pow-base-13.2%
*-rgt-identity3.2%
associate-*r/3.2%
neg-mul-13.2%
sub-neg3.2%
+-inverses3.2%
metadata-eval3.2%
Simplified3.2%
Final simplification3.2%
herbie shell --seed 2023250
(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)))