
(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 9 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 -3.0 (* c a) (pow b 2.0))))))
double code(double a, double b, double c) {
return c / (-b - sqrt(fma(-3.0, (c * a), pow(b, 2.0))));
}
function code(a, b, c) return Float64(c / Float64(Float64(-b) - sqrt(fma(-3.0, Float64(c * a), (b ^ 2.0))))) end
code[a_, b_, c_] := N[(c / N[((-b) - N[Sqrt[N[(-3.0 * N[(c * a), $MachinePrecision] + N[Power[b, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-3, c \cdot a, {b}^{2}\right)}}
\end{array}
Initial program 31.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
add-cbrt-cube31.8%
pow1/331.7%
pow331.7%
Applied egg-rr31.7%
flip-+31.6%
Applied egg-rr32.8%
associate--r-99.5%
*-commutative99.5%
associate-*r*99.2%
*-commutative99.2%
associate-*l*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
associate-*l*99.2%
Simplified99.2%
div-inv99.1%
+-commutative99.1%
*-commutative99.1%
fma-define99.1%
*-commutative99.1%
neg-mul-199.1%
unpow-prod-down99.1%
metadata-eval99.1%
*-un-lft-identity99.1%
*-commutative99.1%
Applied egg-rr99.1%
*-commutative99.1%
times-frac99.2%
associate-*r/99.2%
*-lft-identity99.2%
associate-/r*99.3%
fma-undefine99.3%
+-inverses99.3%
+-rgt-identity99.3%
associate-*r*99.3%
*-commutative99.3%
sub-neg99.3%
+-commutative99.3%
Simplified99.3%
Taylor expanded in a around 0 99.8%
(FPCore (a b c) :precision binary64 (/ (/ (* a (* c 3.0)) (* a 3.0)) (- (- b) (sqrt (* c (+ (* -3.0 a) (/ (pow b 2.0) c)))))))
double code(double a, double b, double c) {
return ((a * (c * 3.0)) / (a * 3.0)) / (-b - sqrt((c * ((-3.0 * a) + (pow(b, 2.0) / c)))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((a * (c * 3.0d0)) / (a * 3.0d0)) / (-b - sqrt((c * (((-3.0d0) * a) + ((b ** 2.0d0) / c)))))
end function
public static double code(double a, double b, double c) {
return ((a * (c * 3.0)) / (a * 3.0)) / (-b - Math.sqrt((c * ((-3.0 * a) + (Math.pow(b, 2.0) / c)))));
}
def code(a, b, c): return ((a * (c * 3.0)) / (a * 3.0)) / (-b - math.sqrt((c * ((-3.0 * a) + (math.pow(b, 2.0) / c)))))
function code(a, b, c) return Float64(Float64(Float64(a * Float64(c * 3.0)) / Float64(a * 3.0)) / Float64(Float64(-b) - sqrt(Float64(c * Float64(Float64(-3.0 * a) + Float64((b ^ 2.0) / c)))))) end
function tmp = code(a, b, c) tmp = ((a * (c * 3.0)) / (a * 3.0)) / (-b - sqrt((c * ((-3.0 * a) + ((b ^ 2.0) / c))))); end
code[a_, b_, c_] := N[(N[(N[(a * N[(c * 3.0), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(c * N[(N[(-3.0 * a), $MachinePrecision] + N[(N[Power[b, 2.0], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{a \cdot \left(c \cdot 3\right)}{a \cdot 3}}{\left(-b\right) - \sqrt{c \cdot \left(-3 \cdot a + \frac{{b}^{2}}{c}\right)}}
\end{array}
Initial program 31.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
add-cbrt-cube31.8%
pow1/331.7%
pow331.7%
Applied egg-rr31.7%
flip-+31.6%
Applied egg-rr32.8%
associate--r-99.5%
*-commutative99.5%
associate-*r*99.2%
*-commutative99.2%
associate-*l*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
associate-*l*99.2%
Simplified99.2%
div-inv99.1%
+-commutative99.1%
*-commutative99.1%
fma-define99.1%
*-commutative99.1%
neg-mul-199.1%
unpow-prod-down99.1%
metadata-eval99.1%
*-un-lft-identity99.1%
*-commutative99.1%
Applied egg-rr99.1%
*-commutative99.1%
times-frac99.2%
associate-*r/99.2%
*-lft-identity99.2%
associate-/r*99.3%
fma-undefine99.3%
+-inverses99.3%
+-rgt-identity99.3%
associate-*r*99.3%
*-commutative99.3%
sub-neg99.3%
+-commutative99.3%
Simplified99.3%
Taylor expanded in c around inf 99.3%
(FPCore (a b c) :precision binary64 (/ (/ (* a (* c 3.0)) (* a 3.0)) (- (- b) (sqrt (+ (pow b 2.0) (* -3.0 (* c a)))))))
double code(double a, double b, double c) {
return ((a * (c * 3.0)) / (a * 3.0)) / (-b - sqrt((pow(b, 2.0) + (-3.0 * (c * a)))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((a * (c * 3.0d0)) / (a * 3.0d0)) / (-b - sqrt(((b ** 2.0d0) + ((-3.0d0) * (c * a)))))
end function
public static double code(double a, double b, double c) {
return ((a * (c * 3.0)) / (a * 3.0)) / (-b - Math.sqrt((Math.pow(b, 2.0) + (-3.0 * (c * a)))));
}
def code(a, b, c): return ((a * (c * 3.0)) / (a * 3.0)) / (-b - math.sqrt((math.pow(b, 2.0) + (-3.0 * (c * a)))))
function code(a, b, c) return Float64(Float64(Float64(a * Float64(c * 3.0)) / Float64(a * 3.0)) / Float64(Float64(-b) - sqrt(Float64((b ^ 2.0) + Float64(-3.0 * Float64(c * a)))))) end
function tmp = code(a, b, c) tmp = ((a * (c * 3.0)) / (a * 3.0)) / (-b - sqrt(((b ^ 2.0) + (-3.0 * (c * a))))); end
code[a_, b_, c_] := N[(N[(N[(a * N[(c * 3.0), $MachinePrecision]), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] + N[(-3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{a \cdot \left(c \cdot 3\right)}{a \cdot 3}}{\left(-b\right) - \sqrt{{b}^{2} + -3 \cdot \left(c \cdot a\right)}}
\end{array}
Initial program 31.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
add-cbrt-cube31.8%
pow1/331.7%
pow331.7%
Applied egg-rr31.7%
flip-+31.6%
Applied egg-rr32.8%
associate--r-99.5%
*-commutative99.5%
associate-*r*99.2%
*-commutative99.2%
associate-*l*99.2%
*-commutative99.2%
associate-*r*99.2%
*-commutative99.2%
associate-*l*99.2%
Simplified99.2%
div-inv99.1%
+-commutative99.1%
*-commutative99.1%
fma-define99.1%
*-commutative99.1%
neg-mul-199.1%
unpow-prod-down99.1%
metadata-eval99.1%
*-un-lft-identity99.1%
*-commutative99.1%
Applied egg-rr99.1%
*-commutative99.1%
times-frac99.2%
associate-*r/99.2%
*-lft-identity99.2%
associate-/r*99.3%
fma-undefine99.3%
+-inverses99.3%
+-rgt-identity99.3%
associate-*r*99.3%
*-commutative99.3%
sub-neg99.3%
+-commutative99.3%
Simplified99.3%
Taylor expanded in c around 0 99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (+ (* -0.5 (/ c b)) (* -0.375 (/ (* a (pow c 2.0)) (pow b 3.0)))))
double code(double a, double b, double c) {
return (-0.5 * (c / b)) + (-0.375 * ((a * pow(c, 2.0)) / pow(b, 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 = ((-0.5d0) * (c / b)) + ((-0.375d0) * ((a * (c ** 2.0d0)) / (b ** 3.0d0)))
end function
public static double code(double a, double b, double c) {
return (-0.5 * (c / b)) + (-0.375 * ((a * Math.pow(c, 2.0)) / Math.pow(b, 3.0)));
}
def code(a, b, c): return (-0.5 * (c / b)) + (-0.375 * ((a * math.pow(c, 2.0)) / math.pow(b, 3.0)))
function code(a, b, c) return Float64(Float64(-0.5 * Float64(c / b)) + Float64(-0.375 * Float64(Float64(a * (c ^ 2.0)) / (b ^ 3.0)))) end
function tmp = code(a, b, c) tmp = (-0.5 * (c / b)) + (-0.375 * ((a * (c ^ 2.0)) / (b ^ 3.0))); end
code[a_, b_, c_] := N[(N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision] + N[(-0.375 * N[(N[(a * N[Power[c, 2.0], $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b} + -0.375 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}
\end{array}
Initial program 31.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
Taylor expanded in a around 0 90.4%
(FPCore (a b c) :precision binary64 (+ (* c (* (* c a) (* -0.375 (pow b -3.0)))) (* c (/ -0.5 b))))
double code(double a, double b, double c) {
return (c * ((c * a) * (-0.375 * pow(b, -3.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 = (c * ((c * a) * ((-0.375d0) * (b ** (-3.0d0))))) + (c * ((-0.5d0) / b))
end function
public static double code(double a, double b, double c) {
return (c * ((c * a) * (-0.375 * Math.pow(b, -3.0)))) + (c * (-0.5 / b));
}
def code(a, b, c): return (c * ((c * a) * (-0.375 * math.pow(b, -3.0)))) + (c * (-0.5 / b))
function code(a, b, c) return Float64(Float64(c * Float64(Float64(c * a) * Float64(-0.375 * (b ^ -3.0)))) + Float64(c * Float64(-0.5 / b))) end
function tmp = code(a, b, c) tmp = (c * ((c * a) * (-0.375 * (b ^ -3.0)))) + (c * (-0.5 / b)); end
code[a_, b_, c_] := N[(N[(c * N[(N[(c * a), $MachinePrecision] * N[(-0.375 * N[Power[b, -3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * N[(-0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \left(\left(c \cdot a\right) \cdot \left(-0.375 \cdot {b}^{-3}\right)\right) + c \cdot \frac{-0.5}{b}
\end{array}
Initial program 31.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
Taylor expanded in c around 0 90.2%
sub-neg90.2%
*-commutative90.2%
div-inv90.2%
pow-flip90.2%
metadata-eval90.2%
un-div-inv90.2%
Applied egg-rr90.2%
distribute-rgt-in90.2%
associate-*l*90.2%
*-commutative90.2%
distribute-neg-frac90.2%
metadata-eval90.2%
Applied egg-rr90.2%
Final simplification90.2%
(FPCore (a b c) :precision binary64 (* c (- (* (* c a) (* -0.375 (pow b -3.0))) (/ 0.5 b))))
double code(double a, double b, double c) {
return c * (((c * a) * (-0.375 * 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 * (((c * a) * ((-0.375d0) * (b ** (-3.0d0)))) - (0.5d0 / b))
end function
public static double code(double a, double b, double c) {
return c * (((c * a) * (-0.375 * Math.pow(b, -3.0))) - (0.5 / b));
}
def code(a, b, c): return c * (((c * a) * (-0.375 * math.pow(b, -3.0))) - (0.5 / b))
function code(a, b, c) return Float64(c * Float64(Float64(Float64(c * a) * Float64(-0.375 * (b ^ -3.0))) - Float64(0.5 / b))) end
function tmp = code(a, b, c) tmp = c * (((c * a) * (-0.375 * (b ^ -3.0))) - (0.5 / b)); end
code[a_, b_, c_] := N[(c * N[(N[(N[(c * a), $MachinePrecision] * N[(-0.375 * N[Power[b, -3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \left(\left(c \cdot a\right) \cdot \left(-0.375 \cdot {b}^{-3}\right) - \frac{0.5}{b}\right)
\end{array}
Initial program 31.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
Taylor expanded in c around 0 90.2%
sub-neg90.2%
*-commutative90.2%
div-inv90.2%
pow-flip90.2%
metadata-eval90.2%
un-div-inv90.2%
Applied egg-rr90.2%
sub-neg90.2%
associate-*l*90.2%
Applied egg-rr90.2%
*-commutative90.2%
*-commutative90.2%
Simplified90.2%
(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.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
Taylor expanded in b around inf 80.9%
associate-*r/80.9%
*-commutative80.9%
Simplified80.9%
(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.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
Taylor expanded in c around 0 90.2%
Taylor expanded in a around 0 80.7%
(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.8%
sqr-neg31.8%
sqr-neg31.8%
associate-*l*31.8%
Simplified31.8%
add-cbrt-cube31.8%
pow1/331.7%
pow331.7%
Applied egg-rr31.7%
*-un-lft-identity31.7%
neg-mul-131.7%
fma-define31.7%
pow231.7%
pow-pow31.8%
metadata-eval31.8%
pow131.8%
associate-*r*31.8%
*-commutative31.8%
*-commutative31.8%
Applied egg-rr31.8%
associate-*r/31.8%
*-commutative31.8%
times-frac31.8%
metadata-eval31.8%
unpow231.8%
fma-neg31.9%
*-commutative31.9%
distribute-rgt-neg-in31.9%
distribute-rgt-neg-in31.9%
metadata-eval31.9%
Simplified31.9%
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%
herbie shell --seed 2024096
(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)))