
(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 6 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 (- (- b) (sqrt (fma (* c -3.0) a (pow b 2.0))))))
double code(double a, double b, double c) {
return c / (-b - sqrt(fma((c * -3.0), a, pow(b, 2.0))));
}
function code(a, b, c) return Float64(c / Float64(Float64(-b) - sqrt(fma(Float64(c * -3.0), a, (b ^ 2.0))))) end
code[a_, b_, c_] := N[(c / N[((-b) - N[Sqrt[N[(N[(c * -3.0), $MachinePrecision] * a + N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(c \cdot -3, a, {b}^{2}\right)}}
\end{array}
Initial program 31.7%
sqr-neg31.7%
sqr-neg31.7%
associate-*l*31.7%
Simplified31.7%
add-exp-log31.6%
Applied egg-rr31.6%
flip-+31.7%
pow231.7%
add-sqr-sqrt32.6%
pow232.6%
rem-exp-log32.7%
associate-*r*32.7%
*-commutative32.7%
pow232.7%
rem-exp-log32.7%
associate-*r*32.7%
*-commutative32.7%
Applied egg-rr32.7%
associate--r-99.4%
associate-*l*99.1%
associate-*l*99.1%
Simplified99.1%
div-inv99.1%
+-commutative99.1%
fma-define99.1%
neg-mul-199.1%
unpow-prod-down99.1%
metadata-eval99.1%
*-un-lft-identity99.1%
associate-*r*99.1%
*-commutative99.1%
Applied egg-rr99.1%
*-commutative99.1%
times-frac99.1%
associate-*r/99.1%
*-lft-identity99.1%
associate-/r*99.3%
fma-undefine99.3%
associate-*l*99.6%
+-inverses99.6%
+-rgt-identity99.6%
*-commutative99.6%
*-commutative99.6%
associate-*r*99.6%
cancel-sign-sub-inv99.6%
Simplified99.6%
div-inv99.4%
associate-/l*99.5%
pow199.5%
pow199.5%
pow-div99.5%
metadata-eval99.5%
metadata-eval99.5%
add-log-exp44.8%
exp-prod44.8%
pow144.8%
add-log-exp99.5%
Applied egg-rr99.5%
associate-*r/99.8%
*-rgt-identity99.8%
fma-define99.8%
associate-*r*99.8%
fma-define99.8%
*-commutative99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 3.0)) (* a 3.0)) (- (- b) (sqrt (+ (pow b 2.0) (* -3.0 (* c a)))))))
double code(double a, double b, double c) {
return ((c * (a * 3.0)) / (a * 3.0)) / (-b - sqrt((pow(b, 2.0) + (-3.0 * (c * 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 * 3.0d0)) / (a * 3.0d0)) / (-b - sqrt(((b ** 2.0d0) + ((-3.0d0) * (c * a)))))
end function
public static double code(double a, double b, double c) {
return ((c * (a * 3.0)) / (a * 3.0)) / (-b - Math.sqrt((Math.pow(b, 2.0) + (-3.0 * (c * a)))));
}
def code(a, b, c): return ((c * (a * 3.0)) / (a * 3.0)) / (-b - math.sqrt((math.pow(b, 2.0) + (-3.0 * (c * a)))))
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 3.0)) / Float64(a * 3.0)) / Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) + Float64(-3.0 * Float64(c * a)))))) end
function tmp = code(a, b, c) tmp = ((c * (a * 3.0)) / (a * 3.0)) / (-b - sqrt(((b ^ 2.0) + (-3.0 * (c * a))))); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $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]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 3\right)}{a \cdot 3}}{\left(-b\right) - \sqrt{{b}^{2} + -3 \cdot \left(c \cdot a\right)}}
\end{array}
Initial program 31.7%
sqr-neg31.7%
sqr-neg31.7%
associate-*l*31.7%
Simplified31.7%
add-exp-log31.6%
Applied egg-rr31.6%
flip-+31.7%
pow231.7%
add-sqr-sqrt32.6%
pow232.6%
rem-exp-log32.7%
associate-*r*32.7%
*-commutative32.7%
pow232.7%
rem-exp-log32.7%
associate-*r*32.7%
*-commutative32.7%
Applied egg-rr32.7%
associate--r-99.4%
associate-*l*99.1%
associate-*l*99.1%
Simplified99.1%
div-inv99.1%
+-commutative99.1%
fma-define99.1%
neg-mul-199.1%
unpow-prod-down99.1%
metadata-eval99.1%
*-un-lft-identity99.1%
associate-*r*99.1%
*-commutative99.1%
Applied egg-rr99.1%
*-commutative99.1%
times-frac99.1%
associate-*r/99.1%
*-lft-identity99.1%
associate-/r*99.3%
fma-undefine99.3%
associate-*l*99.6%
+-inverses99.6%
+-rgt-identity99.6%
*-commutative99.6%
*-commutative99.6%
associate-*r*99.6%
cancel-sign-sub-inv99.6%
Simplified99.6%
fma-undefine99.6%
Applied egg-rr99.6%
Final simplification99.6%
(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 31.7%
sqr-neg31.7%
sqr-neg31.7%
associate-*l*31.7%
Simplified31.7%
Taylor expanded in a around 0 90.9%
Final simplification90.9%
(FPCore (a b c) :precision binary64 (* c (- (* -0.375 (/ (* c a) (pow b 3.0))) (/ 0.5 b))))
double code(double a, double b, double c) {
return c * ((-0.375 * ((c * a) / pow(b, 3.0))) - (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.375d0) * ((c * a) / (b ** 3.0d0))) - (0.5d0 / b))
end function
public static double code(double a, double b, double c) {
return c * ((-0.375 * ((c * a) / Math.pow(b, 3.0))) - (0.5 / b));
}
def code(a, b, c): return c * ((-0.375 * ((c * a) / math.pow(b, 3.0))) - (0.5 / b))
function code(a, b, c) return Float64(c * Float64(Float64(-0.375 * Float64(Float64(c * a) / (b ^ 3.0))) - Float64(0.5 / b))) end
function tmp = code(a, b, c) tmp = c * ((-0.375 * ((c * a) / (b ^ 3.0))) - (0.5 / b)); end
code[a_, b_, c_] := N[(c * N[(N[(-0.375 * N[(N[(c * a), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \left(-0.375 \cdot \frac{c \cdot a}{{b}^{3}} - \frac{0.5}{b}\right)
\end{array}
Initial program 31.7%
sqr-neg31.7%
sqr-neg31.7%
associate-*l*31.7%
Simplified31.7%
Taylor expanded in c around 0 90.6%
add-cbrt-cube90.1%
pow390.2%
associate-*r/90.2%
un-div-inv90.2%
Applied egg-rr90.2%
rem-cbrt-cube90.6%
associate-/l*90.6%
Applied egg-rr90.6%
Final simplification90.6%
(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(c * Float64(-0.5 / b)) end
function tmp = code(a, b, c) tmp = c * (-0.5 / b); end
code[a_, b_, c_] := N[(c * N[(-0.5 / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \frac{-0.5}{b}
\end{array}
Initial program 31.7%
sqr-neg31.7%
sqr-neg31.7%
associate-*l*31.7%
Simplified31.7%
Taylor expanded in c around 0 90.6%
Taylor expanded in a around 0 80.8%
Final simplification80.8%
(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 31.7%
sqr-neg31.7%
sqr-neg31.7%
associate-*l*31.7%
Simplified31.7%
Taylor expanded in b around inf 81.0%
associate-*r/81.0%
*-commutative81.0%
Simplified81.0%
Final simplification81.0%
herbie shell --seed 2024080
(FPCore (a b c)
:name "Cubic critical, 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) (* (* 3.0 a) c)))) (* 3.0 a)))