
(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 (let* ((t_0 (* c (* a -4.0)))) (/ (/ t_0 (+ b (sqrt (fma b b t_0)))) (* a 2.0))))
double code(double a, double b, double c) {
double t_0 = c * (a * -4.0);
return (t_0 / (b + sqrt(fma(b, b, t_0)))) / (a * 2.0);
}
function code(a, b, c) t_0 = Float64(c * Float64(a * -4.0)) return Float64(Float64(t_0 / Float64(b + sqrt(fma(b, b, t_0)))) / Float64(a * 2.0)) end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$0 / N[(b + N[Sqrt[N[(b * b + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot -4\right)\\
\frac{\frac{t_0}{b + \sqrt{\mathsf{fma}\left(b, b, t_0\right)}}}{a \cdot 2}
\end{array}
\end{array}
Initial program 31.4%
Simplified31.4%
*-commutative31.4%
metadata-eval31.4%
distribute-lft-neg-in31.4%
distribute-rgt-neg-in31.4%
*-commutative31.4%
fma-neg31.4%
associate-*l*31.4%
Applied egg-rr31.4%
flip--31.3%
add-sqr-sqrt32.3%
cancel-sign-sub-inv32.3%
metadata-eval32.3%
cancel-sign-sub-inv32.3%
metadata-eval32.3%
Applied egg-rr32.3%
+-commutative32.3%
associate--l+99.4%
*-commutative99.4%
*-commutative99.4%
associate-*l*99.4%
+-inverses99.4%
+-commutative99.4%
fma-def99.4%
*-commutative99.4%
*-commutative99.4%
associate-*l*99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (+ (* b b) (* -4.0 (* c a)))))
(if (<= (/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* a 2.0)) -150000.0)
(/ (/ (- t_0 (* b b)) (+ b (sqrt t_0))) (* a 2.0))
(- (/ (- c) b) (* (/ a (pow b 3.0)) (* c c))))))
double code(double a, double b, double c) {
double t_0 = (b * b) + (-4.0 * (c * a));
double tmp;
if (((sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0) {
tmp = ((t_0 - (b * b)) / (b + sqrt(t_0))) / (a * 2.0);
} else {
tmp = (-c / b) - ((a / pow(b, 3.0)) * (c * c));
}
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) :: t_0
real(8) :: tmp
t_0 = (b * b) + ((-4.0d0) * (c * a))
if (((sqrt(((b * b) - (c * (a * 4.0d0)))) - b) / (a * 2.0d0)) <= (-150000.0d0)) then
tmp = ((t_0 - (b * b)) / (b + sqrt(t_0))) / (a * 2.0d0)
else
tmp = (-c / b) - ((a / (b ** 3.0d0)) * (c * c))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = (b * b) + (-4.0 * (c * a));
double tmp;
if (((Math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0) {
tmp = ((t_0 - (b * b)) / (b + Math.sqrt(t_0))) / (a * 2.0);
} else {
tmp = (-c / b) - ((a / Math.pow(b, 3.0)) * (c * c));
}
return tmp;
}
def code(a, b, c): t_0 = (b * b) + (-4.0 * (c * a)) tmp = 0 if ((math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0: tmp = ((t_0 - (b * b)) / (b + math.sqrt(t_0))) / (a * 2.0) else: tmp = (-c / b) - ((a / math.pow(b, 3.0)) * (c * c)) return tmp
function code(a, b, c) t_0 = Float64(Float64(b * b) + Float64(-4.0 * Float64(c * a))) tmp = 0.0 if (Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - b) / Float64(a * 2.0)) <= -150000.0) tmp = Float64(Float64(Float64(t_0 - Float64(b * b)) / Float64(b + sqrt(t_0))) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(a / (b ^ 3.0)) * Float64(c * c))); end return tmp end
function tmp_2 = code(a, b, c) t_0 = (b * b) + (-4.0 * (c * a)); tmp = 0.0; if (((sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0) tmp = ((t_0 - (b * b)) / (b + sqrt(t_0))) / (a * 2.0); else tmp = (-c / b) - ((a / (b ^ 3.0)) * (c * c)); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(b * b), $MachinePrecision] + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], -150000.0], N[(N[(N[(t$95$0 - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(b + N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[((-c) / b), $MachinePrecision] - N[(N[(a / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision] * N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := b \cdot b + -4 \cdot \left(c \cdot a\right)\\
\mathbf{if}\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2} \leq -150000:\\
\;\;\;\;\frac{\frac{t_0 - b \cdot b}{b + \sqrt{t_0}}}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{a}{{b}^{3}} \cdot \left(c \cdot c\right)\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) < -1.5e5Initial program 83.2%
Simplified83.3%
*-commutative83.3%
metadata-eval83.3%
distribute-lft-neg-in83.3%
distribute-rgt-neg-in83.3%
*-commutative83.3%
fma-neg83.2%
associate-*l*83.2%
Applied egg-rr83.2%
flip--82.8%
add-sqr-sqrt84.4%
cancel-sign-sub-inv84.4%
metadata-eval84.4%
cancel-sign-sub-inv84.4%
metadata-eval84.4%
Applied egg-rr84.4%
if -1.5e5 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) Initial program 27.3%
Taylor expanded in b around inf 92.3%
mul-1-neg92.3%
unsub-neg92.3%
mul-1-neg92.3%
distribute-neg-frac92.3%
associate-/l*92.3%
associate-/r/92.3%
unpow292.3%
Simplified92.3%
Final simplification91.7%
(FPCore (a b c) :precision binary64 (if (<= (/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* a 2.0)) -150000.0) (/ (- (sqrt (- (* b b) (* 4.0 (* c a)))) b) (* a 2.0)) (- (/ (- c) b) (* (/ a (pow b 3.0)) (* c c)))))
double code(double a, double b, double c) {
double tmp;
if (((sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0) {
tmp = (sqrt(((b * b) - (4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((a / pow(b, 3.0)) * (c * c));
}
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 * (a * 4.0d0)))) - b) / (a * 2.0d0)) <= (-150000.0d0)) then
tmp = (sqrt(((b * b) - (4.0d0 * (c * a)))) - b) / (a * 2.0d0)
else
tmp = (-c / b) - ((a / (b ** 3.0d0)) * (c * c))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (((Math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0) {
tmp = (Math.sqrt(((b * b) - (4.0 * (c * a)))) - b) / (a * 2.0);
} else {
tmp = (-c / b) - ((a / Math.pow(b, 3.0)) * (c * c));
}
return tmp;
}
def code(a, b, c): tmp = 0 if ((math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0: tmp = (math.sqrt(((b * b) - (4.0 * (c * a)))) - b) / (a * 2.0) else: tmp = (-c / b) - ((a / math.pow(b, 3.0)) * (c * c)) return tmp
function code(a, b, c) tmp = 0.0 if (Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - b) / Float64(a * 2.0)) <= -150000.0) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(c * a)))) - b) / Float64(a * 2.0)); else tmp = Float64(Float64(Float64(-c) / b) - Float64(Float64(a / (b ^ 3.0)) * Float64(c * c))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (((sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)) <= -150000.0) tmp = (sqrt(((b * b) - (4.0 * (c * a)))) - b) / (a * 2.0); else tmp = (-c / b) - ((a / (b ^ 3.0)) * (c * c)); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], -150000.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[((-c) / b), $MachinePrecision] - N[(N[(a / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision] * N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2} \leq -150000:\\
\;\;\;\;\frac{\sqrt{b \cdot b - 4 \cdot \left(c \cdot a\right)} - b}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b} - \frac{a}{{b}^{3}} \cdot \left(c \cdot c\right)\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) < -1.5e5Initial program 83.2%
Simplified83.3%
*-commutative83.3%
metadata-eval83.3%
distribute-lft-neg-in83.3%
distribute-rgt-neg-in83.3%
*-commutative83.3%
fma-neg83.2%
associate-*l*83.2%
Applied egg-rr83.2%
if -1.5e5 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 4 a) c)))) (*.f64 2 a)) Initial program 27.3%
Taylor expanded in b around inf 92.3%
mul-1-neg92.3%
unsub-neg92.3%
mul-1-neg92.3%
distribute-neg-frac92.3%
associate-/l*92.3%
associate-/r/92.3%
unpow292.3%
Simplified92.3%
Final simplification91.6%
(FPCore (a b c) :precision binary64 (- (/ (- c) b) (* (/ a (pow b 3.0)) (* c c))))
double code(double a, double b, double c) {
return (-c / b) - ((a / pow(b, 3.0)) * (c * c));
}
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) - ((a / (b ** 3.0d0)) * (c * c))
end function
public static double code(double a, double b, double c) {
return (-c / b) - ((a / Math.pow(b, 3.0)) * (c * c));
}
def code(a, b, c): return (-c / b) - ((a / math.pow(b, 3.0)) * (c * c))
function code(a, b, c) return Float64(Float64(Float64(-c) / b) - Float64(Float64(a / (b ^ 3.0)) * Float64(c * c))) end
function tmp = code(a, b, c) tmp = (-c / b) - ((a / (b ^ 3.0)) * (c * c)); end
code[a_, b_, c_] := N[(N[((-c) / b), $MachinePrecision] - N[(N[(a / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision] * N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - \frac{a}{{b}^{3}} \cdot \left(c \cdot c\right)
\end{array}
Initial program 31.4%
Taylor expanded in b around inf 89.7%
mul-1-neg89.7%
unsub-neg89.7%
mul-1-neg89.7%
distribute-neg-frac89.7%
associate-/l*89.7%
associate-/r/89.7%
unpow289.7%
Simplified89.7%
Final simplification89.7%
(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 31.4%
Taylor expanded in b around inf 80.7%
mul-1-neg80.7%
distribute-neg-frac80.7%
Simplified80.7%
Final simplification80.7%
herbie shell --seed 2023293
(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)))