
(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 2.0) (- (- b) (sqrt (fma -4.0 (* c a) (pow b 2.0))))))
double code(double a, double b, double c) {
return (c * 2.0) / (-b - sqrt(fma(-4.0, (c * a), pow(b, 2.0))));
}
function code(a, b, c) return Float64(Float64(c * 2.0) / Float64(Float64(-b) - sqrt(fma(-4.0, Float64(c * a), (b ^ 2.0))))) end
code[a_, b_, c_] := N[(N[(c * 2.0), $MachinePrecision] / N[((-b) - N[Sqrt[N[(-4.0 * N[(c * a), $MachinePrecision] + N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot 2}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, c \cdot a, {b}^{2}\right)}}
\end{array}
Initial program 33.5%
*-commutative33.5%
Simplified33.5%
add-exp-log33.5%
Applied egg-rr33.5%
flip-+33.5%
pow233.5%
add-sqr-sqrt34.4%
pow234.4%
*-commutative34.4%
rem-exp-log34.5%
*-commutative34.5%
pow234.5%
*-commutative34.5%
rem-exp-log34.5%
*-commutative34.5%
Applied egg-rr34.5%
*-un-lft-identity34.5%
associate-/l/34.5%
associate--r-99.3%
neg-mul-199.3%
unpow-prod-down99.3%
metadata-eval99.3%
*-un-lft-identity99.3%
associate-*r*99.3%
*-commutative99.3%
associate-*r*99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.6%
+-inverses99.6%
associate-*l*99.6%
*-commutative99.6%
*-commutative99.6%
*-commutative99.6%
cancel-sign-sub-inv99.6%
metadata-eval99.6%
+-commutative99.6%
fma-define99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in c around 0 99.7%
*-commutative99.7%
Simplified99.7%
(FPCore (a b c) :precision binary64 (let* ((t_0 (* c (* a 4.0)))) (/ (/ t_0 (- (- b) (sqrt (- (pow b 2.0) t_0)))) (* 2.0 a))))
double code(double a, double b, double c) {
double t_0 = c * (a * 4.0);
return (t_0 / (-b - sqrt((pow(b, 2.0) - t_0)))) / (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
real(8) :: t_0
t_0 = c * (a * 4.0d0)
code = (t_0 / (-b - sqrt(((b ** 2.0d0) - t_0)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
double t_0 = c * (a * 4.0);
return (t_0 / (-b - Math.sqrt((Math.pow(b, 2.0) - t_0)))) / (2.0 * a);
}
def code(a, b, c): t_0 = c * (a * 4.0) return (t_0 / (-b - math.sqrt((math.pow(b, 2.0) - t_0)))) / (2.0 * a)
function code(a, b, c) t_0 = Float64(c * Float64(a * 4.0)) return Float64(Float64(t_0 / Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) - t_0)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) t_0 = c * (a * 4.0); tmp = (t_0 / (-b - sqrt(((b ^ 2.0) - t_0)))) / (2.0 * a); end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$0 / N[((-b) - N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot 4\right)\\
\frac{\frac{t\_0}{\left(-b\right) - \sqrt{{b}^{2} - t\_0}}}{2 \cdot a}
\end{array}
\end{array}
Initial program 33.5%
*-commutative33.5%
Simplified33.5%
add-exp-log33.5%
Applied egg-rr33.5%
flip-+33.5%
pow233.5%
add-sqr-sqrt34.4%
pow234.4%
*-commutative34.4%
rem-exp-log34.5%
*-commutative34.5%
pow234.5%
*-commutative34.5%
rem-exp-log34.5%
*-commutative34.5%
Applied egg-rr34.5%
Taylor expanded in b around 0 99.4%
*-commutative99.4%
*-commutative99.4%
associate-*l*99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 4.0)) (* 2.0 a)) (- (- b) (sqrt (fma -4.0 (* c a) (* b b))))))
double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (2.0 * a)) / (-b - sqrt(fma(-4.0, (c * a), (b * b))));
}
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 4.0)) / Float64(2.0 * a)) / Float64(Float64(-b) - sqrt(fma(-4.0, Float64(c * a), Float64(b * b))))) end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(-4.0 * N[(c * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 4\right)}{2 \cdot a}}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, c \cdot a, b \cdot b\right)}}
\end{array}
Initial program 33.5%
*-commutative33.5%
Simplified33.5%
add-exp-log33.5%
Applied egg-rr33.5%
flip-+33.5%
pow233.5%
add-sqr-sqrt34.4%
pow234.4%
*-commutative34.4%
rem-exp-log34.5%
*-commutative34.5%
pow234.5%
*-commutative34.5%
rem-exp-log34.5%
*-commutative34.5%
Applied egg-rr34.5%
*-un-lft-identity34.5%
associate-/l/34.5%
associate--r-99.3%
neg-mul-199.3%
unpow-prod-down99.3%
metadata-eval99.3%
*-un-lft-identity99.3%
associate-*r*99.3%
*-commutative99.3%
associate-*r*99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.6%
+-inverses99.6%
associate-*l*99.6%
*-commutative99.6%
*-commutative99.6%
*-commutative99.6%
cancel-sign-sub-inv99.6%
metadata-eval99.6%
+-commutative99.6%
fma-define99.6%
*-commutative99.6%
Simplified99.6%
unpow299.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 4.0)) (* 2.0 a)) (* b (- (* 2.0 (/ (* c a) (pow b 2.0))) 2.0))))
double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (2.0 * a)) / (b * ((2.0 * ((c * a) / pow(b, 2.0))) - 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 = ((c * (a * 4.0d0)) / (2.0d0 * a)) / (b * ((2.0d0 * ((c * a) / (b ** 2.0d0))) - 2.0d0))
end function
public static double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (2.0 * a)) / (b * ((2.0 * ((c * a) / Math.pow(b, 2.0))) - 2.0));
}
def code(a, b, c): return ((c * (a * 4.0)) / (2.0 * a)) / (b * ((2.0 * ((c * a) / math.pow(b, 2.0))) - 2.0))
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 4.0)) / Float64(2.0 * a)) / Float64(b * Float64(Float64(2.0 * Float64(Float64(c * a) / (b ^ 2.0))) - 2.0))) end
function tmp = code(a, b, c) tmp = ((c * (a * 4.0)) / (2.0 * a)) / (b * ((2.0 * ((c * a) / (b ^ 2.0))) - 2.0)); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] / N[(b * N[(N[(2.0 * N[(N[(c * a), $MachinePrecision] / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 4\right)}{2 \cdot a}}{b \cdot \left(2 \cdot \frac{c \cdot a}{{b}^{2}} - 2\right)}
\end{array}
Initial program 33.5%
*-commutative33.5%
Simplified33.5%
add-exp-log33.5%
Applied egg-rr33.5%
flip-+33.5%
pow233.5%
add-sqr-sqrt34.4%
pow234.4%
*-commutative34.4%
rem-exp-log34.5%
*-commutative34.5%
pow234.5%
*-commutative34.5%
rem-exp-log34.5%
*-commutative34.5%
Applied egg-rr34.5%
*-un-lft-identity34.5%
associate-/l/34.5%
associate--r-99.3%
neg-mul-199.3%
unpow-prod-down99.3%
metadata-eval99.3%
*-un-lft-identity99.3%
associate-*r*99.3%
*-commutative99.3%
associate-*r*99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.6%
+-inverses99.6%
associate-*l*99.6%
*-commutative99.6%
*-commutative99.6%
*-commutative99.6%
cancel-sign-sub-inv99.6%
metadata-eval99.6%
+-commutative99.6%
fma-define99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in b around inf 88.9%
Final simplification88.9%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 4.0)) (* 2.0 a)) (* 2.0 (- (* a (/ c b)) b))))
double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (2.0 * a)) / (2.0 * ((a * (c / 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 * (a * 4.0d0)) / (2.0d0 * a)) / (2.0d0 * ((a * (c / b)) - b))
end function
public static double code(double a, double b, double c) {
return ((c * (a * 4.0)) / (2.0 * a)) / (2.0 * ((a * (c / b)) - b));
}
def code(a, b, c): return ((c * (a * 4.0)) / (2.0 * a)) / (2.0 * ((a * (c / b)) - b))
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 4.0)) / Float64(2.0 * a)) / Float64(2.0 * Float64(Float64(a * Float64(c / b)) - b))) end
function tmp = code(a, b, c) tmp = ((c * (a * 4.0)) / (2.0 * a)) / (2.0 * ((a * (c / b)) - b)); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] / N[(2.0 * N[(N[(a * N[(c / b), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 4\right)}{2 \cdot a}}{2 \cdot \left(a \cdot \frac{c}{b} - b\right)}
\end{array}
Initial program 33.5%
*-commutative33.5%
Simplified33.5%
add-exp-log33.5%
Applied egg-rr33.5%
flip-+33.5%
pow233.5%
add-sqr-sqrt34.4%
pow234.4%
*-commutative34.4%
rem-exp-log34.5%
*-commutative34.5%
pow234.5%
*-commutative34.5%
rem-exp-log34.5%
*-commutative34.5%
Applied egg-rr34.5%
*-un-lft-identity34.5%
associate-/l/34.5%
associate--r-99.3%
neg-mul-199.3%
unpow-prod-down99.3%
metadata-eval99.3%
*-un-lft-identity99.3%
associate-*r*99.3%
*-commutative99.3%
associate-*r*99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.6%
+-inverses99.6%
associate-*l*99.6%
*-commutative99.6%
*-commutative99.6%
*-commutative99.6%
cancel-sign-sub-inv99.6%
metadata-eval99.6%
+-commutative99.6%
fma-define99.6%
*-commutative99.6%
Simplified99.6%
Taylor expanded in c around 0 88.9%
distribute-lft-out--88.9%
associate-*r/88.9%
*-commutative88.9%
Simplified88.9%
Final simplification88.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(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 33.5%
*-commutative33.5%
Simplified33.5%
Taylor expanded in b around inf 79.5%
associate-*r/79.5%
mul-1-neg79.5%
Simplified79.5%
Final simplification79.5%
herbie shell --seed 2024172
(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)))