
(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 (/ (/ (* 3.0 (* c a)) (- (- b) (sqrt (fma b b (* -3.0 (* c a)))))) (* 3.0 a)))
double code(double a, double b, double c) {
return ((3.0 * (c * a)) / (-b - sqrt(fma(b, b, (-3.0 * (c * a)))))) / (3.0 * a);
}
function code(a, b, c) return Float64(Float64(Float64(3.0 * Float64(c * a)) / Float64(Float64(-b) - sqrt(fma(b, b, Float64(-3.0 * Float64(c * a)))))) / Float64(3.0 * a)) end
code[a_, b_, c_] := N[(N[(N[(3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(b * b + N[(-3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{3 \cdot \left(c \cdot a\right)}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, -3 \cdot \left(c \cdot a\right)\right)}}}{3 \cdot a}
\end{array}
Initial program 31.3%
neg-sub031.3%
sqr-neg31.3%
associate-+l-31.3%
sub0-neg31.3%
sub-neg31.3%
distribute-neg-in31.3%
remove-double-neg31.3%
sqr-neg31.3%
associate-*l*31.3%
Simplified31.3%
add-sqr-sqrt31.3%
sqrt-unprod31.3%
*-commutative31.3%
*-commutative31.3%
swap-sqr31.3%
metadata-eval31.3%
metadata-eval31.3%
swap-sqr31.3%
associate-*r*31.3%
associate-*r*31.3%
sqrt-unprod0.0%
add-sqr-sqrt2.5%
associate-*r*2.5%
metadata-eval2.5%
distribute-rgt-neg-in2.5%
*-commutative2.5%
neg-sub02.5%
add-sqr-sqrt2.5%
sqrt-unprod2.5%
*-commutative2.5%
*-commutative2.5%
swap-sqr2.5%
metadata-eval2.5%
metadata-eval2.5%
swap-sqr2.5%
Applied egg-rr31.3%
neg-sub031.3%
distribute-rgt-neg-in31.3%
distribute-rgt-neg-in31.3%
metadata-eval31.3%
Simplified31.3%
Applied egg-rr32.2%
Taylor expanded in b around 0 99.2%
*-commutative99.2%
Simplified99.2%
Final simplification99.2%
(FPCore (a b c) :precision binary64 (/ (fma -0.5 c (* -0.375 (* a (pow (/ c b) 2.0)))) b))
double code(double a, double b, double c) {
return fma(-0.5, c, (-0.375 * (a * pow((c / b), 2.0)))) / b;
}
function code(a, b, c) return Float64(fma(-0.5, c, Float64(-0.375 * Float64(a * (Float64(c / b) ^ 2.0)))) / b) end
code[a_, b_, c_] := N[(N[(-0.5 * c + N[(-0.375 * N[(a * N[Power[N[(c / b), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-0.5, c, -0.375 \cdot \left(a \cdot {\left(\frac{c}{b}\right)}^{2}\right)\right)}{b}
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
Simplified31.3%
Taylor expanded in a around 0 93.2%
Taylor expanded in a around 0 90.1%
Taylor expanded in b around inf 90.5%
fma-define90.5%
associate-/l*90.5%
unpow290.5%
unpow290.5%
times-frac90.5%
unpow190.5%
pow-plus90.5%
metadata-eval90.5%
Simplified90.5%
(FPCore (a b c) :precision binary64 (* c (- (* -0.375 (* a (/ c (pow b 3.0)))) (/ 0.5 b))))
double code(double a, double b, double c) {
return c * ((-0.375 * (a * (c / 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) * (a * (c / (b ** 3.0d0)))) - (0.5d0 / b))
end function
public static double code(double a, double b, double c) {
return c * ((-0.375 * (a * (c / Math.pow(b, 3.0)))) - (0.5 / b));
}
def code(a, b, c): return c * ((-0.375 * (a * (c / math.pow(b, 3.0)))) - (0.5 / b))
function code(a, b, c) return Float64(c * Float64(Float64(-0.375 * Float64(a * Float64(c / (b ^ 3.0)))) - Float64(0.5 / b))) end
function tmp = code(a, b, c) tmp = c * ((-0.375 * (a * (c / (b ^ 3.0)))) - (0.5 / b)); end
code[a_, b_, c_] := N[(c * N[(N[(-0.375 * N[(a * N[(c / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \left(-0.375 \cdot \left(a \cdot \frac{c}{{b}^{3}}\right) - \frac{0.5}{b}\right)
\end{array}
Initial program 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
Simplified31.3%
Taylor expanded in c around 0 90.3%
associate-/l*90.3%
associate-*r/90.3%
metadata-eval90.3%
Simplified90.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 31.3%
/-rgt-identity31.3%
metadata-eval31.3%
Simplified31.3%
Taylor expanded in b around inf 81.1%
associate-*r/81.1%
*-commutative81.1%
Simplified81.1%
(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.3%
/-rgt-identity31.3%
metadata-eval31.3%
Simplified31.3%
Taylor expanded in b around inf 81.1%
associate-*r/81.1%
*-commutative81.1%
Simplified81.1%
Taylor expanded in c around 0 81.1%
associate-*r/81.1%
*-commutative81.1%
associate-*r/80.9%
Simplified80.9%
(FPCore (a b c) :precision binary64 (/ 0.0 a))
double code(double a, double b, double c) {
return 0.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 = 0.0d0 / a
end function
public static double code(double a, double b, double c) {
return 0.0 / a;
}
def code(a, b, c): return 0.0 / a
function code(a, b, c) return Float64(0.0 / a) end
function tmp = code(a, b, c) tmp = 0.0 / a; end
code[a_, b_, c_] := N[(0.0 / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{0}{a}
\end{array}
Initial program 31.3%
neg-sub031.3%
sqr-neg31.3%
associate-+l-31.3%
sub0-neg31.3%
sub-neg31.3%
distribute-neg-in31.3%
remove-double-neg31.3%
sqr-neg31.3%
associate-*l*31.3%
Simplified31.3%
add-sqr-sqrt31.3%
sqrt-unprod31.3%
*-commutative31.3%
*-commutative31.3%
swap-sqr31.3%
metadata-eval31.3%
metadata-eval31.3%
swap-sqr31.3%
associate-*r*31.3%
associate-*r*31.3%
sqrt-unprod0.0%
add-sqr-sqrt2.5%
associate-*r*2.5%
metadata-eval2.5%
distribute-rgt-neg-in2.5%
*-commutative2.5%
neg-sub02.5%
add-sqr-sqrt2.5%
sqrt-unprod2.5%
*-commutative2.5%
*-commutative2.5%
swap-sqr2.5%
metadata-eval2.5%
metadata-eval2.5%
swap-sqr2.5%
Applied egg-rr31.3%
neg-sub031.3%
distribute-rgt-neg-in31.3%
distribute-rgt-neg-in31.3%
metadata-eval31.3%
Simplified31.3%
associate-*r*31.3%
pow231.3%
metadata-eval31.3%
sqrt-pow131.3%
*-commutative31.3%
log1p-expm1-u23.7%
Applied egg-rr23.8%
Taylor expanded in a around 0 3.2%
associate-*r/3.2%
distribute-rgt1-in3.2%
metadata-eval3.2%
mul0-lft3.2%
metadata-eval3.2%
Simplified3.2%
herbie shell --seed 2024137
(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)))