
(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 11 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 a) a) (- (- b) (sqrt (fma (* -3.0 a) c (* b b))))))
double code(double a, double b, double c) {
return ((c * a) / a) / (-b - sqrt(fma((-3.0 * a), c, (b * b))));
}
function code(a, b, c) return Float64(Float64(Float64(c * a) / a) / Float64(Float64(-b) - sqrt(fma(Float64(-3.0 * a), c, Float64(b * b))))) end
code[a_, b_, c_] := N[(N[(N[(c * a), $MachinePrecision] / a), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(-3.0 * a), $MachinePrecision] * c + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{c \cdot a}{a}}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-3 \cdot a, c, b \cdot b\right)}}
\end{array}
Initial program 49.4%
lift-/.f64N/A
lift-*.f64N/A
associate-/l/N/A
div-invN/A
lower-*.f64N/A
Applied rewrites49.4%
Applied rewrites99.5%
Final simplification99.5%
(FPCore (a b c)
:precision binary64
(if (<= b 1.32)
(/ (- (sqrt (fma b b (* (* c a) -3.0))) b) (* 3.0 a))
(/
0.3333333333333333
(* (fma (/ a (* b b)) 0.5 (/ -0.6666666666666666 c)) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 1.32) {
tmp = (sqrt(fma(b, b, ((c * a) * -3.0))) - b) / (3.0 * a);
} else {
tmp = 0.3333333333333333 / (fma((a / (b * b)), 0.5, (-0.6666666666666666 / c)) * b);
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 1.32) tmp = Float64(Float64(sqrt(fma(b, b, Float64(Float64(c * a) * -3.0))) - b) / Float64(3.0 * a)); else tmp = Float64(0.3333333333333333 / Float64(fma(Float64(a / Float64(b * b)), 0.5, Float64(-0.6666666666666666 / c)) * b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 1.32], N[(N[(N[Sqrt[N[(b * b + N[(N[(c * a), $MachinePrecision] * -3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision], N[(0.3333333333333333 / N[(N[(N[(a / N[(b * b), $MachinePrecision]), $MachinePrecision] * 0.5 + N[(-0.6666666666666666 / c), $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 1.32:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot -3\right)} - b}{3 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.3333333333333333}{\mathsf{fma}\left(\frac{a}{b \cdot b}, 0.5, \frac{-0.6666666666666666}{c}\right) \cdot b}\\
\end{array}
\end{array}
if b < 1.32000000000000006Initial program 81.3%
lift--.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
metadata-evalN/A
*-commutativeN/A
lower-*.f6481.4
Applied rewrites81.4%
if 1.32000000000000006 < b Initial program 50.6%
lift-/.f64N/A
clear-numN/A
lift-*.f64N/A
associate-/l*N/A
associate-/r*N/A
lower-/.f64N/A
metadata-evalN/A
lower-/.f6450.6
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6450.6
Applied rewrites50.6%
Taylor expanded in b around inf
lower-*.f64N/A
lower--.f64N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6486.0
Applied rewrites86.0%
Taylor expanded in b around inf
*-commutativeN/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6486.0
Applied rewrites86.0%
Final simplification85.3%
herbie shell --seed 2024234
(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)))