
(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 a) (* a 4.0)) (* (- (- b) (sqrt (fma c (* a -4.0) (* b b)))) 2.0)))
double code(double a, double b, double c) {
return ((c / a) * (a * 4.0)) / ((-b - sqrt(fma(c, (a * -4.0), (b * b)))) * 2.0);
}
function code(a, b, c) return Float64(Float64(Float64(c / a) * Float64(a * 4.0)) / Float64(Float64(Float64(-b) - sqrt(fma(c, Float64(a * -4.0), Float64(b * b)))) * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(c / a), $MachinePrecision] * N[(a * 4.0), $MachinePrecision]), $MachinePrecision] / N[(N[((-b) - N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c}{a} \cdot \left(a \cdot 4\right)}{\left(\left(-b\right) - \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}\right) \cdot 2}
\end{array}
Initial program 16.0%
flip-+16.0%
pow216.0%
add-sqr-sqrt16.5%
*-commutative16.5%
*-commutative16.5%
*-commutative16.5%
*-commutative16.5%
Applied egg-rr16.5%
unpow216.5%
sqr-neg16.5%
sub-neg16.5%
+-commutative16.5%
distribute-rgt-neg-in16.5%
fma-def16.5%
distribute-rgt-neg-in16.5%
metadata-eval16.5%
sub-neg16.5%
+-commutative16.5%
distribute-rgt-neg-in16.5%
fma-def16.5%
distribute-rgt-neg-in16.5%
metadata-eval16.5%
Simplified16.5%
Taylor expanded in b around 0 99.5%
associate-*r*99.5%
*-commutative99.5%
associate-*r*99.5%
Simplified99.5%
div-inv99.3%
*-commutative99.3%
Applied egg-rr99.3%
*-commutative99.3%
times-frac99.4%
associate-*l*99.4%
*-lft-identity99.4%
times-frac99.5%
*-commutative99.5%
Simplified99.5%
associate-*r/99.8%
*-commutative99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (a b c) :precision binary64 (* (/ c a) (/ (* a 4.0) (* 2.0 (- (- b) (sqrt (+ (* b b) (* c (* a -4.0)))))))))
double code(double a, double b, double c) {
return (c / a) * ((a * 4.0) / (2.0 * (-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 = (c / a) * ((a * 4.0d0) / (2.0d0 * (-b - sqrt(((b * b) + (c * (a * (-4.0d0))))))))
end function
public static double code(double a, double b, double c) {
return (c / a) * ((a * 4.0) / (2.0 * (-b - Math.sqrt(((b * b) + (c * (a * -4.0)))))));
}
def code(a, b, c): return (c / a) * ((a * 4.0) / (2.0 * (-b - math.sqrt(((b * b) + (c * (a * -4.0)))))))
function code(a, b, c) return Float64(Float64(c / a) * Float64(Float64(a * 4.0) / Float64(2.0 * Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(c * Float64(a * -4.0)))))))) end
function tmp = code(a, b, c) tmp = (c / a) * ((a * 4.0) / (2.0 * (-b - sqrt(((b * b) + (c * (a * -4.0))))))); end
code[a_, b_, c_] := N[(N[(c / a), $MachinePrecision] * N[(N[(a * 4.0), $MachinePrecision] / N[(2.0 * N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{a} \cdot \frac{a \cdot 4}{2 \cdot \left(\left(-b\right) - \sqrt{b \cdot b + c \cdot \left(a \cdot -4\right)}\right)}
\end{array}
Initial program 16.0%
flip-+16.0%
pow216.0%
add-sqr-sqrt16.5%
*-commutative16.5%
*-commutative16.5%
*-commutative16.5%
*-commutative16.5%
Applied egg-rr16.5%
unpow216.5%
sqr-neg16.5%
sub-neg16.5%
+-commutative16.5%
distribute-rgt-neg-in16.5%
fma-def16.5%
distribute-rgt-neg-in16.5%
metadata-eval16.5%
sub-neg16.5%
+-commutative16.5%
distribute-rgt-neg-in16.5%
fma-def16.5%
distribute-rgt-neg-in16.5%
metadata-eval16.5%
Simplified16.5%
Taylor expanded in b around 0 99.5%
associate-*r*99.5%
*-commutative99.5%
associate-*r*99.5%
Simplified99.5%
div-inv99.3%
*-commutative99.3%
Applied egg-rr99.3%
*-commutative99.3%
times-frac99.4%
associate-*l*99.4%
*-lft-identity99.4%
times-frac99.5%
*-commutative99.5%
Simplified99.5%
fma-udef99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (a b c) :precision binary64 (/ (* (* a 4.0) (/ c (- (- b) (sqrt (+ (* b b) (* c (* a -4.0))))))) (* a 2.0)))
double code(double a, double b, double c) {
return ((a * 4.0) * (c / (-b - sqrt(((b * b) + (c * (a * -4.0))))))) / (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 = ((a * 4.0d0) * (c / (-b - sqrt(((b * b) + (c * (a * (-4.0d0)))))))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return ((a * 4.0) * (c / (-b - Math.sqrt(((b * b) + (c * (a * -4.0))))))) / (a * 2.0);
}
def code(a, b, c): return ((a * 4.0) * (c / (-b - math.sqrt(((b * b) + (c * (a * -4.0))))))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(a * 4.0) * Float64(c / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) + Float64(c * Float64(a * -4.0))))))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = ((a * 4.0) * (c / (-b - sqrt(((b * b) + (c * (a * -4.0))))))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(N[(a * 4.0), $MachinePrecision] * N[(c / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(a \cdot 4\right) \cdot \frac{c}{\left(-b\right) - \sqrt{b \cdot b + c \cdot \left(a \cdot -4\right)}}}{a \cdot 2}
\end{array}
Initial program 16.0%
flip-+16.0%
pow216.0%
add-sqr-sqrt16.5%
*-commutative16.5%
*-commutative16.5%
*-commutative16.5%
*-commutative16.5%
Applied egg-rr16.5%
unpow216.5%
sqr-neg16.5%
sub-neg16.5%
+-commutative16.5%
distribute-rgt-neg-in16.5%
fma-def16.5%
distribute-rgt-neg-in16.5%
metadata-eval16.5%
sub-neg16.5%
+-commutative16.5%
distribute-rgt-neg-in16.5%
fma-def16.5%
distribute-rgt-neg-in16.5%
metadata-eval16.5%
Simplified16.5%
Taylor expanded in b around 0 99.5%
associate-*r*99.5%
*-commutative99.5%
associate-*r*99.5%
Simplified99.5%
div-inv99.3%
Applied egg-rr99.3%
associate-*r/99.5%
*-rgt-identity99.5%
associate-/l*99.4%
associate-/r/99.7%
*-commutative99.7%
Simplified99.7%
fma-udef99.5%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (a b c) :precision binary64 (- (/ (- c) b) (/ (* c (* c a)) (* b (* b b)))))
double code(double a, double b, double c) {
return (-c / b) - ((c * (c * a)) / (b * (b * 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) - ((c * (c * a)) / (b * (b * b)))
end function
public static double code(double a, double b, double c) {
return (-c / b) - ((c * (c * a)) / (b * (b * b)));
}
def code(a, b, c): return (-c / b) - ((c * (c * a)) / (b * (b * b)))
function code(a, b, c) return Float64(Float64(Float64(-c) / b) - Float64(Float64(c * Float64(c * a)) / Float64(b * Float64(b * b)))) end
function tmp = code(a, b, c) tmp = (-c / b) - ((c * (c * a)) / (b * (b * b))); end
code[a_, b_, c_] := N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - \frac{c \cdot \left(c \cdot a\right)}{b \cdot \left(b \cdot b\right)}
\end{array}
Initial program 16.0%
neg-sub016.0%
associate-+l-16.0%
sub0-neg16.0%
neg-mul-116.0%
associate-*l/16.0%
*-commutative16.0%
associate-/r*16.0%
/-rgt-identity16.0%
metadata-eval16.0%
Simplified16.0%
Taylor expanded in b around inf 95.9%
distribute-lft-out95.9%
mul-1-neg95.9%
+-commutative95.9%
unpow295.9%
associate-*l*95.9%
Simplified95.9%
unpow395.9%
Applied egg-rr95.9%
Final simplification95.9%
(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 16.0%
neg-sub016.0%
associate-+l-16.0%
sub0-neg16.0%
neg-mul-116.0%
associate-*l/16.0%
*-commutative16.0%
associate-/r*16.0%
/-rgt-identity16.0%
metadata-eval16.0%
Simplified16.0%
Taylor expanded in b around inf 92.0%
associate-*r/92.0%
neg-mul-192.0%
Simplified92.0%
Final simplification92.0%
herbie shell --seed 2023228
(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)))