
(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 8 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 (* 3.0 a)) (- (- b) (sqrt (- (* b b) (* a (* c 3.0)))))) (* 3.0 a)))
double code(double a, double b, double c) {
return ((c * (3.0 * a)) / (-b - sqrt(((b * b) - (a * (c * 3.0)))))) / (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 * (3.0d0 * a)) / (-b - sqrt(((b * b) - (a * (c * 3.0d0)))))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
return ((c * (3.0 * a)) / (-b - Math.sqrt(((b * b) - (a * (c * 3.0)))))) / (3.0 * a);
}
def code(a, b, c): return ((c * (3.0 * a)) / (-b - math.sqrt(((b * b) - (a * (c * 3.0)))))) / (3.0 * a)
function code(a, b, c) return Float64(Float64(Float64(c * Float64(3.0 * a)) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(a * Float64(c * 3.0)))))) / Float64(3.0 * a)) end
function tmp = code(a, b, c) tmp = ((c * (3.0 * a)) / (-b - sqrt(((b * b) - (a * (c * 3.0)))))) / (3.0 * a); end
code[a_, b_, c_] := N[(N[(N[(c * N[(3.0 * a), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(a * N[(c * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(3 \cdot a\right)}{\left(-b\right) - \sqrt{b \cdot b - a \cdot \left(c \cdot 3\right)}}}{3 \cdot a}
\end{array}
Initial program 32.6%
add-cbrt-cube32.7%
cbrt-prod32.4%
distribute-rgt-neg-in32.4%
cbrt-prod32.3%
pow232.3%
Applied egg-rr32.3%
distribute-rgt-neg-out32.3%
unpow232.3%
add-cube-cbrt32.6%
pow132.6%
metadata-eval32.6%
pow-div32.6%
pow232.6%
pow132.6%
distribute-frac-neg32.6%
pow232.6%
flip-+32.4%
Applied egg-rr33.3%
associate--r-99.3%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.1%
associate-*r*99.1%
*-commutative99.1%
associate-*r*99.1%
Simplified99.1%
Taylor expanded in b around 0 99.2%
associate-*r*99.3%
*-commutative99.3%
*-commutative99.3%
*-commutative99.3%
Simplified99.3%
pow299.3%
Applied egg-rr99.3%
(FPCore (a b c) :precision binary64 (/ (/ (* 3.0 (* c a)) (- (- b) (sqrt (- (* b b) (* a (* c 3.0)))))) (* 3.0 a)))
double code(double a, double b, double c) {
return ((3.0 * (c * a)) / (-b - sqrt(((b * b) - (a * (c * 3.0)))))) / (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 = ((3.0d0 * (c * a)) / (-b - sqrt(((b * b) - (a * (c * 3.0d0)))))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
return ((3.0 * (c * a)) / (-b - Math.sqrt(((b * b) - (a * (c * 3.0)))))) / (3.0 * a);
}
def code(a, b, c): return ((3.0 * (c * a)) / (-b - math.sqrt(((b * b) - (a * (c * 3.0)))))) / (3.0 * a)
function code(a, b, c) return Float64(Float64(Float64(3.0 * Float64(c * a)) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(a * Float64(c * 3.0)))))) / Float64(3.0 * a)) end
function tmp = code(a, b, c) tmp = ((3.0 * (c * a)) / (-b - sqrt(((b * b) - (a * (c * 3.0)))))) / (3.0 * a); end
code[a_, b_, c_] := N[(N[(N[(3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(a * N[(c * 3.0), $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{b \cdot b - a \cdot \left(c \cdot 3\right)}}}{3 \cdot a}
\end{array}
Initial program 32.6%
add-cbrt-cube32.7%
cbrt-prod32.4%
distribute-rgt-neg-in32.4%
cbrt-prod32.3%
pow232.3%
Applied egg-rr32.3%
distribute-rgt-neg-out32.3%
unpow232.3%
add-cube-cbrt32.6%
pow132.6%
metadata-eval32.6%
pow-div32.6%
pow232.6%
pow132.6%
distribute-frac-neg32.6%
pow232.6%
flip-+32.4%
Applied egg-rr33.3%
associate--r-99.3%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.1%
associate-*r*99.1%
*-commutative99.1%
associate-*r*99.1%
Simplified99.1%
Taylor expanded in b around 0 99.2%
pow299.3%
Applied egg-rr99.2%
Final simplification99.2%
(FPCore (a b c) :precision binary64 (if (<= b 0.00105) (/ (- (sqrt (- (* b b) (* c (* 3.0 a)))) b) (* 3.0 a)) (/ (+ (* -0.375 (* a (pow (/ c b) 2.0))) (* c -0.5)) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 0.00105) {
tmp = (sqrt(((b * b) - (c * (3.0 * a)))) - b) / (3.0 * a);
} else {
tmp = ((-0.375 * (a * pow((c / b), 2.0))) + (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 <= 0.00105d0) then
tmp = (sqrt(((b * b) - (c * (3.0d0 * a)))) - b) / (3.0d0 * a)
else
tmp = (((-0.375d0) * (a * ((c / b) ** 2.0d0))) + (c * (-0.5d0))) / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 0.00105) {
tmp = (Math.sqrt(((b * b) - (c * (3.0 * a)))) - b) / (3.0 * a);
} else {
tmp = ((-0.375 * (a * Math.pow((c / b), 2.0))) + (c * -0.5)) / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 0.00105: tmp = (math.sqrt(((b * b) - (c * (3.0 * a)))) - b) / (3.0 * a) else: tmp = ((-0.375 * (a * math.pow((c / b), 2.0))) + (c * -0.5)) / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 0.00105) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(3.0 * a)))) - b) / Float64(3.0 * a)); else tmp = Float64(Float64(Float64(-0.375 * Float64(a * (Float64(c / b) ^ 2.0))) + Float64(c * -0.5)) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 0.00105) tmp = (sqrt(((b * b) - (c * (3.0 * a)))) - b) / (3.0 * a); else tmp = ((-0.375 * (a * ((c / b) ^ 2.0))) + (c * -0.5)) / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 0.00105], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision], N[(N[(N[(-0.375 * N[(a * N[Power[N[(c / b), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 0.00105:\\
\;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(3 \cdot a\right)} - b}{3 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-0.375 \cdot \left(a \cdot {\left(\frac{c}{b}\right)}^{2}\right) + c \cdot -0.5}{b}\\
\end{array}
\end{array}
if b < 0.00104999999999999994Initial program 80.7%
if 0.00104999999999999994 < b Initial program 29.0%
add-cbrt-cube29.2%
cbrt-prod29.0%
distribute-rgt-neg-in29.0%
cbrt-prod28.9%
pow228.9%
Applied egg-rr28.9%
Taylor expanded in b around inf 92.5%
+-commutative92.5%
fma-define92.5%
associate-/l*92.5%
unpow292.5%
unpow292.5%
times-frac92.5%
unpow192.5%
pow-plus92.5%
metadata-eval92.5%
*-commutative92.5%
Simplified92.5%
fma-undefine92.5%
Applied egg-rr92.5%
Final simplification91.6%
(FPCore (a b c) :precision binary64 (/ (+ (* -0.375 (* a (pow (/ c b) 2.0))) (* c -0.5)) b))
double code(double a, double b, double c) {
return ((-0.375 * (a * pow((c / b), 2.0))) + (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 = (((-0.375d0) * (a * ((c / b) ** 2.0d0))) + (c * (-0.5d0))) / b
end function
public static double code(double a, double b, double c) {
return ((-0.375 * (a * Math.pow((c / b), 2.0))) + (c * -0.5)) / b;
}
def code(a, b, c): return ((-0.375 * (a * math.pow((c / b), 2.0))) + (c * -0.5)) / b
function code(a, b, c) return Float64(Float64(Float64(-0.375 * Float64(a * (Float64(c / b) ^ 2.0))) + Float64(c * -0.5)) / b) end
function tmp = code(a, b, c) tmp = ((-0.375 * (a * ((c / b) ^ 2.0))) + (c * -0.5)) / b; end
code[a_, b_, c_] := N[(N[(N[(-0.375 * N[(a * N[Power[N[(c / b), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{-0.375 \cdot \left(a \cdot {\left(\frac{c}{b}\right)}^{2}\right) + c \cdot -0.5}{b}
\end{array}
Initial program 32.6%
add-cbrt-cube32.7%
cbrt-prod32.4%
distribute-rgt-neg-in32.4%
cbrt-prod32.3%
pow232.3%
Applied egg-rr32.3%
Taylor expanded in b around inf 90.0%
+-commutative90.0%
fma-define90.0%
associate-/l*90.0%
unpow290.0%
unpow290.0%
times-frac90.0%
unpow190.0%
pow-plus90.0%
metadata-eval90.0%
*-commutative90.0%
Simplified90.0%
fma-undefine90.0%
Applied egg-rr90.0%
(FPCore (a b c) :precision binary64 (/ (* c (- (* -0.375 (/ (* c a) (pow b 2.0))) 0.5)) b))
double code(double a, double b, double c) {
return (c * ((-0.375 * ((c * a) / pow(b, 2.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 ** 2.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, 2.0))) - 0.5)) / b;
}
def code(a, b, c): return (c * ((-0.375 * ((c * a) / math.pow(b, 2.0))) - 0.5)) / b
function code(a, b, c) return Float64(Float64(c * Float64(Float64(-0.375 * Float64(Float64(c * a) / (b ^ 2.0))) - 0.5)) / b) end
function tmp = code(a, b, c) tmp = (c * ((-0.375 * ((c * a) / (b ^ 2.0))) - 0.5)) / b; end
code[a_, b_, c_] := N[(N[(c * N[(N[(-0.375 * N[(N[(c * a), $MachinePrecision] / N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \left(-0.375 \cdot \frac{c \cdot a}{{b}^{2}} - 0.5\right)}{b}
\end{array}
Initial program 32.6%
add-cbrt-cube32.7%
cbrt-prod32.4%
distribute-rgt-neg-in32.4%
cbrt-prod32.3%
pow232.3%
Applied egg-rr32.3%
Taylor expanded in b around inf 90.0%
+-commutative90.0%
fma-define90.0%
associate-/l*90.0%
unpow290.0%
unpow290.0%
times-frac90.0%
unpow190.0%
pow-plus90.0%
metadata-eval90.0%
*-commutative90.0%
Simplified90.0%
Taylor expanded in c around 0 90.0%
Final simplification90.0%
(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 32.6%
Simplified32.8%
Taylor expanded in c around 0 89.7%
associate-*r/89.7%
metadata-eval89.7%
Simplified89.7%
Final simplification89.7%
(FPCore (a b c) :precision binary64 (* (/ c b) -0.5))
double code(double a, double b, double c) {
return (c / b) * -0.5;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (c / b) * (-0.5d0)
end function
public static double code(double a, double b, double c) {
return (c / b) * -0.5;
}
def code(a, b, c): return (c / b) * -0.5
function code(a, b, c) return Float64(Float64(c / b) * -0.5) end
function tmp = code(a, b, c) tmp = (c / b) * -0.5; end
code[a_, b_, c_] := N[(N[(c / b), $MachinePrecision] * -0.5), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{b} \cdot -0.5
\end{array}
Initial program 32.6%
Simplified32.8%
Taylor expanded in b around inf 80.2%
Final simplification80.2%
(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 32.6%
add-cbrt-cube32.7%
cbrt-prod32.4%
distribute-rgt-neg-in32.4%
cbrt-prod32.3%
pow232.3%
Applied egg-rr32.3%
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 2024170
(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)))