
(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 (/ (/ (+ (* c (* a (pow 27.0 0.3333333333333333))) (- (pow b 2.0) (pow b 2.0))) (- (+ b (sqrt (- (pow b 2.0) (* 3.0 (* c a))))))) (* a 3.0)))
double code(double a, double b, double c) {
return (((c * (a * pow(27.0, 0.3333333333333333))) + (pow(b, 2.0) - pow(b, 2.0))) / -(b + sqrt((pow(b, 2.0) - (3.0 * (c * a)))))) / (a * 3.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 * (27.0d0 ** 0.3333333333333333d0))) + ((b ** 2.0d0) - (b ** 2.0d0))) / -(b + sqrt(((b ** 2.0d0) - (3.0d0 * (c * a)))))) / (a * 3.0d0)
end function
public static double code(double a, double b, double c) {
return (((c * (a * Math.pow(27.0, 0.3333333333333333))) + (Math.pow(b, 2.0) - Math.pow(b, 2.0))) / -(b + Math.sqrt((Math.pow(b, 2.0) - (3.0 * (c * a)))))) / (a * 3.0);
}
def code(a, b, c): return (((c * (a * math.pow(27.0, 0.3333333333333333))) + (math.pow(b, 2.0) - math.pow(b, 2.0))) / -(b + math.sqrt((math.pow(b, 2.0) - (3.0 * (c * a)))))) / (a * 3.0)
function code(a, b, c) return Float64(Float64(Float64(Float64(c * Float64(a * (27.0 ^ 0.3333333333333333))) + Float64((b ^ 2.0) - (b ^ 2.0))) / Float64(-Float64(b + sqrt(Float64((b ^ 2.0) - Float64(3.0 * Float64(c * a))))))) / Float64(a * 3.0)) end
function tmp = code(a, b, c) tmp = (((c * (a * (27.0 ^ 0.3333333333333333))) + ((b ^ 2.0) - (b ^ 2.0))) / -(b + sqrt(((b ^ 2.0) - (3.0 * (c * a)))))) / (a * 3.0); end
code[a_, b_, c_] := N[(N[(N[(N[(c * N[(a * N[Power[27.0, 0.3333333333333333], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Power[b, 2.0], $MachinePrecision] - N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / (-N[(b + N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] - N[(3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision])), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot {27}^{0.3333333333333333}\right) + \left({b}^{2} - {b}^{2}\right)}{-\left(b + \sqrt{{b}^{2} - 3 \cdot \left(c \cdot a\right)}\right)}}{a \cdot 3}
\end{array}
Initial program 16.5%
add-cbrt-cube16.5%
pow1/316.4%
pow316.4%
associate-*l*16.4%
unpow-prod-down16.4%
metadata-eval16.4%
Applied egg-rr16.4%
flip-+16.5%
pow216.5%
add-sqr-sqrt17.0%
pow217.0%
unpow1/317.0%
*-commutative17.0%
cbrt-prod17.1%
unpow317.0%
add-cbrt-cube17.0%
Applied egg-rr17.1%
associate--r-98.6%
+-commutative98.6%
*-commutative98.6%
associate-*l*98.6%
unpow298.6%
sqr-neg98.6%
unpow298.6%
*-commutative98.6%
associate-*l*98.6%
Simplified98.6%
pow1/399.3%
Applied egg-rr99.3%
Taylor expanded in c around 0 99.3%
*-commutative99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (/ (/ (+ (- (pow b 2.0) (pow b 2.0)) (* 3.0 (* c a))) (- (- b) (sqrt (- (pow b 2.0) (* c (* a (cbrt 27.0))))))) (* a 3.0)))
double code(double a, double b, double c) {
return (((pow(b, 2.0) - pow(b, 2.0)) + (3.0 * (c * a))) / (-b - sqrt((pow(b, 2.0) - (c * (a * cbrt(27.0))))))) / (a * 3.0);
}
public static double code(double a, double b, double c) {
return (((Math.pow(b, 2.0) - Math.pow(b, 2.0)) + (3.0 * (c * a))) / (-b - Math.sqrt((Math.pow(b, 2.0) - (c * (a * Math.cbrt(27.0))))))) / (a * 3.0);
}
function code(a, b, c) return Float64(Float64(Float64(Float64((b ^ 2.0) - (b ^ 2.0)) + Float64(3.0 * Float64(c * a))) / Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) - Float64(c * Float64(a * cbrt(27.0))))))) / Float64(a * 3.0)) end
code[a_, b_, c_] := N[(N[(N[(N[(N[Power[b, 2.0], $MachinePrecision] - N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision] + N[(3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] - N[(c * N[(a * N[Power[27.0, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\left({b}^{2} - {b}^{2}\right) + 3 \cdot \left(c \cdot a\right)}{\left(-b\right) - \sqrt{{b}^{2} - c \cdot \left(a \cdot \sqrt[3]{27}\right)}}}{a \cdot 3}
\end{array}
Initial program 16.5%
add-cbrt-cube16.5%
pow1/316.4%
pow316.4%
associate-*l*16.4%
unpow-prod-down16.4%
metadata-eval16.4%
Applied egg-rr16.4%
flip-+16.5%
pow216.5%
add-sqr-sqrt17.0%
pow217.0%
unpow1/317.0%
*-commutative17.0%
cbrt-prod17.1%
unpow317.0%
add-cbrt-cube17.0%
Applied egg-rr17.1%
associate--r-98.6%
+-commutative98.6%
*-commutative98.6%
associate-*l*98.6%
unpow298.6%
sqr-neg98.6%
unpow298.6%
*-commutative98.6%
associate-*l*98.6%
Simplified98.6%
Taylor expanded in c around 0 99.2%
*-commutative99.3%
Simplified99.2%
Final simplification99.2%
(FPCore (a b c) :precision binary64 (let* ((t_0 (* c (* a (cbrt 27.0))))) (/ t_0 (/ (- (- b) (sqrt (- (pow b 2.0) t_0))) (/ 0.3333333333333333 a)))))
double code(double a, double b, double c) {
double t_0 = c * (a * cbrt(27.0));
return t_0 / ((-b - sqrt((pow(b, 2.0) - t_0))) / (0.3333333333333333 / a));
}
public static double code(double a, double b, double c) {
double t_0 = c * (a * Math.cbrt(27.0));
return t_0 / ((-b - Math.sqrt((Math.pow(b, 2.0) - t_0))) / (0.3333333333333333 / a));
}
function code(a, b, c) t_0 = Float64(c * Float64(a * cbrt(27.0))) return Float64(t_0 / Float64(Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) - t_0))) / Float64(0.3333333333333333 / a))) end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * N[Power[27.0, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 / N[(N[((-b) - N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(0.3333333333333333 / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot \sqrt[3]{27}\right)\\
\frac{t_0}{\frac{\left(-b\right) - \sqrt{{b}^{2} - t_0}}{\frac{0.3333333333333333}{a}}}
\end{array}
\end{array}
Initial program 16.5%
add-cbrt-cube16.5%
pow1/316.4%
pow316.4%
associate-*l*16.4%
unpow-prod-down16.4%
metadata-eval16.4%
Applied egg-rr16.4%
flip-+16.5%
pow216.5%
add-sqr-sqrt17.0%
pow217.0%
unpow1/317.0%
*-commutative17.0%
cbrt-prod17.1%
unpow317.0%
add-cbrt-cube17.0%
Applied egg-rr17.1%
associate--r-98.6%
+-commutative98.6%
*-commutative98.6%
associate-*l*98.6%
unpow298.6%
sqr-neg98.6%
unpow298.6%
*-commutative98.6%
associate-*l*98.6%
Simplified98.6%
expm1-log1p-u83.9%
expm1-udef19.9%
Applied egg-rr19.9%
expm1-def83.9%
expm1-log1p98.5%
associate-/r*98.6%
*-rgt-identity98.6%
associate-*r/98.6%
associate-/l*98.6%
fma-udef98.6%
+-rgt-identity98.6%
*-commutative98.6%
Simplified99.0%
Final simplification99.0%
(FPCore (a b c) :precision binary64 (+ (* -0.5 (/ c b)) (* -0.375 (/ (* a (pow c 2.0)) (pow b 3.0)))))
double code(double a, double b, double c) {
return (-0.5 * (c / b)) + (-0.375 * ((a * pow(c, 2.0)) / pow(b, 3.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) * (c / b)) + ((-0.375d0) * ((a * (c ** 2.0d0)) / (b ** 3.0d0)))
end function
public static double code(double a, double b, double c) {
return (-0.5 * (c / b)) + (-0.375 * ((a * Math.pow(c, 2.0)) / Math.pow(b, 3.0)));
}
def code(a, b, c): return (-0.5 * (c / b)) + (-0.375 * ((a * math.pow(c, 2.0)) / math.pow(b, 3.0)))
function code(a, b, c) return Float64(Float64(-0.5 * Float64(c / b)) + Float64(-0.375 * Float64(Float64(a * (c ^ 2.0)) / (b ^ 3.0)))) end
function tmp = code(a, b, c) tmp = (-0.5 * (c / b)) + (-0.375 * ((a * (c ^ 2.0)) / (b ^ 3.0))); end
code[a_, b_, c_] := N[(N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision] + N[(-0.375 * N[(N[(a * N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b} + -0.375 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}
\end{array}
Initial program 16.5%
Taylor expanded in b around inf 96.3%
Final simplification96.3%
(FPCore (a b c) :precision binary64 (/ (* c -0.5) b))
double code(double a, double b, double c) {
return (c * -0.5) / 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 * (-0.5d0)) / b
end function
public static double code(double a, double b, double c) {
return (c * -0.5) / b;
}
def code(a, b, c): return (c * -0.5) / b
function code(a, b, c) return Float64(Float64(c * -0.5) / b) end
function tmp = code(a, b, c) tmp = (c * -0.5) / b; end
code[a_, b_, c_] := N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot -0.5}{b}
\end{array}
Initial program 16.5%
Taylor expanded in b around inf 91.5%
*-commutative91.5%
associate-*l/91.5%
Simplified91.5%
Final simplification91.5%
herbie shell --seed 2023301
(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)))