
(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 7 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 (/ (/ (* -2.0 (* c a)) a) (+ b (sqrt (fma b b (* c (* a -4.0)))))))
double code(double a, double b, double c) {
return ((-2.0 * (c * a)) / a) / (b + sqrt(fma(b, b, (c * (a * -4.0)))));
}
function code(a, b, c) return Float64(Float64(Float64(-2.0 * Float64(c * a)) / a) / Float64(b + sqrt(fma(b, b, Float64(c * Float64(a * -4.0)))))) end
code[a_, b_, c_] := N[(N[(N[(-2.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision] / N[(b + N[Sqrt[N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{-2 \cdot \left(c \cdot a\right)}{a}}{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}
\end{array}
Initial program 31.7%
/-rgt-identity31.7%
metadata-eval31.7%
associate-/l*31.7%
associate-*r/31.7%
+-commutative31.7%
unsub-neg31.7%
fma-neg31.8%
associate-*l*31.8%
*-commutative31.8%
distribute-rgt-neg-in31.8%
metadata-eval31.8%
associate-/r*31.8%
metadata-eval31.8%
metadata-eval31.8%
Simplified31.8%
fma-udef31.7%
*-commutative31.7%
Applied egg-rr31.7%
flip--31.7%
add-sqr-sqrt32.8%
fma-def32.6%
*-commutative32.6%
fma-def32.6%
*-commutative32.6%
Applied egg-rr32.6%
*-commutative32.6%
+-commutative32.6%
metadata-eval32.6%
distribute-lft-neg-in32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
distribute-lft-neg-in32.6%
metadata-eval32.6%
*-commutative32.6%
Simplified32.6%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
associate-*l/99.4%
associate-*r*99.4%
Applied egg-rr99.4%
associate-*r/99.6%
*-commutative99.6%
associate-*r*99.6%
*-commutative99.6%
associate-*r*99.6%
metadata-eval99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (a b c) :precision binary64 (/ (* -2.0 (* c a)) (* a (+ b (sqrt (fma b b (* c (* a -4.0))))))))
double code(double a, double b, double c) {
return (-2.0 * (c * a)) / (a * (b + sqrt(fma(b, b, (c * (a * -4.0))))));
}
function code(a, b, c) return Float64(Float64(-2.0 * Float64(c * a)) / Float64(a * Float64(b + sqrt(fma(b, b, Float64(c * Float64(a * -4.0))))))) end
code[a_, b_, c_] := N[(N[(-2.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(a * N[(b + N[Sqrt[N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2 \cdot \left(c \cdot a\right)}{a \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}\right)}
\end{array}
Initial program 31.7%
/-rgt-identity31.7%
metadata-eval31.7%
associate-/l*31.7%
associate-*r/31.7%
+-commutative31.7%
unsub-neg31.7%
fma-neg31.8%
associate-*l*31.8%
*-commutative31.8%
distribute-rgt-neg-in31.8%
metadata-eval31.8%
associate-/r*31.8%
metadata-eval31.8%
metadata-eval31.8%
Simplified31.8%
fma-udef31.7%
*-commutative31.7%
Applied egg-rr31.7%
flip--31.7%
add-sqr-sqrt32.8%
fma-def32.6%
*-commutative32.6%
fma-def32.6%
*-commutative32.6%
Applied egg-rr32.6%
*-commutative32.6%
+-commutative32.6%
metadata-eval32.6%
distribute-lft-neg-in32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
distribute-lft-neg-in32.6%
metadata-eval32.6%
*-commutative32.6%
Simplified32.6%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
frac-times99.4%
associate-*r*99.4%
Applied egg-rr99.4%
*-commutative99.4%
associate-*r*99.4%
*-commutative99.4%
associate-*r*99.4%
metadata-eval99.4%
*-commutative99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (* (/ (* c (* a -4.0)) (+ b (+ b (* -2.0 (* a (+ (* a (/ c (/ (pow b 3.0) c))) (/ c b))))))) (/ 0.5 a)))
double code(double a, double b, double c) {
return ((c * (a * -4.0)) / (b + (b + (-2.0 * (a * ((a * (c / (pow(b, 3.0) / c))) + (c / b))))))) * (0.5 / 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 * (a * (-4.0d0))) / (b + (b + ((-2.0d0) * (a * ((a * (c / ((b ** 3.0d0) / c))) + (c / b))))))) * (0.5d0 / a)
end function
public static double code(double a, double b, double c) {
return ((c * (a * -4.0)) / (b + (b + (-2.0 * (a * ((a * (c / (Math.pow(b, 3.0) / c))) + (c / b))))))) * (0.5 / a);
}
def code(a, b, c): return ((c * (a * -4.0)) / (b + (b + (-2.0 * (a * ((a * (c / (math.pow(b, 3.0) / c))) + (c / b))))))) * (0.5 / a)
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * -4.0)) / Float64(b + Float64(b + Float64(-2.0 * Float64(a * Float64(Float64(a * Float64(c / Float64((b ^ 3.0) / c))) + Float64(c / b))))))) * Float64(0.5 / a)) end
function tmp = code(a, b, c) tmp = ((c * (a * -4.0)) / (b + (b + (-2.0 * (a * ((a * (c / ((b ^ 3.0) / c))) + (c / b))))))) * (0.5 / a); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision] / N[(b + N[(b + N[(-2.0 * N[(a * N[(N[(a * N[(c / N[(N[Power[b, 3.0], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \left(a \cdot -4\right)}{b + \left(b + -2 \cdot \left(a \cdot \left(a \cdot \frac{c}{\frac{{b}^{3}}{c}} + \frac{c}{b}\right)\right)\right)} \cdot \frac{0.5}{a}
\end{array}
Initial program 31.7%
/-rgt-identity31.7%
metadata-eval31.7%
associate-/l*31.7%
associate-*r/31.7%
+-commutative31.7%
unsub-neg31.7%
fma-neg31.8%
associate-*l*31.8%
*-commutative31.8%
distribute-rgt-neg-in31.8%
metadata-eval31.8%
associate-/r*31.8%
metadata-eval31.8%
metadata-eval31.8%
Simplified31.8%
fma-udef31.7%
*-commutative31.7%
Applied egg-rr31.7%
flip--31.7%
add-sqr-sqrt32.8%
fma-def32.6%
*-commutative32.6%
fma-def32.6%
*-commutative32.6%
Applied egg-rr32.6%
*-commutative32.6%
+-commutative32.6%
metadata-eval32.6%
distribute-lft-neg-in32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
distribute-lft-neg-in32.6%
metadata-eval32.6%
*-commutative32.6%
Simplified32.6%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
Taylor expanded in b around inf 92.1%
+-commutative92.1%
associate-*l/92.1%
associate-+l+92.1%
distribute-lft-in92.1%
associate-*l/92.1%
unpow292.1%
unpow292.1%
+-commutative92.1%
associate-*r*92.1%
distribute-rgt-out92.1%
associate-/l*92.1%
Simplified92.1%
Final simplification92.1%
(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(a / Float64((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[(a / N[(N[Power[b, 3.0], $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - \frac{a}{\frac{{b}^{3}}{c \cdot c}}
\end{array}
Initial program 31.7%
neg-sub031.7%
associate-+l-31.7%
sub0-neg31.7%
neg-mul-131.7%
associate-*l/31.7%
*-commutative31.7%
associate-/r*31.7%
/-rgt-identity31.7%
metadata-eval31.7%
Simplified31.7%
Taylor expanded in b around inf 89.4%
+-commutative89.4%
mul-1-neg89.4%
unsub-neg89.4%
associate-*r/89.4%
neg-mul-189.4%
*-commutative89.4%
associate-/l*89.4%
unpow289.4%
Simplified89.4%
Final simplification89.4%
(FPCore (a b c) :precision binary64 (* (/ 0.5 a) (/ (* c (* a -4.0)) (+ b (+ b (* -2.0 (/ (* c a) b)))))))
double code(double a, double b, double c) {
return (0.5 / a) * ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / b)))));
}
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 / a) * ((c * (a * (-4.0d0))) / (b + (b + ((-2.0d0) * ((c * a) / b)))))
end function
public static double code(double a, double b, double c) {
return (0.5 / a) * ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / b)))));
}
def code(a, b, c): return (0.5 / a) * ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / b)))))
function code(a, b, c) return Float64(Float64(0.5 / a) * Float64(Float64(c * Float64(a * -4.0)) / Float64(b + Float64(b + Float64(-2.0 * Float64(Float64(c * a) / b)))))) end
function tmp = code(a, b, c) tmp = (0.5 / a) * ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / b))))); end
code[a_, b_, c_] := N[(N[(0.5 / a), $MachinePrecision] * N[(N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision] / N[(b + N[(b + N[(-2.0 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{a} \cdot \frac{c \cdot \left(a \cdot -4\right)}{b + \left(b + -2 \cdot \frac{c \cdot a}{b}\right)}
\end{array}
Initial program 31.7%
/-rgt-identity31.7%
metadata-eval31.7%
associate-/l*31.7%
associate-*r/31.7%
+-commutative31.7%
unsub-neg31.7%
fma-neg31.8%
associate-*l*31.8%
*-commutative31.8%
distribute-rgt-neg-in31.8%
metadata-eval31.8%
associate-/r*31.8%
metadata-eval31.8%
metadata-eval31.8%
Simplified31.8%
fma-udef31.7%
*-commutative31.7%
Applied egg-rr31.7%
flip--31.7%
add-sqr-sqrt32.8%
fma-def32.6%
*-commutative32.6%
fma-def32.6%
*-commutative32.6%
Applied egg-rr32.6%
*-commutative32.6%
+-commutative32.6%
metadata-eval32.6%
distribute-lft-neg-in32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
distribute-lft-neg-in32.6%
metadata-eval32.6%
*-commutative32.6%
Simplified32.6%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
Taylor expanded in b around inf 89.3%
Final simplification89.3%
(FPCore (a b c) :precision binary64 (* (/ 0.5 a) (/ (* c (* a -4.0)) (+ (* -2.0 (/ (* c a) b)) (* b 2.0)))))
double code(double a, double b, double c) {
return (0.5 / a) * ((c * (a * -4.0)) / ((-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 / a) * ((c * (a * (-4.0d0))) / (((-2.0d0) * ((c * a) / b)) + (b * 2.0d0)))
end function
public static double code(double a, double b, double c) {
return (0.5 / a) * ((c * (a * -4.0)) / ((-2.0 * ((c * a) / b)) + (b * 2.0)));
}
def code(a, b, c): return (0.5 / a) * ((c * (a * -4.0)) / ((-2.0 * ((c * a) / b)) + (b * 2.0)))
function code(a, b, c) return Float64(Float64(0.5 / a) * Float64(Float64(c * Float64(a * -4.0)) / Float64(Float64(-2.0 * Float64(Float64(c * a) / b)) + Float64(b * 2.0)))) end
function tmp = code(a, b, c) tmp = (0.5 / a) * ((c * (a * -4.0)) / ((-2.0 * ((c * a) / b)) + (b * 2.0))); end
code[a_, b_, c_] := N[(N[(0.5 / a), $MachinePrecision] * N[(N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision] / N[(N[(-2.0 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] + N[(b * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{a} \cdot \frac{c \cdot \left(a \cdot -4\right)}{-2 \cdot \frac{c \cdot a}{b} + b \cdot 2}
\end{array}
Initial program 31.7%
/-rgt-identity31.7%
metadata-eval31.7%
associate-/l*31.7%
associate-*r/31.7%
+-commutative31.7%
unsub-neg31.7%
fma-neg31.8%
associate-*l*31.8%
*-commutative31.8%
distribute-rgt-neg-in31.8%
metadata-eval31.8%
associate-/r*31.8%
metadata-eval31.8%
metadata-eval31.8%
Simplified31.8%
fma-udef31.7%
*-commutative31.7%
Applied egg-rr31.7%
flip--31.7%
add-sqr-sqrt32.8%
fma-def32.6%
*-commutative32.6%
fma-def32.6%
*-commutative32.6%
Applied egg-rr32.6%
*-commutative32.6%
+-commutative32.6%
metadata-eval32.6%
distribute-lft-neg-in32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
fma-neg32.6%
associate-*r*32.6%
*-commutative32.6%
associate-*r*32.6%
distribute-lft-neg-in32.6%
metadata-eval32.6%
*-commutative32.6%
Simplified32.6%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
Taylor expanded in b around inf 89.3%
Final simplification89.3%
(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.7%
neg-sub031.7%
associate-+l-31.7%
sub0-neg31.7%
neg-mul-131.7%
associate-*l/31.7%
*-commutative31.7%
associate-/r*31.7%
/-rgt-identity31.7%
metadata-eval31.7%
Simplified31.7%
Taylor expanded in b around inf 81.0%
associate-*r/81.0%
neg-mul-181.0%
Simplified81.0%
Final simplification81.0%
herbie shell --seed 2023229
(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)))