
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (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 = (-b + sqrt(((b * b) - ((3.0d0 * a) * c)))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(3.0 * a) * c)))) / Float64(3.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(3.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \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) (* (* 3.0 a) c)))) (* 3.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (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 = (-b + sqrt(((b * b) - ((3.0d0 * a) * c)))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(3.0 * a) * c)))) / Float64(3.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(3.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a}
\end{array}
(FPCore (a b c) :precision binary64 (fma -0.5625 (/ (pow c 3.0) (/ (pow b 5.0) (* a a))) (fma -0.16666666666666666 (* (/ (pow (* c a) 4.0) (pow b 7.0)) (/ 6.328125 a)) (fma -0.5 (/ c b) (* -0.375 (/ (* c c) (/ (pow b 3.0) a)))))))
double code(double a, double b, double c) {
return fma(-0.5625, (pow(c, 3.0) / (pow(b, 5.0) / (a * a))), fma(-0.16666666666666666, ((pow((c * a), 4.0) / pow(b, 7.0)) * (6.328125 / a)), fma(-0.5, (c / b), (-0.375 * ((c * c) / (pow(b, 3.0) / a))))));
}
function code(a, b, c) return fma(-0.5625, Float64((c ^ 3.0) / Float64((b ^ 5.0) / Float64(a * a))), fma(-0.16666666666666666, Float64(Float64((Float64(c * a) ^ 4.0) / (b ^ 7.0)) * Float64(6.328125 / a)), fma(-0.5, Float64(c / b), Float64(-0.375 * Float64(Float64(c * c) / Float64((b ^ 3.0) / a)))))) end
code[a_, b_, c_] := N[(-0.5625 * N[(N[Power[c, 3.0], $MachinePrecision] / N[(N[Power[b, 5.0], $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.16666666666666666 * N[(N[(N[Power[N[(c * a), $MachinePrecision], 4.0], $MachinePrecision] / N[Power[b, 7.0], $MachinePrecision]), $MachinePrecision] * N[(6.328125 / a), $MachinePrecision]), $MachinePrecision] + N[(-0.5 * N[(c / b), $MachinePrecision] + N[(-0.375 * N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5625, \frac{{c}^{3}}{\frac{{b}^{5}}{a \cdot a}}, \mathsf{fma}\left(-0.16666666666666666, \frac{{\left(c \cdot a\right)}^{4}}{{b}^{7}} \cdot \frac{6.328125}{a}, \mathsf{fma}\left(-0.5, \frac{c}{b}, -0.375 \cdot \frac{c \cdot c}{\frac{{b}^{3}}{a}}\right)\right)\right)
\end{array}
Initial program 18.3%
/-rgt-identity18.3%
metadata-eval18.3%
associate-/l*18.3%
associate-*r/18.3%
*-commutative18.3%
associate-*l/18.3%
associate-*r/18.3%
metadata-eval18.3%
metadata-eval18.3%
times-frac18.3%
neg-mul-118.3%
distribute-rgt-neg-in18.3%
times-frac18.3%
metadata-eval18.3%
neg-mul-118.3%
Simplified18.3%
Taylor expanded in b around inf 98.2%
fma-def98.2%
associate-/l*98.2%
unpow298.2%
fma-def98.2%
Simplified98.2%
unpow-prod-down98.2%
swap-sqr98.2%
pow-prod-down98.2%
pow-prod-up98.2%
metadata-eval98.2%
metadata-eval98.2%
Applied egg-rr98.2%
Taylor expanded in c around 0 98.2%
+-commutative98.2%
distribute-rgt-out98.2%
associate-*r*98.2%
*-commutative98.2%
times-frac98.2%
Simplified98.2%
Final simplification98.2%
(FPCore (a b c) :precision binary64 (fma -0.5625 (/ (pow c 3.0) (/ (pow b 5.0) (* a a))) (fma -0.375 (/ (* c c) (/ (pow b 3.0) a)) (* -0.5 (/ c b)))))
double code(double a, double b, double c) {
return fma(-0.5625, (pow(c, 3.0) / (pow(b, 5.0) / (a * a))), fma(-0.375, ((c * c) / (pow(b, 3.0) / a)), (-0.5 * (c / b))));
}
function code(a, b, c) return fma(-0.5625, Float64((c ^ 3.0) / Float64((b ^ 5.0) / Float64(a * a))), fma(-0.375, Float64(Float64(c * c) / Float64((b ^ 3.0) / a)), Float64(-0.5 * Float64(c / b)))) end
code[a_, b_, c_] := N[(-0.5625 * N[(N[Power[c, 3.0], $MachinePrecision] / N[(N[Power[b, 5.0], $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.375 * N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision] + N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5625, \frac{{c}^{3}}{\frac{{b}^{5}}{a \cdot a}}, \mathsf{fma}\left(-0.375, \frac{c \cdot c}{\frac{{b}^{3}}{a}}, -0.5 \cdot \frac{c}{b}\right)\right)
\end{array}
Initial program 18.3%
/-rgt-identity18.3%
metadata-eval18.3%
associate-/l*18.3%
associate-*r/18.3%
*-commutative18.3%
associate-*l/18.3%
associate-*r/18.3%
metadata-eval18.3%
metadata-eval18.3%
times-frac18.3%
neg-mul-118.3%
distribute-rgt-neg-in18.3%
times-frac18.3%
metadata-eval18.3%
neg-mul-118.3%
Simplified18.3%
Taylor expanded in b around inf 97.3%
fma-def97.3%
associate-/l*97.3%
unpow297.3%
+-commutative97.3%
fma-def97.3%
associate-/l*97.3%
unpow297.3%
Simplified97.3%
Final simplification97.3%
(FPCore (a b c) :precision binary64 (+ (* -0.5 (/ c b)) (* a (* (/ c (pow b 3.0)) (* c -0.375)))))
double code(double a, double b, double c) {
return (-0.5 * (c / b)) + (a * ((c / pow(b, 3.0)) * (c * -0.375)));
}
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) * (c / b)) + (a * ((c / (b ** 3.0d0)) * (c * (-0.375d0))))
end function
public static double code(double a, double b, double c) {
return (-0.5 * (c / b)) + (a * ((c / Math.pow(b, 3.0)) * (c * -0.375)));
}
def code(a, b, c): return (-0.5 * (c / b)) + (a * ((c / math.pow(b, 3.0)) * (c * -0.375)))
function code(a, b, c) return Float64(Float64(-0.5 * Float64(c / b)) + Float64(a * Float64(Float64(c / (b ^ 3.0)) * Float64(c * -0.375)))) end
function tmp = code(a, b, c) tmp = (-0.5 * (c / b)) + (a * ((c / (b ^ 3.0)) * (c * -0.375))); end
code[a_, b_, c_] := N[(N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision] + N[(a * N[(N[(c / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision] * N[(c * -0.375), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b} + a \cdot \left(\frac{c}{{b}^{3}} \cdot \left(c \cdot -0.375\right)\right)
\end{array}
Initial program 18.3%
/-rgt-identity18.3%
metadata-eval18.3%
associate-/l*18.3%
associate-*r/18.3%
*-commutative18.3%
associate-*l/18.3%
associate-*r/18.3%
metadata-eval18.3%
metadata-eval18.3%
times-frac18.3%
neg-mul-118.3%
distribute-rgt-neg-in18.3%
times-frac18.3%
metadata-eval18.3%
neg-mul-118.3%
Simplified18.3%
Taylor expanded in b around inf 94.8%
fma-def94.8%
associate-/l*94.8%
unpow294.8%
Simplified94.8%
Taylor expanded in c around 0 95.2%
+-commutative95.2%
*-commutative95.2%
unpow295.2%
associate-*l/95.2%
*-commutative95.2%
associate-*l*95.2%
fma-def95.2%
associate-/l*95.2%
associate-/r/95.2%
Simplified95.2%
fma-udef95.2%
associate-*l*95.2%
Applied egg-rr95.2%
Final simplification95.2%
(FPCore (a b c) :precision binary64 (* c (+ (/ -0.5 b) (* -0.375 (/ c (/ (pow b 3.0) a))))))
double code(double a, double b, double c) {
return c * ((-0.5 / b) + (-0.375 * (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 * (((-0.5d0) / b) + ((-0.375d0) * (c / ((b ** 3.0d0) / a))))
end function
public static double code(double a, double b, double c) {
return c * ((-0.5 / b) + (-0.375 * (c / (Math.pow(b, 3.0) / a))));
}
def code(a, b, c): return c * ((-0.5 / b) + (-0.375 * (c / (math.pow(b, 3.0) / a))))
function code(a, b, c) return Float64(c * Float64(Float64(-0.5 / b) + Float64(-0.375 * Float64(c / Float64((b ^ 3.0) / a))))) end
function tmp = code(a, b, c) tmp = c * ((-0.5 / b) + (-0.375 * (c / ((b ^ 3.0) / a)))); end
code[a_, b_, c_] := N[(c * N[(N[(-0.5 / b), $MachinePrecision] + N[(-0.375 * N[(c / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \left(\frac{-0.5}{b} + -0.375 \cdot \frac{c}{\frac{{b}^{3}}{a}}\right)
\end{array}
Initial program 18.3%
/-rgt-identity18.3%
metadata-eval18.3%
associate-/l*18.3%
associate-*r/18.3%
*-commutative18.3%
associate-*l/18.3%
associate-*r/18.3%
metadata-eval18.3%
metadata-eval18.3%
times-frac18.3%
neg-mul-118.3%
distribute-rgt-neg-in18.3%
times-frac18.3%
metadata-eval18.3%
neg-mul-118.3%
Simplified18.3%
Taylor expanded in b around inf 94.8%
fma-def94.8%
associate-/l*94.8%
unpow294.8%
Simplified94.8%
Taylor expanded in c around 0 95.2%
+-commutative95.2%
*-commutative95.2%
unpow295.2%
associate-*l/95.2%
*-commutative95.2%
associate-*l*95.2%
fma-def95.2%
associate-/l*95.2%
associate-/r/95.2%
Simplified95.2%
Taylor expanded in a around 0 95.2%
metadata-eval95.2%
times-frac94.8%
*-commutative94.8%
associate-/l*94.7%
associate-/r/94.8%
*-commutative94.8%
*-commutative94.8%
associate-*r/94.8%
unpow294.8%
associate-*r/94.8%
*-commutative94.8%
associate-*r*94.8%
associate-*r*94.8%
*-commutative94.8%
associate-*r*94.8%
distribute-rgt-out94.8%
Simplified94.9%
Final simplification94.9%
(FPCore (a b c) :precision binary64 (* -0.5 (/ c b)))
double code(double a, double b, double c) {
return -0.5 * (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 = (-0.5d0) * (c / b)
end function
public static double code(double a, double b, double c) {
return -0.5 * (c / b);
}
def code(a, b, c): return -0.5 * (c / b)
function code(a, b, c) return Float64(-0.5 * Float64(c / b)) end
function tmp = code(a, b, c) tmp = -0.5 * (c / b); end
code[a_, b_, c_] := N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b}
\end{array}
Initial program 18.3%
/-rgt-identity18.3%
metadata-eval18.3%
associate-/l*18.3%
associate-*r/18.3%
*-commutative18.3%
associate-*l/18.3%
associate-*r/18.3%
metadata-eval18.3%
metadata-eval18.3%
times-frac18.3%
neg-mul-118.3%
distribute-rgt-neg-in18.3%
times-frac18.3%
metadata-eval18.3%
neg-mul-118.3%
Simplified18.3%
Taylor expanded in b around inf 90.2%
Final simplification90.2%
herbie shell --seed 2023258
(FPCore (a b c)
:name "Cubic critical, 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) (* (* 3.0 a) c)))) (* 3.0 a)))