
(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 4 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 b b (* c (* a -3.0)))))))
double code(double a, double b, double c) {
return c / (-b - sqrt(fma(b, b, (c * (a * -3.0)))));
}
function code(a, b, c) return Float64(c / Float64(Float64(-b) - sqrt(fma(b, b, Float64(c * Float64(a * -3.0)))))) end
code[a_, b_, c_] := N[(c / N[((-b) - N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)}}
\end{array}
Initial program 19.0%
Applied egg-rr19.0%
associate-/r/N/A
flip--N/A
associate-*r/N/A
/-lowering-/.f64N/A
Applied egg-rr19.3%
Taylor expanded in a around 0
mul-1-negN/A
neg-lowering-neg.f6499.9
Simplified99.9%
+-commutativeN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f6499.9
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (a b c) :precision binary64 (/ (- c) (fma a (* (/ c b) -1.5) (* b 2.0))))
double code(double a, double b, double c) {
return -c / fma(a, ((c / b) * -1.5), (b * 2.0));
}
function code(a, b, c) return Float64(Float64(-c) / fma(a, Float64(Float64(c / b) * -1.5), Float64(b * 2.0))) end
code[a_, b_, c_] := N[((-c) / N[(a * N[(N[(c / b), $MachinePrecision] * -1.5), $MachinePrecision] + N[(b * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{\mathsf{fma}\left(a, \frac{c}{b} \cdot -1.5, b \cdot 2\right)}
\end{array}
Initial program 19.0%
Applied egg-rr19.0%
associate-/r/N/A
flip--N/A
associate-*r/N/A
/-lowering-/.f64N/A
Applied egg-rr19.3%
Taylor expanded in a around 0
mul-1-negN/A
neg-lowering-neg.f6499.9
Simplified99.9%
Taylor expanded in c around 0
*-commutativeN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
*-commutativeN/A
*-lowering-*.f6495.5
Simplified95.5%
(FPCore (a b c) :precision binary64 (/ c (- (- b) (fma a (* (/ c b) -1.5) b))))
double code(double a, double b, double c) {
return c / (-b - fma(a, ((c / b) * -1.5), b));
}
function code(a, b, c) return Float64(c / Float64(Float64(-b) - fma(a, Float64(Float64(c / b) * -1.5), b))) end
code[a_, b_, c_] := N[(c / N[((-b) - N[(a * N[(N[(c / b), $MachinePrecision] * -1.5), $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(-b\right) - \mathsf{fma}\left(a, \frac{c}{b} \cdot -1.5, b\right)}
\end{array}
Initial program 19.0%
Applied egg-rr19.0%
associate-/r/N/A
flip--N/A
associate-*r/N/A
/-lowering-/.f64N/A
Applied egg-rr19.3%
Taylor expanded in a around 0
mul-1-negN/A
neg-lowering-neg.f6499.9
Simplified99.9%
Taylor expanded in c around 0
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f6495.5
Simplified95.5%
Final simplification95.5%
(FPCore (a b c) :precision binary64 (* (/ c b) -0.5))
double code(double a, double b, double c) {
return (c / b) * -0.5;
}
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) * (-0.5d0)
end function
public static double code(double a, double b, double c) {
return (c / b) * -0.5;
}
def code(a, b, c): return (c / b) * -0.5
function code(a, b, c) return Float64(Float64(c / b) * -0.5) end
function tmp = code(a, b, c) tmp = (c / b) * -0.5; end
code[a_, b_, c_] := N[(N[(c / b), $MachinePrecision] * -0.5), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{b} \cdot -0.5
\end{array}
Initial program 19.0%
Taylor expanded in b around inf
*-lowering-*.f64N/A
/-lowering-/.f6489.5
Simplified89.5%
Final simplification89.5%
herbie shell --seed 2024204
(FPCore (a b c)
:name "Cubic critical, wide range"
:precision binary64
:pre (and (and (and (< 4.930380657631324e-32 a) (< a 2.028240960365167e+31)) (and (< 4.930380657631324e-32 b) (< b 2.028240960365167e+31))) (and (< 4.930380657631324e-32 c) (< c 2.028240960365167e+31)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))