
(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 (/ (* (/ c (+ b (sqrt (fma b b (* c (* a -4.0)))))) (* a -2.0)) a))
double code(double a, double b, double c) {
return ((c / (b + sqrt(fma(b, b, (c * (a * -4.0)))))) * (a * -2.0)) / a;
}
function code(a, b, c) return Float64(Float64(Float64(c / Float64(b + sqrt(fma(b, b, Float64(c * Float64(a * -4.0)))))) * Float64(a * -2.0)) / a) end
code[a_, b_, c_] := N[(N[(N[(c / N[(b + N[Sqrt[N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(a * -2.0), $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c}{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}} \cdot \left(a \cdot -2\right)}{a}
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
associate-/l*31.3%
associate-*r/31.3%
+-commutative31.3%
unsub-neg31.3%
fma-neg31.5%
associate-*l*31.5%
*-commutative31.5%
distribute-rgt-neg-in31.5%
metadata-eval31.5%
associate-/r*31.5%
metadata-eval31.5%
metadata-eval31.5%
Simplified31.5%
fma-udef31.3%
*-commutative31.3%
Applied egg-rr31.3%
flip--31.4%
add-sqr-sqrt32.3%
fma-def32.4%
*-commutative32.4%
fma-def32.4%
*-commutative32.4%
Applied egg-rr32.4%
*-commutative32.4%
+-commutative32.4%
*-commutative32.4%
Simplified32.4%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
associate-*r/99.3%
associate-/l*99.3%
associate-*r*99.3%
Applied egg-rr99.3%
associate-/r/99.6%
associate-*l*99.6%
associate-*l*99.6%
metadata-eval99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (a b c) :precision binary64 (* (/ c a) (/ (* a -2.0) (+ b (sqrt (fma b b (* c (* a -4.0))))))))
double code(double a, double b, double c) {
return (c / a) * ((a * -2.0) / (b + sqrt(fma(b, b, (c * (a * -4.0))))));
}
function code(a, b, c) return Float64(Float64(c / a) * Float64(Float64(a * -2.0) / Float64(b + sqrt(fma(b, b, Float64(c * Float64(a * -4.0))))))) end
code[a_, b_, c_] := N[(N[(c / a), $MachinePrecision] * N[(N[(a * -2.0), $MachinePrecision] / N[(b + N[Sqrt[N[(b * b + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{a} \cdot \frac{a \cdot -2}{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
associate-/l*31.3%
associate-*r/31.3%
+-commutative31.3%
unsub-neg31.3%
fma-neg31.5%
associate-*l*31.5%
*-commutative31.5%
distribute-rgt-neg-in31.5%
metadata-eval31.5%
associate-/r*31.5%
metadata-eval31.5%
metadata-eval31.5%
Simplified31.5%
fma-udef31.3%
*-commutative31.3%
Applied egg-rr31.3%
flip--31.4%
add-sqr-sqrt32.3%
fma-def32.4%
*-commutative32.4%
fma-def32.4%
*-commutative32.4%
Applied egg-rr32.4%
*-commutative32.4%
+-commutative32.4%
*-commutative32.4%
Simplified32.4%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
frac-times99.3%
associate-*r*99.3%
Applied egg-rr99.3%
associate-*l*99.3%
*-commutative99.3%
times-frac99.3%
associate-*l*99.3%
metadata-eval99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (- (/ (- c) b) (* a (/ c (/ (pow b 3.0) c)))))
double code(double a, double b, double c) {
return (-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 = (-c / b) - (a * (c / ((b ** 3.0d0) / c)))
end function
public static double code(double a, double b, double c) {
return (-c / b) - (a * (c / (Math.pow(b, 3.0) / c)));
}
def code(a, b, c): return (-c / b) - (a * (c / (math.pow(b, 3.0) / c)))
function code(a, b, c) return Float64(Float64(Float64(-c) / b) - Float64(a * Float64(c / Float64((b ^ 3.0) / c)))) end
function tmp = code(a, b, c) tmp = (-c / b) - (a * (c / ((b ^ 3.0) / c))); end
code[a_, b_, c_] := N[(N[((-c) / b), $MachinePrecision] - N[(a * N[(c / N[(N[Power[b, 3.0], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b} - a \cdot \frac{c}{\frac{{b}^{3}}{c}}
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
associate-/l*31.3%
associate-*r/31.3%
+-commutative31.3%
unsub-neg31.3%
fma-neg31.5%
associate-*l*31.5%
*-commutative31.5%
distribute-rgt-neg-in31.5%
metadata-eval31.5%
associate-/r*31.5%
metadata-eval31.5%
metadata-eval31.5%
Simplified31.5%
fma-udef31.3%
*-commutative31.3%
Applied egg-rr31.3%
Taylor expanded in b around inf 90.5%
+-commutative90.5%
mul-1-neg90.5%
unsub-neg90.5%
associate-*r/90.5%
mul-1-neg90.5%
associate-/l*90.5%
associate-/r/90.5%
unpow290.5%
associate-/l*90.5%
Simplified90.5%
Final simplification90.5%
(FPCore (a b c) :precision binary64 (* (/ (* c (* a -4.0)) (+ b (+ b (* -2.0 (/ (* c a) b))))) (/ 0.5 a)))
double code(double a, double b, double c) {
return ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / 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) * ((c * a) / 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 * ((c * a) / b))))) * (0.5 / a);
}
def code(a, b, c): return ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / 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(Float64(c * a) / b))))) * Float64(0.5 / a)) end
function tmp = code(a, b, c) tmp = ((c * (a * -4.0)) / (b + (b + (-2.0 * ((c * a) / 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[(N[(c * a), $MachinePrecision] / b), $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 \frac{c \cdot a}{b}\right)} \cdot \frac{0.5}{a}
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
associate-/l*31.3%
associate-*r/31.3%
+-commutative31.3%
unsub-neg31.3%
fma-neg31.5%
associate-*l*31.5%
*-commutative31.5%
distribute-rgt-neg-in31.5%
metadata-eval31.5%
associate-/r*31.5%
metadata-eval31.5%
metadata-eval31.5%
Simplified31.5%
fma-udef31.3%
*-commutative31.3%
Applied egg-rr31.3%
flip--31.4%
add-sqr-sqrt32.3%
fma-def32.4%
*-commutative32.4%
fma-def32.4%
*-commutative32.4%
Applied egg-rr32.4%
*-commutative32.4%
+-commutative32.4%
*-commutative32.4%
Simplified32.4%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
associate-*r*99.3%
Simplified99.3%
Taylor expanded in b around inf 90.4%
Final simplification90.4%
(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.3%
neg-sub031.3%
associate-+l-31.3%
sub0-neg31.3%
neg-mul-131.3%
associate-*l/31.3%
*-commutative31.3%
associate-/r*31.3%
/-rgt-identity31.3%
metadata-eval31.3%
Simplified31.3%
Taylor expanded in b around inf 81.1%
associate-*r/81.1%
neg-mul-181.1%
Simplified81.1%
Final simplification81.1%
(FPCore (a b c) :precision binary64 (/ b a))
double code(double a, double b, double c) {
return b / 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 / a
end function
public static double code(double a, double b, double c) {
return b / a;
}
def code(a, b, c): return b / a
function code(a, b, c) return Float64(b / a) end
function tmp = code(a, b, c) tmp = b / a; end
code[a_, b_, c_] := N[(b / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{a}
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
associate-/l*31.3%
associate-*r/31.3%
+-commutative31.3%
unsub-neg31.3%
fma-neg31.5%
associate-*l*31.5%
*-commutative31.5%
distribute-rgt-neg-in31.5%
metadata-eval31.5%
associate-/r*31.5%
metadata-eval31.5%
metadata-eval31.5%
Simplified31.5%
fma-udef31.3%
*-commutative31.3%
Applied egg-rr31.3%
flip--31.4%
add-sqr-sqrt32.3%
fma-def32.4%
*-commutative32.4%
fma-def32.4%
*-commutative32.4%
Applied egg-rr32.4%
*-commutative32.4%
+-commutative32.4%
*-commutative32.4%
Simplified32.4%
Taylor expanded in b around inf 25.6%
Taylor expanded in b around 0 1.6%
Final simplification1.6%
herbie shell --seed 2023255
(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)))