
(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 12 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
(if (<= b -9.8e+98)
(fma c (/ 0.5 b) (/ (* b -0.6666666666666666) a))
(if (<= b 5.1e-62)
(/ (/ (- b (sqrt (fma a (* c -3.0) (* b b)))) a) -3.0)
(/ (* c -0.5) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -9.8e+98) {
tmp = fma(c, (0.5 / b), ((b * -0.6666666666666666) / a));
} else if (b <= 5.1e-62) {
tmp = ((b - sqrt(fma(a, (c * -3.0), (b * b)))) / a) / -3.0;
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -9.8e+98) tmp = fma(c, Float64(0.5 / b), Float64(Float64(b * -0.6666666666666666) / a)); elseif (b <= 5.1e-62) tmp = Float64(Float64(Float64(b - sqrt(fma(a, Float64(c * -3.0), Float64(b * b)))) / a) / -3.0); else tmp = Float64(Float64(c * -0.5) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -9.8e+98], N[(c * N[(0.5 / b), $MachinePrecision] + N[(N[(b * -0.6666666666666666), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 5.1e-62], N[(N[(N[(b - N[Sqrt[N[(a * N[(c * -3.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision] / -3.0), $MachinePrecision], N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.8 \cdot 10^{+98}:\\
\;\;\;\;\mathsf{fma}\left(c, \frac{0.5}{b}, \frac{b \cdot -0.6666666666666666}{a}\right)\\
\mathbf{elif}\;b \leq 5.1 \cdot 10^{-62}:\\
\;\;\;\;\frac{\frac{b - \sqrt{\mathsf{fma}\left(a, c \cdot -3, b \cdot b\right)}}{a}}{-3}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot -0.5}{b}\\
\end{array}
\end{array}
if b < -9.79999999999999958e98Initial program 55.2%
Taylor expanded in b around -inf
mul-1-negN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
lower-neg.f6496.4
Applied rewrites96.4%
Taylor expanded in c around 0
Applied rewrites96.8%
if -9.79999999999999958e98 < b < 5.1e-62Initial program 79.0%
Applied rewrites79.1%
if 5.1e-62 < b Initial program 11.1%
Taylor expanded in b around inf
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6487.1
Applied rewrites87.1%
Final simplification86.1%
(FPCore (a b c)
:precision binary64
(if (<= b -1900.0)
(fma c (/ 0.5 b) (/ (* b -0.6666666666666666) a))
(if (<= b 5.1e-62)
(/
(fma
(sqrt (fma b b (* -3.0 (* a c))))
0.3333333333333333
(* b -0.3333333333333333))
a)
(/ (* c -0.5) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1900.0) {
tmp = fma(c, (0.5 / b), ((b * -0.6666666666666666) / a));
} else if (b <= 5.1e-62) {
tmp = fma(sqrt(fma(b, b, (-3.0 * (a * c)))), 0.3333333333333333, (b * -0.3333333333333333)) / a;
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -1900.0) tmp = fma(c, Float64(0.5 / b), Float64(Float64(b * -0.6666666666666666) / a)); elseif (b <= 5.1e-62) tmp = Float64(fma(sqrt(fma(b, b, Float64(-3.0 * Float64(a * c)))), 0.3333333333333333, Float64(b * -0.3333333333333333)) / a); else tmp = Float64(Float64(c * -0.5) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -1900.0], N[(c * N[(0.5 / b), $MachinePrecision] + N[(N[(b * -0.6666666666666666), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 5.1e-62], N[(N[(N[Sqrt[N[(b * b + N[(-3.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * 0.3333333333333333 + N[(b * -0.3333333333333333), $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision], N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1900:\\
\;\;\;\;\mathsf{fma}\left(c, \frac{0.5}{b}, \frac{b \cdot -0.6666666666666666}{a}\right)\\
\mathbf{elif}\;b \leq 5.1 \cdot 10^{-62}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(b, b, -3 \cdot \left(a \cdot c\right)\right)}, 0.3333333333333333, b \cdot -0.3333333333333333\right)}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot -0.5}{b}\\
\end{array}
\end{array}
if b < -1900Initial program 63.8%
Taylor expanded in b around -inf
mul-1-negN/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
lower-neg.f6490.9
Applied rewrites90.9%
Taylor expanded in c around 0
Applied rewrites91.0%
if -1900 < b < 5.1e-62Initial program 77.1%
Applied rewrites77.0%
lift-/.f64N/A
lift-/.f64N/A
associate-/l/N/A
associate-/r*N/A
lower-/.f64N/A
lower-/.f6477.0
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lower-*.f6477.0
Applied rewrites77.0%
lift-/.f64N/A
lift--.f64N/A
div-subN/A
lower--.f64N/A
div-invN/A
lower-*.f64N/A
metadata-evalN/A
div-invN/A
lower-*.f64N/A
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-*.f64N/A
associate-*r*N/A
*-commutativeN/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f64N/A
lower-*.f64N/A
metadata-eval76.9
Applied rewrites76.9%
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
distribute-rgt-neg-inN/A
metadata-evalN/A
metadata-evalN/A
lower-fma.f64N/A
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-eval76.9
Applied rewrites76.9%
if 5.1e-62 < b Initial program 17.2%
Taylor expanded in b around inf
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6486.8
Applied rewrites86.8%
herbie shell --seed 2024222
(FPCore (a b c)
:name "Cubic critical"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))