
(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) -4.0) (+ b (sqrt (- (* b b) (* c (* a 4.0)))))) (* a 2.0)))
double code(double a, double b, double c) {
return (((c * a) * -4.0) / (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 = (((c * a) * (-4.0d0)) / (b + sqrt(((b * b) - (c * (a * 4.0d0)))))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return (((c * a) * -4.0) / (b + Math.sqrt(((b * b) - (c * (a * 4.0)))))) / (a * 2.0);
}
def code(a, b, c): return (((c * a) * -4.0) / (b + math.sqrt(((b * b) - (c * (a * 4.0)))))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(Float64(c * a) * -4.0) / 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 = (((c * a) * -4.0) / (b + sqrt(((b * b) - (c * (a * 4.0)))))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision] / N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(c \cdot a\right) \cdot -4}{b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}}{a \cdot 2}
\end{array}
Initial program 17.5%
*-commutative17.5%
+-commutative17.5%
unsub-neg17.5%
fma-neg17.5%
associate-*l*17.5%
*-commutative17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
Simplified17.5%
fma-udef17.5%
*-commutative17.5%
metadata-eval17.5%
cancel-sign-sub-inv17.5%
associate-*l*17.5%
*-un-lft-identity17.5%
prod-diff17.5%
Applied egg-rr17.5%
*-rgt-identity17.5%
fma-neg17.6%
fma-udef17.6%
*-rgt-identity17.6%
*-rgt-identity17.6%
associate--r-17.5%
associate--r+17.5%
+-inverses17.5%
neg-sub017.5%
associate-*r*17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
*-commutative17.5%
associate-*r*17.5%
Simplified17.5%
flip--17.6%
add-sqr-sqrt18.3%
Applied egg-rr18.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (* (/ (* c a) (/ (+ b (sqrt (- (* b b) (* c (* a 4.0))))) -4.0)) (/ 0.5 a)))
double code(double a, double b, double c) {
return ((c * a) / ((b + sqrt(((b * b) - (c * (a * 4.0))))) / -4.0)) * (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) / ((b + sqrt(((b * b) - (c * (a * 4.0d0))))) / (-4.0d0))) * (0.5d0 / a)
end function
public static double code(double a, double b, double c) {
return ((c * a) / ((b + Math.sqrt(((b * b) - (c * (a * 4.0))))) / -4.0)) * (0.5 / a);
}
def code(a, b, c): return ((c * a) / ((b + math.sqrt(((b * b) - (c * (a * 4.0))))) / -4.0)) * (0.5 / a)
function code(a, b, c) return Float64(Float64(Float64(c * a) / Float64(Float64(b + sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0))))) / -4.0)) * Float64(0.5 / a)) end
function tmp = code(a, b, c) tmp = ((c * a) / ((b + sqrt(((b * b) - (c * (a * 4.0))))) / -4.0)) * (0.5 / a); end
code[a_, b_, c_] := N[(N[(N[(c * a), $MachinePrecision] / N[(N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / -4.0), $MachinePrecision]), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot a}{\frac{b + \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}}{-4}} \cdot \frac{0.5}{a}
\end{array}
Initial program 17.5%
*-commutative17.5%
+-commutative17.5%
unsub-neg17.5%
fma-neg17.5%
associate-*l*17.5%
*-commutative17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
Simplified17.5%
fma-udef17.5%
*-commutative17.5%
metadata-eval17.5%
cancel-sign-sub-inv17.5%
associate-*l*17.5%
*-un-lft-identity17.5%
prod-diff17.5%
Applied egg-rr17.5%
*-rgt-identity17.5%
fma-neg17.6%
fma-udef17.6%
*-rgt-identity17.6%
*-rgt-identity17.6%
associate--r-17.5%
associate--r+17.5%
+-inverses17.5%
neg-sub017.5%
associate-*r*17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
*-commutative17.5%
associate-*r*17.5%
Simplified17.5%
flip--17.6%
add-sqr-sqrt18.3%
Applied egg-rr18.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
metadata-eval99.3%
div-inv99.3%
div-inv99.3%
associate-/l*99.3%
+-commutative99.3%
clear-num99.3%
Applied egg-rr99.3%
Final simplification99.3%
(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}
\\
\left(-\frac{c}{b}\right) - \frac{c \cdot c}{\frac{{b}^{3}}{a}}
\end{array}
Initial program 17.5%
/-rgt-identity17.5%
metadata-eval17.5%
associate-/l*17.5%
associate-*r/17.5%
+-commutative17.5%
unsub-neg17.5%
fma-neg17.5%
associate-*l*17.5%
*-commutative17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
associate-/r*17.5%
metadata-eval17.5%
metadata-eval17.5%
Simplified17.5%
Taylor expanded in b around inf 95.6%
+-commutative95.6%
mul-1-neg95.6%
unsub-neg95.6%
mul-1-neg95.6%
distribute-neg-frac95.6%
associate-/l*95.6%
unpow295.6%
Simplified95.6%
Final simplification95.6%
(FPCore (a b c) :precision binary64 (/ (/ (* (* c a) -4.0) (+ (* -2.0 (/ (* c a) b)) (* b 2.0))) (* a 2.0)))
double code(double a, double b, double c) {
return (((c * a) * -4.0) / ((-2.0 * ((c * a) / b)) + (b * 2.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 = (((c * a) * (-4.0d0)) / (((-2.0d0) * ((c * a) / b)) + (b * 2.0d0))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return (((c * a) * -4.0) / ((-2.0 * ((c * a) / b)) + (b * 2.0))) / (a * 2.0);
}
def code(a, b, c): return (((c * a) * -4.0) / ((-2.0 * ((c * a) / b)) + (b * 2.0))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(Float64(c * a) * -4.0) / Float64(Float64(-2.0 * Float64(Float64(c * a) / b)) + Float64(b * 2.0))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = (((c * a) * -4.0) / ((-2.0 * ((c * a) / b)) + (b * 2.0))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[(N[(N[(c * a), $MachinePrecision] * -4.0), $MachinePrecision] / N[(N[(-2.0 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] + N[(b * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left(c \cdot a\right) \cdot -4}{-2 \cdot \frac{c \cdot a}{b} + b \cdot 2}}{a \cdot 2}
\end{array}
Initial program 17.5%
*-commutative17.5%
+-commutative17.5%
unsub-neg17.5%
fma-neg17.5%
associate-*l*17.5%
*-commutative17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
Simplified17.5%
fma-udef17.5%
*-commutative17.5%
metadata-eval17.5%
cancel-sign-sub-inv17.5%
associate-*l*17.5%
*-un-lft-identity17.5%
prod-diff17.5%
Applied egg-rr17.5%
*-rgt-identity17.5%
fma-neg17.6%
fma-udef17.6%
*-rgt-identity17.6%
*-rgt-identity17.6%
associate--r-17.5%
associate--r+17.5%
+-inverses17.5%
neg-sub017.5%
associate-*r*17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
*-commutative17.5%
associate-*r*17.5%
Simplified17.5%
flip--17.6%
add-sqr-sqrt18.3%
Applied egg-rr18.3%
Taylor expanded in b around 0 99.3%
*-commutative99.3%
Simplified99.3%
Taylor expanded in b around inf 95.3%
Final simplification95.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 17.5%
/-rgt-identity17.5%
metadata-eval17.5%
associate-/l*17.5%
associate-*r/17.5%
+-commutative17.5%
unsub-neg17.5%
fma-neg17.5%
associate-*l*17.5%
*-commutative17.5%
distribute-rgt-neg-in17.5%
metadata-eval17.5%
associate-/r*17.5%
metadata-eval17.5%
metadata-eval17.5%
Simplified17.5%
Taylor expanded in b around inf 90.5%
mul-1-neg90.5%
distribute-neg-frac90.5%
Simplified90.5%
Final simplification90.5%
herbie shell --seed 2023174
(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)))