
(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 (fma -1.0 (/ (* a (* c c)) (pow b 3.0)) (fma -0.25 (* (/ (pow (* a c) 4.0) a) (/ 20.0 (pow b 7.0))) (fma -1.0 (/ c b) (* -2.0 (/ (pow c 3.0) (/ (pow b 5.0) (* a a))))))))
double code(double a, double b, double c) {
return fma(-1.0, ((a * (c * c)) / pow(b, 3.0)), fma(-0.25, ((pow((a * c), 4.0) / a) * (20.0 / pow(b, 7.0))), fma(-1.0, (c / b), (-2.0 * (pow(c, 3.0) / (pow(b, 5.0) / (a * a)))))));
}
function code(a, b, c) return fma(-1.0, Float64(Float64(a * Float64(c * c)) / (b ^ 3.0)), fma(-0.25, Float64(Float64((Float64(a * c) ^ 4.0) / a) * Float64(20.0 / (b ^ 7.0))), fma(-1.0, Float64(c / b), Float64(-2.0 * Float64((c ^ 3.0) / Float64((b ^ 5.0) / Float64(a * a))))))) end
code[a_, b_, c_] := N[(-1.0 * N[(N[(a * N[(c * c), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision] + N[(-0.25 * N[(N[(N[Power[N[(a * c), $MachinePrecision], 4.0], $MachinePrecision] / a), $MachinePrecision] * N[(20.0 / N[Power[b, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-1.0 * N[(c / b), $MachinePrecision] + N[(-2.0 * N[(N[Power[c, 3.0], $MachinePrecision] / N[(N[Power[b, 5.0], $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-1, \frac{a \cdot \left(c \cdot c\right)}{{b}^{3}}, \mathsf{fma}\left(-0.25, \frac{{\left(a \cdot c\right)}^{4}}{a} \cdot \frac{20}{{b}^{7}}, \mathsf{fma}\left(-1, \frac{c}{b}, -2 \cdot \frac{{c}^{3}}{\frac{{b}^{5}}{a \cdot a}}\right)\right)\right)
\end{array}
Initial program 34.6%
/-rgt-identity34.6%
metadata-eval34.6%
associate-/l*34.6%
associate-*r/34.6%
+-commutative34.6%
unsub-neg34.6%
fma-neg34.5%
associate-*l*34.5%
*-commutative34.5%
distribute-rgt-neg-in34.5%
metadata-eval34.5%
associate-/r*34.5%
metadata-eval34.5%
metadata-eval34.5%
Simplified34.5%
Taylor expanded in b around inf 95.3%
fma-def95.3%
*-commutative95.3%
unpow295.3%
fma-def95.3%
Simplified95.3%
Taylor expanded in c around 0 95.3%
+-commutative95.3%
distribute-rgt-out95.3%
associate-*l*95.3%
times-frac95.3%
Simplified95.3%
Final simplification95.3%
(FPCore (a b c)
:precision binary64
(-
(-
(fma
-0.5
(/ (* (* (pow c 4.0) (pow a 3.0)) 10.0) (pow b 7.0))
(/ (* -2.0 (pow c 3.0)) (/ (pow b 5.0) (* a a))))
(/ c b))
(/ (* a (* c c)) (pow b 3.0))))
double code(double a, double b, double c) {
return (fma(-0.5, (((pow(c, 4.0) * pow(a, 3.0)) * 10.0) / pow(b, 7.0)), ((-2.0 * pow(c, 3.0)) / (pow(b, 5.0) / (a * a)))) - (c / b)) - ((a * (c * c)) / pow(b, 3.0));
}
function code(a, b, c) return Float64(Float64(fma(-0.5, Float64(Float64(Float64((c ^ 4.0) * (a ^ 3.0)) * 10.0) / (b ^ 7.0)), Float64(Float64(-2.0 * (c ^ 3.0)) / Float64((b ^ 5.0) / Float64(a * a)))) - Float64(c / b)) - Float64(Float64(a * Float64(c * c)) / (b ^ 3.0))) end
code[a_, b_, c_] := N[(N[(N[(-0.5 * N[(N[(N[(N[Power[c, 4.0], $MachinePrecision] * N[Power[a, 3.0], $MachinePrecision]), $MachinePrecision] * 10.0), $MachinePrecision] / N[Power[b, 7.0], $MachinePrecision]), $MachinePrecision] + N[(N[(-2.0 * N[Power[c, 3.0], $MachinePrecision]), $MachinePrecision] / N[(N[Power[b, 5.0], $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(c / b), $MachinePrecision]), $MachinePrecision] - N[(N[(a * N[(c * c), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\mathsf{fma}\left(-0.5, \frac{\left({c}^{4} \cdot {a}^{3}\right) \cdot 10}{{b}^{7}}, \frac{-2 \cdot {c}^{3}}{\frac{{b}^{5}}{a \cdot a}}\right) - \frac{c}{b}\right) - \frac{a \cdot \left(c \cdot c\right)}{{b}^{3}}
\end{array}
Initial program 34.6%
/-rgt-identity34.6%
metadata-eval34.6%
associate-/l*34.6%
associate-*r/34.6%
+-commutative34.6%
unsub-neg34.6%
fma-neg34.5%
associate-*l*34.5%
*-commutative34.5%
distribute-rgt-neg-in34.5%
metadata-eval34.5%
associate-/r*34.5%
metadata-eval34.5%
metadata-eval34.5%
Simplified34.5%
fma-udef34.6%
*-commutative34.6%
metadata-eval34.6%
cancel-sign-sub-inv34.6%
associate-*l*34.6%
*-un-lft-identity34.6%
prod-diff34.5%
Applied egg-rr34.5%
+-commutative34.5%
fma-udef34.5%
*-rgt-identity34.5%
*-rgt-identity34.5%
count-234.5%
*-commutative34.5%
*-commutative34.5%
associate-*r*34.5%
*-rgt-identity34.5%
fma-neg34.5%
*-commutative34.5%
*-commutative34.5%
associate-*r*34.5%
Simplified34.5%
add-log-exp24.9%
associate-*r*24.9%
metadata-eval24.9%
cancel-sign-sub-inv24.9%
metadata-eval24.9%
Applied egg-rr24.9%
flip--24.8%
add-sqr-sqrt25.4%
fma-def25.4%
fma-def25.3%
associate-*r*25.3%
fma-def25.3%
fma-def25.3%
associate-*r*25.3%
Applied egg-rr25.3%
fma-udef25.3%
fma-udef25.4%
unpow225.4%
associate-*r*25.4%
*-commutative25.4%
+-commutative25.4%
associate-+r+25.5%
*-commutative25.5%
associate-*r*25.5%
associate-*r*25.5%
distribute-rgt-in25.5%
fma-def25.5%
distribute-rgt-out25.5%
metadata-eval25.5%
unpow225.5%
Simplified25.5%
Taylor expanded in b around inf 95.3%
+-commutative95.3%
mul-1-neg95.3%
unsub-neg95.3%
Simplified95.3%
Final simplification95.3%
(FPCore (a b c) :precision binary64 (fma 0.25 (/ c (/ b -4.0)) (fma 0.03125 (* (* a a) (/ (* (pow c 3.0) -64.0) (pow b 5.0))) (/ -0.0625 (/ (pow b 3.0) (* a (* (* c c) 16.0)))))))
double code(double a, double b, double c) {
return fma(0.25, (c / (b / -4.0)), fma(0.03125, ((a * a) * ((pow(c, 3.0) * -64.0) / pow(b, 5.0))), (-0.0625 / (pow(b, 3.0) / (a * ((c * c) * 16.0))))));
}
function code(a, b, c) return fma(0.25, Float64(c / Float64(b / -4.0)), fma(0.03125, Float64(Float64(a * a) * Float64(Float64((c ^ 3.0) * -64.0) / (b ^ 5.0))), Float64(-0.0625 / Float64((b ^ 3.0) / Float64(a * Float64(Float64(c * c) * 16.0)))))) end
code[a_, b_, c_] := N[(0.25 * N[(c / N[(b / -4.0), $MachinePrecision]), $MachinePrecision] + N[(0.03125 * N[(N[(a * a), $MachinePrecision] * N[(N[(N[Power[c, 3.0], $MachinePrecision] * -64.0), $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.0625 / N[(N[Power[b, 3.0], $MachinePrecision] / N[(a * N[(N[(c * c), $MachinePrecision] * 16.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.25, \frac{c}{\frac{b}{-4}}, \mathsf{fma}\left(0.03125, \left(a \cdot a\right) \cdot \frac{{c}^{3} \cdot -64}{{b}^{5}}, \frac{-0.0625}{\frac{{b}^{3}}{a \cdot \left(\left(c \cdot c\right) \cdot 16\right)}}\right)\right)
\end{array}
Initial program 34.6%
/-rgt-identity34.6%
metadata-eval34.6%
associate-/l*34.6%
associate-*r/34.6%
+-commutative34.6%
unsub-neg34.6%
fma-neg34.5%
associate-*l*34.5%
*-commutative34.5%
distribute-rgt-neg-in34.5%
metadata-eval34.5%
associate-/r*34.5%
metadata-eval34.5%
metadata-eval34.5%
Simplified34.5%
fma-udef34.6%
*-commutative34.6%
metadata-eval34.6%
cancel-sign-sub-inv34.6%
associate-*l*34.6%
*-un-lft-identity34.6%
prod-diff34.5%
Applied egg-rr34.5%
+-commutative34.5%
fma-udef34.5%
*-rgt-identity34.5%
*-rgt-identity34.5%
count-234.5%
*-commutative34.5%
*-commutative34.5%
associate-*r*34.5%
*-rgt-identity34.5%
fma-neg34.5%
*-commutative34.5%
*-commutative34.5%
associate-*r*34.5%
Simplified34.5%
Taylor expanded in a around 0 93.7%
Simplified93.7%
Final simplification93.7%
(FPCore (a b c) :precision binary64 (- (- (/ (* -2.0 (pow c 3.0)) (/ (pow b 5.0) (* a a))) (/ c b)) (/ (* a c) (/ (pow b 3.0) c))))
double code(double a, double b, double c) {
return (((-2.0 * pow(c, 3.0)) / (pow(b, 5.0) / (a * a))) - (c / b)) - ((a * c) / (pow(b, 3.0) / c));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((((-2.0d0) * (c ** 3.0d0)) / ((b ** 5.0d0) / (a * a))) - (c / b)) - ((a * c) / ((b ** 3.0d0) / c))
end function
public static double code(double a, double b, double c) {
return (((-2.0 * Math.pow(c, 3.0)) / (Math.pow(b, 5.0) / (a * a))) - (c / b)) - ((a * c) / (Math.pow(b, 3.0) / c));
}
def code(a, b, c): return (((-2.0 * math.pow(c, 3.0)) / (math.pow(b, 5.0) / (a * a))) - (c / b)) - ((a * c) / (math.pow(b, 3.0) / c))
function code(a, b, c) return Float64(Float64(Float64(Float64(-2.0 * (c ^ 3.0)) / Float64((b ^ 5.0) / Float64(a * a))) - Float64(c / b)) - Float64(Float64(a * c) / Float64((b ^ 3.0) / c))) end
function tmp = code(a, b, c) tmp = (((-2.0 * (c ^ 3.0)) / ((b ^ 5.0) / (a * a))) - (c / b)) - ((a * c) / ((b ^ 3.0) / c)); end
code[a_, b_, c_] := N[(N[(N[(N[(-2.0 * N[Power[c, 3.0], $MachinePrecision]), $MachinePrecision] / N[(N[Power[b, 5.0], $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(c / b), $MachinePrecision]), $MachinePrecision] - N[(N[(a * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{-2 \cdot {c}^{3}}{\frac{{b}^{5}}{a \cdot a}} - \frac{c}{b}\right) - \frac{a \cdot c}{\frac{{b}^{3}}{c}}
\end{array}
Initial program 34.6%
/-rgt-identity34.6%
metadata-eval34.6%
associate-/l*34.6%
associate-*r/34.6%
+-commutative34.6%
unsub-neg34.6%
fma-neg34.5%
associate-*l*34.5%
*-commutative34.5%
distribute-rgt-neg-in34.5%
metadata-eval34.5%
associate-/r*34.5%
metadata-eval34.5%
metadata-eval34.5%
Simplified34.5%
Taylor expanded in b around inf 93.3%
fma-def93.3%
associate-/l*93.3%
unpow293.3%
unpow293.3%
fma-def93.3%
cube-prod93.3%
associate-*r/93.3%
Simplified93.3%
Taylor expanded in c around 0 93.7%
+-commutative93.7%
mul-1-neg93.7%
unsub-neg93.7%
+-commutative93.7%
mul-1-neg93.7%
unsub-neg93.7%
unpow293.7%
associate-/l*93.7%
associate-*r/93.7%
associate-*l/93.7%
unpow293.7%
Simplified93.7%
Final simplification93.7%
(FPCore (a b c) :precision binary64 (- (/ (- c) b) (/ (* c c) (/ (pow b 3.0) a))))
double code(double a, double b, double c) {
return (-c / b) - ((c * c) / (pow(b, 3.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 / b) - ((c * c) / ((b ** 3.0d0) / a))
end function
public static double code(double a, double b, double c) {
return (-c / b) - ((c * c) / (Math.pow(b, 3.0) / a));
}
def code(a, b, c): return (-c / b) - ((c * c) / (math.pow(b, 3.0) / a))
function code(a, b, c) return Float64(Float64(Float64(-c) / b) - Float64(Float64(c * c) / Float64((b ^ 3.0) / a))) end
function tmp = code(a, b, c) tmp = (-c / b) - ((c * c) / ((b ^ 3.0) / a)); end
code[a_, b_, c_] := N[(N[((-c) / b), $MachinePrecision] - N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - \frac{c \cdot c}{\frac{{b}^{3}}{a}}
\end{array}
Initial program 34.6%
/-rgt-identity34.6%
metadata-eval34.6%
associate-/l*34.6%
associate-*r/34.6%
+-commutative34.6%
unsub-neg34.6%
fma-neg34.5%
associate-*l*34.5%
*-commutative34.5%
distribute-rgt-neg-in34.5%
metadata-eval34.5%
associate-/r*34.5%
metadata-eval34.5%
metadata-eval34.5%
Simplified34.5%
Taylor expanded in b around inf 90.2%
+-commutative90.2%
mul-1-neg90.2%
unsub-neg90.2%
mul-1-neg90.2%
distribute-neg-frac90.2%
associate-/l*90.2%
unpow290.2%
Simplified90.2%
Final simplification90.2%
(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 34.6%
/-rgt-identity34.6%
metadata-eval34.6%
associate-/l*34.6%
associate-*r/34.6%
+-commutative34.6%
unsub-neg34.6%
fma-neg34.5%
associate-*l*34.5%
*-commutative34.5%
distribute-rgt-neg-in34.5%
metadata-eval34.5%
associate-/r*34.5%
metadata-eval34.5%
metadata-eval34.5%
Simplified34.5%
Taylor expanded in b around inf 79.4%
mul-1-neg79.4%
distribute-neg-frac79.4%
Simplified79.4%
Final simplification79.4%
herbie shell --seed 2023199
(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)))