
(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 (/ (/ (* a c) a) (- (- b) (sqrt (fma b b (* (* a c) -3.0))))))
double code(double a, double b, double c) {
return ((a * c) / a) / (-b - sqrt(fma(b, b, ((a * c) * -3.0))));
}
function code(a, b, c) return Float64(Float64(Float64(a * c) / a) / Float64(Float64(-b) - sqrt(fma(b, b, Float64(Float64(a * c) * -3.0))))) end
code[a_, b_, c_] := N[(N[(N[(a * c), $MachinePrecision] / a), $MachinePrecision] / N[((-b) - N[Sqrt[N[(b * b + N[(N[(a * c), $MachinePrecision] * -3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{a \cdot c}{a}}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -3\right)}}
\end{array}
Initial program 53.6%
neg-sub053.6%
flip--53.4%
metadata-eval53.4%
pow253.4%
add-sqr-sqrt52.4%
sqrt-prod53.4%
sqr-neg53.4%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod53.4%
sqr-neg53.4%
sqrt-prod52.4%
add-sqr-sqrt53.4%
Applied egg-rr53.4%
neg-sub053.4%
Simplified53.4%
flip-+53.4%
Applied egg-rr54.9%
associate--r-99.4%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
Simplified99.2%
+-commutative99.2%
*-commutative99.2%
associate-*l*99.4%
*-commutative99.4%
fma-define99.4%
*-commutative99.4%
neg-mul-199.4%
unpow-prod-down99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
Applied egg-rr99.4%
fma-undefine99.4%
+-inverses99.4%
+-rgt-identity99.4%
*-commutative99.4%
Simplified99.4%
*-un-lft-identity99.4%
associate-/l/99.3%
*-commutative99.3%
*-commutative99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.5%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.2%
times-frac99.5%
metadata-eval99.5%
*-commutative99.5%
cancel-sign-sub-inv99.5%
unpow299.5%
fma-define99.4%
metadata-eval99.4%
*-commutative99.4%
Simplified99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 3.0)) (+ b (sqrt (- (* b b) (* (* a c) 3.0))))) (* a (- 3.0))))
double code(double a, double b, double c) {
return ((c * (a * 3.0)) / (b + sqrt(((b * b) - ((a * c) * 3.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
code = ((c * (a * 3.0d0)) / (b + sqrt(((b * b) - ((a * c) * 3.0d0))))) / (a * -3.0d0)
end function
public static double code(double a, double b, double c) {
return ((c * (a * 3.0)) / (b + Math.sqrt(((b * b) - ((a * c) * 3.0))))) / (a * -3.0);
}
def code(a, b, c): return ((c * (a * 3.0)) / (b + math.sqrt(((b * b) - ((a * c) * 3.0))))) / (a * -3.0)
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 3.0)) / Float64(b + sqrt(Float64(Float64(b * b) - Float64(Float64(a * c) * 3.0))))) / Float64(a * Float64(-3.0))) end
function tmp = code(a, b, c) tmp = ((c * (a * 3.0)) / (b + sqrt(((b * b) - ((a * c) * 3.0))))) / (a * -3.0); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision] / N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(a * c), $MachinePrecision] * 3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * (-3.0)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 3\right)}{b + \sqrt{b \cdot b - \left(a \cdot c\right) \cdot 3}}}{a \cdot \left(-3\right)}
\end{array}
Initial program 53.6%
neg-sub053.6%
flip--53.4%
metadata-eval53.4%
pow253.4%
add-sqr-sqrt52.4%
sqrt-prod53.4%
sqr-neg53.4%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod53.4%
sqr-neg53.4%
sqrt-prod52.4%
add-sqr-sqrt53.4%
Applied egg-rr53.4%
neg-sub053.4%
Simplified53.4%
flip-+53.4%
Applied egg-rr54.9%
associate--r-99.4%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
Simplified99.2%
+-commutative99.2%
*-commutative99.2%
associate-*l*99.4%
*-commutative99.4%
fma-define99.4%
*-commutative99.4%
neg-mul-199.4%
unpow-prod-down99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
Applied egg-rr99.4%
fma-undefine99.4%
+-inverses99.4%
+-rgt-identity99.4%
*-commutative99.4%
Simplified99.4%
pow299.4%
Applied egg-rr99.4%
Final simplification99.4%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (* c (* a 3.0))))
(if (<= b 140.0)
(/ (- (sqrt (- (* b b) t_0)) b) (* a 3.0))
(/ (/ t_0 (- (* 1.5 (/ (* a c) b)) (* b 2.0))) (* a 3.0)))))
double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
double tmp;
if (b <= 140.0) {
tmp = (sqrt(((b * b) - t_0)) - b) / (a * 3.0);
} else {
tmp = (t_0 / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0);
}
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 = c * (a * 3.0d0)
if (b <= 140.0d0) then
tmp = (sqrt(((b * b) - t_0)) - b) / (a * 3.0d0)
else
tmp = (t_0 / ((1.5d0 * ((a * c) / b)) - (b * 2.0d0))) / (a * 3.0d0)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
double tmp;
if (b <= 140.0) {
tmp = (Math.sqrt(((b * b) - t_0)) - b) / (a * 3.0);
} else {
tmp = (t_0 / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0);
}
return tmp;
}
def code(a, b, c): t_0 = c * (a * 3.0) tmp = 0 if b <= 140.0: tmp = (math.sqrt(((b * b) - t_0)) - b) / (a * 3.0) else: tmp = (t_0 / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0) return tmp
function code(a, b, c) t_0 = Float64(c * Float64(a * 3.0)) tmp = 0.0 if (b <= 140.0) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - t_0)) - b) / Float64(a * 3.0)); else tmp = Float64(Float64(t_0 / Float64(Float64(1.5 * Float64(Float64(a * c) / b)) - Float64(b * 2.0))) / Float64(a * 3.0)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = c * (a * 3.0); tmp = 0.0; if (b <= 140.0) tmp = (sqrt(((b * b) - t_0)) - b) / (a * 3.0); else tmp = (t_0 / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, 140.0], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(t$95$0 / N[(N[(1.5 * N[(N[(a * c), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] - N[(b * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot 3\right)\\
\mathbf{if}\;b \leq 140:\\
\;\;\;\;\frac{\sqrt{b \cdot b - t\_0} - b}{a \cdot 3}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{t\_0}{1.5 \cdot \frac{a \cdot c}{b} - b \cdot 2}}{a \cdot 3}\\
\end{array}
\end{array}
if b < 140Initial program 78.8%
if 140 < b Initial program 43.9%
neg-sub043.9%
flip--43.8%
metadata-eval43.8%
pow243.8%
add-sqr-sqrt43.0%
sqrt-prod43.8%
sqr-neg43.8%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod43.8%
sqr-neg43.8%
sqrt-prod43.0%
add-sqr-sqrt43.8%
Applied egg-rr43.8%
neg-sub043.8%
Simplified43.8%
flip-+43.9%
Applied egg-rr45.3%
associate--r-99.4%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
Simplified99.2%
+-commutative99.2%
*-commutative99.2%
associate-*l*99.4%
*-commutative99.4%
fma-define99.4%
*-commutative99.4%
neg-mul-199.4%
unpow-prod-down99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
Applied egg-rr99.4%
fma-undefine99.4%
+-inverses99.4%
+-rgt-identity99.4%
*-commutative99.4%
Simplified99.4%
Taylor expanded in a around 0 89.4%
Final simplification86.5%
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a 3.0)) (- (* 1.5 (/ (* a c) b)) (* b 2.0))) (* a 3.0)))
double code(double a, double b, double c) {
return ((c * (a * 3.0)) / ((1.5 * ((a * c) / b)) - (b * 2.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
code = ((c * (a * 3.0d0)) / ((1.5d0 * ((a * c) / b)) - (b * 2.0d0))) / (a * 3.0d0)
end function
public static double code(double a, double b, double c) {
return ((c * (a * 3.0)) / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0);
}
def code(a, b, c): return ((c * (a * 3.0)) / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0)
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 3.0)) / Float64(Float64(1.5 * Float64(Float64(a * c) / b)) - Float64(b * 2.0))) / Float64(a * 3.0)) end
function tmp = code(a, b, c) tmp = ((c * (a * 3.0)) / ((1.5 * ((a * c) / b)) - (b * 2.0))) / (a * 3.0); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision] / N[(N[(1.5 * N[(N[(a * c), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] - N[(b * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot \left(a \cdot 3\right)}{1.5 \cdot \frac{a \cdot c}{b} - b \cdot 2}}{a \cdot 3}
\end{array}
Initial program 53.6%
neg-sub053.6%
flip--53.4%
metadata-eval53.4%
pow253.4%
add-sqr-sqrt52.4%
sqrt-prod53.4%
sqr-neg53.4%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod53.4%
sqr-neg53.4%
sqrt-prod52.4%
add-sqr-sqrt53.4%
Applied egg-rr53.4%
neg-sub053.4%
Simplified53.4%
flip-+53.4%
Applied egg-rr54.9%
associate--r-99.4%
associate-*r*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
Simplified99.2%
+-commutative99.2%
*-commutative99.2%
associate-*l*99.4%
*-commutative99.4%
fma-define99.4%
*-commutative99.4%
neg-mul-199.4%
unpow-prod-down99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
Applied egg-rr99.4%
fma-undefine99.4%
+-inverses99.4%
+-rgt-identity99.4%
*-commutative99.4%
Simplified99.4%
Taylor expanded in a around 0 82.8%
Final simplification82.8%
(FPCore (a b c) :precision binary64 (/ (* c (- (* -0.375 (/ (* a c) (* b b))) 0.5)) b))
double code(double a, double b, double c) {
return (c * ((-0.375 * ((a * c) / (b * b))) - 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 * b))) - 0.5d0)) / b
end function
public static double code(double a, double b, double c) {
return (c * ((-0.375 * ((a * c) / (b * b))) - 0.5)) / b;
}
def code(a, b, c): return (c * ((-0.375 * ((a * c) / (b * b))) - 0.5)) / b
function code(a, b, c) return Float64(Float64(c * Float64(Float64(-0.375 * Float64(Float64(a * c) / Float64(b * b))) - 0.5)) / b) end
function tmp = code(a, b, c) tmp = (c * ((-0.375 * ((a * c) / (b * b))) - 0.5)) / b; end
code[a_, b_, c_] := N[(N[(c * N[(N[(-0.375 * N[(N[(a * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \left(-0.375 \cdot \frac{a \cdot c}{b \cdot b} - 0.5\right)}{b}
\end{array}
Initial program 53.6%
add-sqr-sqrt53.5%
pow253.5%
Applied egg-rr53.5%
Taylor expanded in b around inf 81.7%
fma-define81.6%
unpow281.6%
rem-square-sqrt82.2%
associate-*r/82.2%
*-commutative82.2%
unpow282.2%
rem-square-sqrt82.2%
times-frac82.2%
metadata-eval82.2%
associate-/l*82.2%
unpow282.2%
unpow282.2%
times-frac82.2%
unpow282.2%
Simplified82.2%
Taylor expanded in c around 0 82.2%
pow299.4%
Applied egg-rr82.2%
(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 53.6%
/-rgt-identity53.6%
metadata-eval53.6%
Simplified53.6%
Taylor expanded in b around inf 65.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 53.6%
neg-sub053.6%
flip--53.4%
metadata-eval53.4%
pow253.4%
add-sqr-sqrt52.4%
sqrt-prod53.4%
sqr-neg53.4%
sqrt-unprod0.0%
add-sqr-sqrt1.6%
sub-neg1.6%
neg-sub01.6%
add-sqr-sqrt0.0%
sqrt-unprod53.4%
sqr-neg53.4%
sqrt-prod52.4%
add-sqr-sqrt53.4%
Applied egg-rr53.4%
neg-sub053.4%
Simplified53.4%
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, 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)))