
(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 (* a (* c -4.0))) (t_1 (fma b b t_0)) (t_2 (- (* b b) t_1)))
(if (<= (/ (- (sqrt (- (* b b) (* (* 4.0 a) c))) b) (* a 2.0)) -5e-21)
(/ (/ (* t_2 (/ t_0 (+ b (sqrt t_1)))) t_2) (* a 2.0))
(/ (fma a (/ (* c c) (* b b)) c) (- b)))))
double code(double a, double b, double c) {
double t_0 = a * (c * -4.0);
double t_1 = fma(b, b, t_0);
double t_2 = (b * b) - t_1;
double tmp;
if (((sqrt(((b * b) - ((4.0 * a) * c))) - b) / (a * 2.0)) <= -5e-21) {
tmp = ((t_2 * (t_0 / (b + sqrt(t_1)))) / t_2) / (a * 2.0);
} else {
tmp = fma(a, ((c * c) / (b * b)), c) / -b;
}
return tmp;
}
function code(a, b, c) t_0 = Float64(a * Float64(c * -4.0)) t_1 = fma(b, b, t_0) t_2 = Float64(Float64(b * b) - t_1) tmp = 0.0 if (Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c))) - b) / Float64(a * 2.0)) <= -5e-21) tmp = Float64(Float64(Float64(t_2 * Float64(t_0 / Float64(b + sqrt(t_1)))) / t_2) / Float64(a * 2.0)); else tmp = Float64(fma(a, Float64(Float64(c * c) / Float64(b * b)), c) / Float64(-b)); end return tmp end
code[a_, b_, c_] := Block[{t$95$0 = N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(b * b + t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(b * b), $MachinePrecision] - t$95$1), $MachinePrecision]}, If[LessEqual[N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], -5e-21], N[(N[(N[(t$95$2 * N[(t$95$0 / N[(b + N[Sqrt[t$95$1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / t$95$2), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := a \cdot \left(c \cdot -4\right)\\
t_1 := \mathsf{fma}\left(b, b, t\_0\right)\\
t_2 := b \cdot b - t\_1\\
\mathbf{if}\;\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}{a \cdot 2} \leq -5 \cdot 10^{-21}:\\
\;\;\;\;\frac{\frac{t\_2 \cdot \frac{t\_0}{b + \sqrt{t\_1}}}{t\_2}}{a \cdot 2}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{-b}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 4 binary64) a) c)))) (*.f64 #s(literal 2 binary64) a)) < -4.99999999999999973e-21Initial program 60.7%
Applied egg-rr60.7%
lift-*.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
lift-sqrt.f64N/A
flip-+N/A
lift--.f64N/A
Applied egg-rr60.8%
lift-*.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
lift-sqrt.f64N/A
flip--N/A
lift-*.f64N/A
lift-sqrt.f64N/A
lift-sqrt.f64N/A
rem-square-sqrtN/A
lift--.f64N/A
lower-/.f64N/A
Applied egg-rr99.1%
if -4.99999999999999973e-21 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 4 binary64) a) c)))) (*.f64 #s(literal 2 binary64) a)) Initial program 3.8%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-/l*N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64100.0
Simplified100.0%
Final simplification99.8%
(FPCore (a b c) :precision binary64 (/ (- (/ (* (* (* c (* c c)) -2.0) (* a a)) (* (* b b) (* b b))) (fma a (/ (* c c) (* b b)) c)) b))
double code(double a, double b, double c) {
return (((((c * (c * c)) * -2.0) * (a * a)) / ((b * b) * (b * b))) - fma(a, ((c * c) / (b * b)), c)) / b;
}
function code(a, b, c) return Float64(Float64(Float64(Float64(Float64(Float64(c * Float64(c * c)) * -2.0) * Float64(a * a)) / Float64(Float64(b * b) * Float64(b * b))) - fma(a, Float64(Float64(c * c) / Float64(b * b)), c)) / b) end
code[a_, b_, c_] := N[(N[(N[(N[(N[(N[(c * N[(c * c), $MachinePrecision]), $MachinePrecision] * -2.0), $MachinePrecision] * N[(a * a), $MachinePrecision]), $MachinePrecision] / N[(N[(b * b), $MachinePrecision] * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(\left(c \cdot \left(c \cdot c\right)\right) \cdot -2\right) \cdot \left(a \cdot a\right)}{\left(b \cdot b\right) \cdot \left(b \cdot b\right)} - \mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{b}
\end{array}
Initial program 18.0%
Taylor expanded in b around inf
lower-/.f64N/A
Simplified97.5%
(FPCore (a b c) :precision binary64 (/ (fma a (/ (* c c) (* b b)) c) (- b)))
double code(double a, double b, double c) {
return fma(a, ((c * c) / (b * b)), c) / -b;
}
function code(a, b, c) return Float64(fma(a, Float64(Float64(c * c) / Float64(b * b)), c) / Float64(-b)) end
code[a_, b_, c_] := N[(N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{-b}
\end{array}
Initial program 18.0%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-/l*N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6495.7
Simplified95.7%
Final simplification95.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(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 18.0%
Taylor expanded in b around inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f6490.4
Simplified90.4%
Final simplification90.4%
(FPCore (a b c) :precision binary64 0.0)
double code(double a, double b, double c) {
return 0.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.0d0
end function
public static double code(double a, double b, double c) {
return 0.0;
}
def code(a, b, c): return 0.0
function code(a, b, c) return 0.0 end
function tmp = code(a, b, c) tmp = 0.0; end
code[a_, b_, c_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 18.0%
Applied egg-rr18.0%
lift-/.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
lift-sqrt.f64N/A
sub-negN/A
distribute-lft-inN/A
lower-fma.f64N/A
lower-*.f64N/A
lower-neg.f6419.3
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lower-*.f6419.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6419.3
Applied egg-rr19.3%
Taylor expanded in c around 0
distribute-rgt-outN/A
metadata-evalN/A
mul0-rgt3.3
Simplified3.3%
herbie shell --seed 2024219
(FPCore (a b c)
:name "Quadratic roots, wide range"
:precision binary64
:pre (and (and (and (< 4.930380657631324e-32 a) (< a 2.028240960365167e+31)) (and (< 4.930380657631324e-32 b) (< b 2.028240960365167e+31))) (and (< 4.930380657631324e-32 c) (< c 2.028240960365167e+31)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))