
(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 (/ (- (- b) (sqrt (fma b b (* c (* a -3.0))))) (* a 3.0))) (* a 3.0)))
double code(double a, double b, double c) {
return (c / ((-b - sqrt(fma(b, b, (c * (a * -3.0))))) / (a * 3.0))) / (a * 3.0);
}
function code(a, b, c) return Float64(Float64(c / Float64(Float64(Float64(-b) - sqrt(fma(b, b, Float64(c * Float64(a * -3.0))))) / Float64(a * 3.0))) / Float64(a * 3.0)) end
code[a_, b_, c_] := N[(N[(c / N[(N[((-b) - N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c}{\frac{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)}}{a \cdot 3}}}{a \cdot 3}
\end{array}
Initial program 55.9%
neg-sub055.9%
associate-+l-55.9%
sub0-neg55.9%
neg-mul-155.9%
associate-*r/55.9%
metadata-eval55.9%
metadata-eval55.9%
times-frac55.9%
*-commutative55.9%
times-frac55.9%
associate-*l/55.9%
Simplified55.9%
add-cube-cbrt55.9%
pow355.9%
associate-*r*55.9%
*-commutative55.9%
*-commutative55.9%
Applied egg-rr55.9%
flip-+55.7%
add-sqr-sqrt57.1%
unpow357.1%
add-cube-cbrt57.2%
associate-*r*57.2%
unpow357.2%
add-cube-cbrt57.2%
associate-*r*57.2%
Applied egg-rr57.2%
sqr-neg57.2%
associate--r-99.2%
+-inverses99.2%
associate-*r*99.3%
fma-neg99.3%
distribute-rgt-neg-in99.3%
metadata-eval99.3%
Simplified99.3%
div-inv99.2%
+-lft-identity99.2%
associate-*l*99.2%
*-commutative99.2%
Applied egg-rr99.2%
associate-*r/99.3%
*-rgt-identity99.3%
associate-/l*99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (let* ((t_0 (/ (- (sqrt (- (* b b) (* c (* a 3.0)))) b) (* a 3.0)))) (if (<= t_0 -4.2e-6) t_0 (/ (* c -0.5) b))))
double code(double a, double b, double c) {
double t_0 = (sqrt(((b * b) - (c * (a * 3.0)))) - b) / (a * 3.0);
double tmp;
if (t_0 <= -4.2e-6) {
tmp = t_0;
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
real(8) :: tmp
t_0 = (sqrt(((b * b) - (c * (a * 3.0d0)))) - b) / (a * 3.0d0)
if (t_0 <= (-4.2d-6)) then
tmp = t_0
else
tmp = (c * (-0.5d0)) / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = (Math.sqrt(((b * b) - (c * (a * 3.0)))) - b) / (a * 3.0);
double tmp;
if (t_0 <= -4.2e-6) {
tmp = t_0;
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
def code(a, b, c): t_0 = (math.sqrt(((b * b) - (c * (a * 3.0)))) - b) / (a * 3.0) tmp = 0 if t_0 <= -4.2e-6: tmp = t_0 else: tmp = (c * -0.5) / b return tmp
function code(a, b, c) t_0 = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 3.0)))) - b) / Float64(a * 3.0)) tmp = 0.0 if (t_0 <= -4.2e-6) tmp = t_0; else tmp = Float64(Float64(c * -0.5) / b); end return tmp end
function tmp_2 = code(a, b, c) t_0 = (sqrt(((b * b) - (c * (a * 3.0)))) - b) / (a * 3.0); tmp = 0.0; if (t_0 <= -4.2e-6) tmp = t_0; else tmp = (c * -0.5) / b; end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -4.2e-6], t$95$0, N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 3\right)} - b}{a \cdot 3}\\
\mathbf{if}\;t_0 \leq -4.2 \cdot 10^{-6}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot -0.5}{b}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 3 a) c)))) (*.f64 3 a)) < -4.1999999999999996e-6Initial program 73.6%
if -4.1999999999999996e-6 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 3 a) c)))) (*.f64 3 a)) Initial program 36.8%
/-rgt-identity36.8%
metadata-eval36.8%
associate-/l*36.8%
associate-*r/36.8%
*-commutative36.8%
associate-*l/36.8%
associate-*r/36.8%
metadata-eval36.8%
metadata-eval36.8%
times-frac36.8%
neg-mul-136.8%
distribute-rgt-neg-in36.8%
times-frac36.8%
metadata-eval36.8%
neg-mul-136.8%
Simplified37.0%
Taylor expanded in b around inf 79.8%
associate-*r/79.8%
Simplified79.8%
Final simplification76.6%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (* c (* a 3.0))))
(/
(/ (+ (- (* b b) (* b b)) t_0) (- (- b) (sqrt (- (* b b) t_0))))
(* a 3.0))))
double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
return ((((b * b) - (b * b)) + t_0) / (-b - sqrt(((b * b) - t_0)))) / (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
real(8) :: t_0
t_0 = c * (a * 3.0d0)
code = ((((b * b) - (b * b)) + t_0) / (-b - sqrt(((b * b) - t_0)))) / (a * 3.0d0)
end function
public static double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
return ((((b * b) - (b * b)) + t_0) / (-b - Math.sqrt(((b * b) - t_0)))) / (a * 3.0);
}
def code(a, b, c): t_0 = c * (a * 3.0) return ((((b * b) - (b * b)) + t_0) / (-b - math.sqrt(((b * b) - t_0)))) / (a * 3.0)
function code(a, b, c) t_0 = Float64(c * Float64(a * 3.0)) return Float64(Float64(Float64(Float64(Float64(b * b) - Float64(b * b)) + t_0) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - t_0)))) / Float64(a * 3.0)) end
function tmp = code(a, b, c) t_0 = c * (a * 3.0); tmp = ((((b * b) - (b * b)) + t_0) / (-b - sqrt(((b * b) - t_0)))) / (a * 3.0); end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[(N[(b * b), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot 3\right)\\
\frac{\frac{\left(b \cdot b - b \cdot b\right) + t_0}{\left(-b\right) - \sqrt{b \cdot b - t_0}}}{a \cdot 3}
\end{array}
\end{array}
Initial program 55.9%
neg-sub055.9%
associate-+l-55.9%
sub0-neg55.9%
neg-mul-155.9%
associate-*r/55.9%
metadata-eval55.9%
metadata-eval55.9%
times-frac55.9%
*-commutative55.9%
times-frac55.9%
associate-*l/55.9%
Simplified55.9%
add-cube-cbrt55.9%
pow355.9%
associate-*r*55.9%
*-commutative55.9%
*-commutative55.9%
Applied egg-rr55.9%
flip-+55.7%
add-sqr-sqrt57.1%
unpow357.1%
add-cube-cbrt57.2%
associate-*r*57.2%
unpow357.2%
add-cube-cbrt57.2%
associate-*r*57.2%
Applied egg-rr57.2%
sqr-neg57.2%
associate--r-99.2%
sqr-neg99.2%
sqr-neg99.2%
associate-*r*99.3%
associate-*r*99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (if (<= b 420.0) (/ (- (sqrt (- (* b b) (* 3.0 (* c a)))) b) (* a 3.0)) (/ (* c -0.5) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 420.0) {
tmp = (sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0);
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: tmp
if (b <= 420.0d0) then
tmp = (sqrt(((b * b) - (3.0d0 * (c * a)))) - b) / (a * 3.0d0)
else
tmp = (c * (-0.5d0)) / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 420.0) {
tmp = (Math.sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0);
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 420.0: tmp = (math.sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0) else: tmp = (c * -0.5) / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 420.0) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(3.0 * Float64(c * a)))) - b) / Float64(a * 3.0)); else tmp = Float64(Float64(c * -0.5) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 420.0) tmp = (sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0); else tmp = (c * -0.5) / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 420.0], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 420:\\
\;\;\;\;\frac{\sqrt{b \cdot b - 3 \cdot \left(c \cdot a\right)} - b}{a \cdot 3}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot -0.5}{b}\\
\end{array}
\end{array}
if b < 420Initial program 76.2%
neg-sub076.2%
associate-+l-76.2%
sub0-neg76.2%
neg-mul-176.2%
associate-*r/76.2%
metadata-eval76.2%
metadata-eval76.2%
times-frac76.2%
*-commutative76.2%
times-frac76.2%
associate-*l/76.2%
Simplified76.2%
if 420 < b Initial program 42.9%
/-rgt-identity42.9%
metadata-eval42.9%
associate-/l*42.9%
associate-*r/42.9%
*-commutative42.9%
associate-*l/42.9%
associate-*r/42.9%
metadata-eval42.9%
metadata-eval42.9%
times-frac42.9%
neg-mul-142.9%
distribute-rgt-neg-in42.9%
times-frac42.9%
metadata-eval42.9%
neg-mul-142.9%
Simplified43.2%
Taylor expanded in b around inf 75.1%
associate-*r/75.1%
Simplified75.1%
Final simplification75.5%
(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 55.9%
/-rgt-identity55.9%
metadata-eval55.9%
associate-/l*55.9%
associate-*r/55.9%
*-commutative55.9%
associate-*l/55.9%
associate-*r/55.9%
metadata-eval55.9%
metadata-eval55.9%
times-frac55.9%
neg-mul-155.9%
distribute-rgt-neg-in55.9%
times-frac55.9%
metadata-eval55.9%
neg-mul-155.9%
Simplified56.1%
Taylor expanded in b around inf 64.1%
associate-*r/64.1%
Simplified64.1%
Final simplification64.1%
herbie shell --seed 2023171
(FPCore (a b c)
:name "Cubic critical, narrow range"
:precision binary64
:pre (and (and (and (< 1.0536712127723509e-8 a) (< a 94906265.62425156)) (and (< 1.0536712127723509e-8 b) (< b 94906265.62425156))) (and (< 1.0536712127723509e-8 c) (< c 94906265.62425156)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))