
(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 7 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 (- (pow b 2.0) (* c (* a 3.0)))))))
double code(double a, double b, double c) {
return c / (-b - sqrt((pow(b, 2.0) - (c * (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 / (-b - sqrt(((b ** 2.0d0) - (c * (a * 3.0d0)))))
end function
public static double code(double a, double b, double c) {
return c / (-b - Math.sqrt((Math.pow(b, 2.0) - (c * (a * 3.0)))));
}
def code(a, b, c): return c / (-b - math.sqrt((math.pow(b, 2.0) - (c * (a * 3.0)))))
function code(a, b, c) return Float64(c / Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) - Float64(c * Float64(a * 3.0)))))) end
function tmp = code(a, b, c) tmp = c / (-b - sqrt(((b ^ 2.0) - (c * (a * 3.0))))); end
code[a_, b_, c_] := N[(c / N[((-b) - N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] - N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(-b\right) - \sqrt{{b}^{2} - c \cdot \left(a \cdot 3\right)}}
\end{array}
Initial program 38.4%
add-cbrt-cube38.4%
pow338.4%
Applied egg-rr38.4%
flip-+38.6%
pow238.6%
add-sqr-sqrt39.6%
pow239.6%
*-commutative39.6%
*-commutative39.6%
pow239.6%
*-commutative39.6%
*-commutative39.6%
Applied egg-rr39.6%
associate--r-98.9%
Simplified98.9%
rem-cbrt-cube99.4%
div-inv99.3%
+-commutative99.3%
*-commutative99.3%
fma-define99.3%
*-commutative99.3%
neg-mul-199.3%
unpow-prod-down99.3%
metadata-eval99.3%
*-un-lft-identity99.3%
*-commutative99.3%
Applied egg-rr99.3%
associate-*l/99.4%
+-inverses99.4%
Simplified99.4%
Taylor expanded in c around 0 99.7%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (/ (- (sqrt (- (* b b) (* c (* a 3.0)))) b) (* a 3.0))))
(if (<= t_0 -0.0035)
t_0
(/ 1.0 (/ (+ (* b -2.0) (* 1.5 (/ (* c a) b))) c)))))
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 <= -0.0035) {
tmp = t_0;
} else {
tmp = 1.0 / (((b * -2.0) + (1.5 * ((c * a) / b))) / c);
}
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 <= (-0.0035d0)) then
tmp = t_0
else
tmp = 1.0d0 / (((b * (-2.0d0)) + (1.5d0 * ((c * a) / b))) / c)
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 <= -0.0035) {
tmp = t_0;
} else {
tmp = 1.0 / (((b * -2.0) + (1.5 * ((c * a) / b))) / c);
}
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 <= -0.0035: tmp = t_0 else: tmp = 1.0 / (((b * -2.0) + (1.5 * ((c * a) / b))) / c) 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 <= -0.0035) tmp = t_0; else tmp = Float64(1.0 / Float64(Float64(Float64(b * -2.0) + Float64(1.5 * Float64(Float64(c * a) / b))) / c)); 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 <= -0.0035) tmp = t_0; else tmp = 1.0 / (((b * -2.0) + (1.5 * ((c * a) / b))) / c); 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, -0.0035], t$95$0, N[(1.0 / N[(N[(N[(b * -2.0), $MachinePrecision] + N[(1.5 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $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 -0.0035:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{b \cdot -2 + 1.5 \cdot \frac{c \cdot a}{b}}{c}}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 3 binary64) a) c)))) (*.f64 #s(literal 3 binary64) a)) < -0.00350000000000000007Initial program 74.6%
if -0.00350000000000000007 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 3 binary64) a) c)))) (*.f64 #s(literal 3 binary64) a)) Initial program 28.0%
add-cbrt-cube28.0%
pow328.0%
Applied egg-rr28.0%
flip-+28.2%
pow228.2%
add-sqr-sqrt29.0%
pow229.0%
*-commutative29.0%
*-commutative29.0%
pow229.0%
*-commutative29.0%
*-commutative29.0%
Applied egg-rr29.0%
associate--r-98.9%
Simplified98.9%
rem-cbrt-cube99.4%
clear-num99.3%
inv-pow99.3%
Applied egg-rr99.3%
unpow-199.3%
associate-/r/99.3%
fma-undefine99.3%
+-inverses99.3%
+-rgt-identity99.3%
Simplified99.3%
Taylor expanded in c around 0 92.4%
Final simplification88.4%
(FPCore (a b c) :precision binary64 (/ 1.0 (+ (* -2.0 (/ b c)) (* a (+ (* 1.125 (/ (* c a) (pow b 3.0))) (* 1.5 (/ 1.0 b)))))))
double code(double a, double b, double c) {
return 1.0 / ((-2.0 * (b / c)) + (a * ((1.125 * ((c * a) / pow(b, 3.0))) + (1.5 * (1.0 / b)))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 1.0d0 / (((-2.0d0) * (b / c)) + (a * ((1.125d0 * ((c * a) / (b ** 3.0d0))) + (1.5d0 * (1.0d0 / b)))))
end function
public static double code(double a, double b, double c) {
return 1.0 / ((-2.0 * (b / c)) + (a * ((1.125 * ((c * a) / Math.pow(b, 3.0))) + (1.5 * (1.0 / b)))));
}
def code(a, b, c): return 1.0 / ((-2.0 * (b / c)) + (a * ((1.125 * ((c * a) / math.pow(b, 3.0))) + (1.5 * (1.0 / b)))))
function code(a, b, c) return Float64(1.0 / Float64(Float64(-2.0 * Float64(b / c)) + Float64(a * Float64(Float64(1.125 * Float64(Float64(c * a) / (b ^ 3.0))) + Float64(1.5 * Float64(1.0 / b)))))) end
function tmp = code(a, b, c) tmp = 1.0 / ((-2.0 * (b / c)) + (a * ((1.125 * ((c * a) / (b ^ 3.0))) + (1.5 * (1.0 / b))))); end
code[a_, b_, c_] := N[(1.0 / N[(N[(-2.0 * N[(b / c), $MachinePrecision]), $MachinePrecision] + N[(a * N[(N[(1.125 * N[(N[(c * a), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.5 * N[(1.0 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{-2 \cdot \frac{b}{c} + a \cdot \left(1.125 \cdot \frac{c \cdot a}{{b}^{3}} + 1.5 \cdot \frac{1}{b}\right)}
\end{array}
Initial program 38.4%
add-cbrt-cube38.4%
pow338.4%
Applied egg-rr38.4%
flip-+38.6%
pow238.6%
add-sqr-sqrt39.6%
pow239.6%
*-commutative39.6%
*-commutative39.6%
pow239.6%
*-commutative39.6%
*-commutative39.6%
Applied egg-rr39.6%
associate--r-98.9%
Simplified98.9%
rem-cbrt-cube99.4%
clear-num99.3%
inv-pow99.3%
Applied egg-rr99.3%
unpow-199.3%
associate-/r/99.2%
fma-undefine99.2%
+-inverses99.2%
+-rgt-identity99.2%
Simplified99.2%
Taylor expanded in a around 0 91.2%
Final simplification91.2%
(FPCore (a b c) :precision binary64 (if (<= b 3.7e-6) (/ (- (sqrt (- (* b b) (* 3.0 (* c a)))) b) (* a 3.0)) (/ 1.0 (+ (* -2.0 (/ b c)) (* 1.5 (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.7e-6) {
tmp = (sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0);
} else {
tmp = 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / 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 <= 3.7d-6) then
tmp = (sqrt(((b * b) - (3.0d0 * (c * a)))) - b) / (a * 3.0d0)
else
tmp = 1.0d0 / (((-2.0d0) * (b / c)) + (1.5d0 * (a / b)))
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 3.7e-6) {
tmp = (Math.sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0);
} else {
tmp = 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b)));
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 3.7e-6: tmp = (math.sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0) else: tmp = 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b))) return tmp
function code(a, b, c) tmp = 0.0 if (b <= 3.7e-6) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(3.0 * Float64(c * a)))) - b) / Float64(a * 3.0)); else tmp = Float64(1.0 / Float64(Float64(-2.0 * Float64(b / c)) + Float64(1.5 * Float64(a / b)))); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 3.7e-6) tmp = (sqrt(((b * b) - (3.0 * (c * a)))) - b) / (a * 3.0); else tmp = 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b))); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 3.7e-6], 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[(1.0 / N[(N[(-2.0 * N[(b / c), $MachinePrecision]), $MachinePrecision] + N[(1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.7 \cdot 10^{-6}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - 3 \cdot \left(c \cdot a\right)} - b}{a \cdot 3}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{-2 \cdot \frac{b}{c} + 1.5 \cdot \frac{a}{b}}\\
\end{array}
\end{array}
if b < 3.7000000000000002e-6Initial program 80.5%
associate-*l*80.6%
Simplified80.6%
if 3.7000000000000002e-6 < b Initial program 35.0%
add-cbrt-cube35.0%
pow335.0%
Applied egg-rr35.0%
flip-+35.2%
pow235.2%
add-sqr-sqrt36.2%
pow236.2%
*-commutative36.2%
*-commutative36.2%
pow236.2%
*-commutative36.2%
*-commutative36.2%
Applied egg-rr36.2%
associate--r-98.9%
Simplified98.9%
rem-cbrt-cube99.4%
clear-num99.2%
inv-pow99.2%
Applied egg-rr99.2%
unpow-199.2%
associate-/r/99.2%
fma-undefine99.2%
+-inverses99.2%
+-rgt-identity99.2%
Simplified99.2%
Taylor expanded in a around 0 89.0%
Final simplification88.4%
(FPCore (a b c) :precision binary64 (/ 1.0 (+ (* -2.0 (/ b c)) (* 1.5 (/ a b)))))
double code(double a, double b, double c) {
return 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b)));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = 1.0d0 / (((-2.0d0) * (b / c)) + (1.5d0 * (a / b)))
end function
public static double code(double a, double b, double c) {
return 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b)));
}
def code(a, b, c): return 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b)))
function code(a, b, c) return Float64(1.0 / Float64(Float64(-2.0 * Float64(b / c)) + Float64(1.5 * Float64(a / b)))) end
function tmp = code(a, b, c) tmp = 1.0 / ((-2.0 * (b / c)) + (1.5 * (a / b))); end
code[a_, b_, c_] := N[(1.0 / N[(N[(-2.0 * N[(b / c), $MachinePrecision]), $MachinePrecision] + N[(1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{-2 \cdot \frac{b}{c} + 1.5 \cdot \frac{a}{b}}
\end{array}
Initial program 38.4%
add-cbrt-cube38.4%
pow338.4%
Applied egg-rr38.4%
flip-+38.6%
pow238.6%
add-sqr-sqrt39.6%
pow239.6%
*-commutative39.6%
*-commutative39.6%
pow239.6%
*-commutative39.6%
*-commutative39.6%
Applied egg-rr39.6%
associate--r-98.9%
Simplified98.9%
rem-cbrt-cube99.4%
clear-num99.3%
inv-pow99.3%
Applied egg-rr99.3%
unpow-199.3%
associate-/r/99.2%
fma-undefine99.2%
+-inverses99.2%
+-rgt-identity99.2%
Simplified99.2%
Taylor expanded in a around 0 86.8%
(FPCore (a b c) :precision binary64 (* -0.5 (/ c b)))
double code(double a, double b, double c) {
return -0.5 * (c / 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.5d0) * (c / b)
end function
public static double code(double a, double b, double c) {
return -0.5 * (c / b);
}
def code(a, b, c): return -0.5 * (c / b)
function code(a, b, c) return Float64(-0.5 * Float64(c / b)) end
function tmp = code(a, b, c) tmp = -0.5 * (c / b); end
code[a_, b_, c_] := N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b}
\end{array}
Initial program 38.4%
Simplified38.4%
Taylor expanded in b around inf 75.8%
(FPCore (a b c) :precision binary64 0.0)
double code(double a, double b, double c) {
return 0.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.0d0
end function
public static double code(double a, double b, double c) {
return 0.0;
}
def code(a, b, c): return 0.0
function code(a, b, c) return 0.0 end
function tmp = code(a, b, c) tmp = 0.0; end
code[a_, b_, c_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 38.4%
Simplified38.4%
Taylor expanded in c around inf 38.4%
*-un-lft-identity38.4%
add-sqr-sqrt38.2%
prod-diff39.1%
add-sqr-sqrt39.5%
fmm-def39.5%
*-un-lft-identity39.5%
*-commutative39.5%
fma-define39.5%
add-sqr-sqrt39.1%
Applied egg-rr39.1%
Taylor expanded in c around 0 3.2%
associate-*r/3.2%
distribute-rgt1-in3.2%
metadata-eval3.2%
mul0-lft3.2%
metadata-eval3.2%
Simplified3.2%
Taylor expanded in a around 0 3.2%
herbie shell --seed 2024169
(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)))