
(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 6 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 (pow (* (- (- b) (sqrt (fma (* c -3.0) a (* b b)))) (pow c -1.0)) -1.0))
double code(double a, double b, double c) {
return pow(((-b - sqrt(fma((c * -3.0), a, (b * b)))) * pow(c, -1.0)), -1.0);
}
function code(a, b, c) return Float64(Float64(Float64(-b) - sqrt(fma(Float64(c * -3.0), a, Float64(b * b)))) * (c ^ -1.0)) ^ -1.0 end
code[a_, b_, c_] := N[Power[N[(N[((-b) - N[Sqrt[N[(N[(c * -3.0), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Power[c, -1.0], $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]
\begin{array}{l}
\\
{\left(\left(\left(-b\right) - \sqrt{\mathsf{fma}\left(c \cdot -3, a, b \cdot b\right)}\right) \cdot {c}^{-1}\right)}^{-1}
\end{array}
Initial program 33.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-/.f6433.6
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6433.6
Applied rewrites33.6%
Applied rewrites34.6%
Taylor expanded in a around 0
lower-/.f6499.4
Applied rewrites99.4%
lift-*.f64N/A
lift-pow.f64N/A
lift-pow.f64N/A
pow-prod-downN/A
unpow-1N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6499.4
Applied rewrites99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (/ (* (* a c) 3.0) (* (* 3.0 a) (- (- b) (sqrt (fma (* -3.0 c) a (* b b)))))))
double code(double a, double b, double c) {
return ((a * c) * 3.0) / ((3.0 * a) * (-b - sqrt(fma((-3.0 * c), a, (b * b)))));
}
function code(a, b, c) return Float64(Float64(Float64(a * c) * 3.0) / Float64(Float64(3.0 * a) * Float64(Float64(-b) - sqrt(fma(Float64(-3.0 * c), a, Float64(b * b)))))) end
code[a_, b_, c_] := N[(N[(N[(a * c), $MachinePrecision] * 3.0), $MachinePrecision] / N[(N[(3.0 * a), $MachinePrecision] * N[((-b) - N[Sqrt[N[(N[(-3.0 * c), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(a \cdot c\right) \cdot 3}{\left(3 \cdot a\right) \cdot \left(\left(-b\right) - \sqrt{\mathsf{fma}\left(-3 \cdot c, a, b \cdot b\right)}\right)}
\end{array}
Initial program 33.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-/.f6433.6
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6433.6
Applied rewrites33.6%
Applied rewrites34.6%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f64N/A
lower-*.f6499.1
Applied rewrites99.1%
(FPCore (a b c) :precision binary64 (/ (* (* -3.0 c) a) (* (* 3.0 a) (+ b (sqrt (fma (* -3.0 c) a (* b b)))))))
double code(double a, double b, double c) {
return ((-3.0 * c) * a) / ((3.0 * a) * (b + sqrt(fma((-3.0 * c), a, (b * b)))));
}
function code(a, b, c) return Float64(Float64(Float64(-3.0 * c) * a) / Float64(Float64(3.0 * a) * Float64(b + sqrt(fma(Float64(-3.0 * c), a, Float64(b * b)))))) end
code[a_, b_, c_] := N[(N[(N[(-3.0 * c), $MachinePrecision] * a), $MachinePrecision] / N[(N[(3.0 * a), $MachinePrecision] * N[(b + N[Sqrt[N[(N[(-3.0 * c), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-3 \cdot c\right) \cdot a}{\left(3 \cdot a\right) \cdot \left(b + \sqrt{\mathsf{fma}\left(-3 \cdot c, a, b \cdot b\right)}\right)}
\end{array}
Initial program 33.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-/.f6433.6
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6433.6
Applied rewrites33.6%
Applied rewrites34.6%
lift--.f64N/A
lift-fma.f64N/A
+-commutativeN/A
associate--r+N/A
lower--.f64N/A
lower--.f64N/A
lower-*.f6499.1
Applied rewrites99.1%
Final simplification99.1%
(FPCore (a b c) :precision binary64 (/ 0.3333333333333333 (* (fma (/ a (* b b)) 0.5 (/ -0.6666666666666666 c)) b)))
double code(double a, double b, double c) {
return 0.3333333333333333 / (fma((a / (b * b)), 0.5, (-0.6666666666666666 / c)) * b);
}
function code(a, b, c) return Float64(0.3333333333333333 / Float64(fma(Float64(a / Float64(b * b)), 0.5, Float64(-0.6666666666666666 / c)) * b)) end
code[a_, b_, c_] := 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}
\\
\frac{0.3333333333333333}{\mathsf{fma}\left(\frac{a}{b \cdot b}, 0.5, \frac{-0.6666666666666666}{c}\right) \cdot b}
\end{array}
Initial program 33.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-/.f6433.6
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6433.6
Applied rewrites33.6%
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-/.f6490.6
Applied rewrites90.6%
(FPCore (a b c) :precision binary64 (/ 0.3333333333333333 (fma (/ a b) 0.5 (* (/ b c) -0.6666666666666666))))
double code(double a, double b, double c) {
return 0.3333333333333333 / fma((a / b), 0.5, ((b / c) * -0.6666666666666666));
}
function code(a, b, c) return Float64(0.3333333333333333 / fma(Float64(a / b), 0.5, Float64(Float64(b / c) * -0.6666666666666666))) end
code[a_, b_, c_] := N[(0.3333333333333333 / N[(N[(a / b), $MachinePrecision] * 0.5 + N[(N[(b / c), $MachinePrecision] * -0.6666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.3333333333333333}{\mathsf{fma}\left(\frac{a}{b}, 0.5, \frac{b}{c} \cdot -0.6666666666666666\right)}
\end{array}
Initial program 33.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-/.f6433.6
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6433.6
Applied rewrites33.6%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6490.6
Applied rewrites90.6%
(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 33.6%
Taylor expanded in a around 0
lower-*.f64N/A
lower-/.f6479.7
Applied rewrites79.7%
herbie shell --seed 2024307
(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)))