
(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 8 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
(let* ((t_0 (* b (* b b))))
(*
c
(fma
c
(fma
c
(fma
-0.5625
(/ (* a a) (pow b 5.0))
(/
(* (* c -0.16666666666666666) (* (* a a) (* (* a a) 6.328125)))
(* (* a b) (* (* b b) (* b t_0)))))
(/ (* a -0.375) t_0))
(/ -0.5 b)))))
double code(double a, double b, double c) {
double t_0 = b * (b * b);
return c * fma(c, fma(c, fma(-0.5625, ((a * a) / pow(b, 5.0)), (((c * -0.16666666666666666) * ((a * a) * ((a * a) * 6.328125))) / ((a * b) * ((b * b) * (b * t_0))))), ((a * -0.375) / t_0)), (-0.5 / b));
}
function code(a, b, c) t_0 = Float64(b * Float64(b * b)) return Float64(c * fma(c, fma(c, fma(-0.5625, Float64(Float64(a * a) / (b ^ 5.0)), Float64(Float64(Float64(c * -0.16666666666666666) * Float64(Float64(a * a) * Float64(Float64(a * a) * 6.328125))) / Float64(Float64(a * b) * Float64(Float64(b * b) * Float64(b * t_0))))), Float64(Float64(a * -0.375) / t_0)), Float64(-0.5 / b))) end
code[a_, b_, c_] := Block[{t$95$0 = N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]}, N[(c * N[(c * N[(c * N[(-0.5625 * N[(N[(a * a), $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(c * -0.16666666666666666), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * 6.328125), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(a * b), $MachinePrecision] * N[(N[(b * b), $MachinePrecision] * N[(b * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(a * -0.375), $MachinePrecision] / t$95$0), $MachinePrecision]), $MachinePrecision] + N[(-0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := b \cdot \left(b \cdot b\right)\\
c \cdot \mathsf{fma}\left(c, \mathsf{fma}\left(c, \mathsf{fma}\left(-0.5625, \frac{a \cdot a}{{b}^{5}}, \frac{\left(c \cdot -0.16666666666666666\right) \cdot \left(\left(a \cdot a\right) \cdot \left(\left(a \cdot a\right) \cdot 6.328125\right)\right)}{\left(a \cdot b\right) \cdot \left(\left(b \cdot b\right) \cdot \left(b \cdot t\_0\right)\right)}\right), \frac{a \cdot -0.375}{t\_0}\right), \frac{-0.5}{b}\right)
\end{array}
\end{array}
Initial program 18.1%
Taylor expanded in c around 0
Simplified97.9%
Applied egg-rr97.9%
Final simplification97.9%
(FPCore (a b c) :precision binary64 (/ (fma -0.5625 (/ (* c (* c (* c (* a a)))) (pow b 4.0)) (fma a (* -0.375 (* c (/ c (* b b)))) (* c -0.5))) b))
double code(double a, double b, double c) {
return fma(-0.5625, ((c * (c * (c * (a * a)))) / pow(b, 4.0)), fma(a, (-0.375 * (c * (c / (b * b)))), (c * -0.5))) / b;
}
function code(a, b, c) return Float64(fma(-0.5625, Float64(Float64(c * Float64(c * Float64(c * Float64(a * a)))) / (b ^ 4.0)), fma(a, Float64(-0.375 * Float64(c * Float64(c / Float64(b * b)))), Float64(c * -0.5))) / b) end
code[a_, b_, c_] := N[(N[(-0.5625 * N[(N[(c * N[(c * N[(c * N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Power[b, 4.0], $MachinePrecision]), $MachinePrecision] + N[(a * N[(-0.375 * N[(c * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-0.5625, \frac{c \cdot \left(c \cdot \left(c \cdot \left(a \cdot a\right)\right)\right)}{{b}^{4}}, \mathsf{fma}\left(a, -0.375 \cdot \left(c \cdot \frac{c}{b \cdot b}\right), c \cdot -0.5\right)\right)}{b}
\end{array}
Initial program 18.1%
Taylor expanded in b around inf
Simplified97.6%
(FPCore (a b c) :precision binary64 (/ (fma -0.5625 (/ (* (* a a) (* c (* c c))) (* b (* b (* b b)))) (fma -0.375 (/ (* a (* c c)) (* b b)) (* c -0.5))) b))
double code(double a, double b, double c) {
return fma(-0.5625, (((a * a) * (c * (c * c))) / (b * (b * (b * b)))), fma(-0.375, ((a * (c * c)) / (b * b)), (c * -0.5))) / b;
}
function code(a, b, c) return Float64(fma(-0.5625, Float64(Float64(Float64(a * a) * Float64(c * Float64(c * c))) / Float64(b * Float64(b * Float64(b * b)))), fma(-0.375, Float64(Float64(a * Float64(c * c)) / Float64(b * b)), Float64(c * -0.5))) / b) end
code[a_, b_, c_] := N[(N[(-0.5625 * N[(N[(N[(a * a), $MachinePrecision] * N[(c * N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(b * N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.375 * N[(N[(a * N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(-0.5625, \frac{\left(a \cdot a\right) \cdot \left(c \cdot \left(c \cdot c\right)\right)}{b \cdot \left(b \cdot \left(b \cdot b\right)\right)}, \mathsf{fma}\left(-0.375, \frac{a \cdot \left(c \cdot c\right)}{b \cdot b}, c \cdot -0.5\right)\right)}{b}
\end{array}
Initial program 18.1%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64N/A
metadata-eval18.1
Applied egg-rr18.1%
Taylor expanded in b around inf
lower-/.f64N/A
Simplified97.6%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-pow.f64N/A
lift-/.f6497.6
lift-pow.f64N/A
metadata-evalN/A
pow-prod-upN/A
pow2N/A
pow2N/A
lift-*.f64N/A
associate-*l*N/A
lift-*.f64N/A
lower-*.f6497.6
Applied egg-rr97.6%
(FPCore (a b c) :precision binary64 (fma a (/ (* -0.375 (* c c)) (* b (* b b))) (/ (* c -0.5) b)))
double code(double a, double b, double c) {
return fma(a, ((-0.375 * (c * c)) / (b * (b * b))), ((c * -0.5) / b));
}
function code(a, b, c) return fma(a, Float64(Float64(-0.375 * Float64(c * c)) / Float64(b * Float64(b * b))), Float64(Float64(c * -0.5) / b)) end
code[a_, b_, c_] := N[(a * N[(N[(-0.375 * N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(a, \frac{-0.375 \cdot \left(c \cdot c\right)}{b \cdot \left(b \cdot b\right)}, \frac{c \cdot -0.5}{b}\right)
\end{array}
Initial program 18.1%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
cube-multN/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6495.8
Simplified95.8%
Final simplification95.8%
(FPCore (a b c) :precision binary64 (/ (fma a (* -0.375 (* c (/ c (* b b)))) (* c -0.5)) b))
double code(double a, double b, double c) {
return fma(a, (-0.375 * (c * (c / (b * b)))), (c * -0.5)) / b;
}
function code(a, b, c) return Float64(fma(a, Float64(-0.375 * Float64(c * Float64(c / Float64(b * b)))), Float64(c * -0.5)) / b) end
code[a_, b_, c_] := N[(N[(a * N[(-0.375 * N[(c * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, -0.375 \cdot \left(c \cdot \frac{c}{b \cdot b}\right), c \cdot -0.5\right)}{b}
\end{array}
Initial program 18.1%
Taylor expanded in b around inf
lower-/.f64N/A
Simplified95.8%
(FPCore (a b c) :precision binary64 (* c (fma a (* -0.375 (/ c (* b (* b b)))) (/ -0.5 b))))
double code(double a, double b, double c) {
return c * fma(a, (-0.375 * (c / (b * (b * b)))), (-0.5 / b));
}
function code(a, b, c) return Float64(c * fma(a, Float64(-0.375 * Float64(c / Float64(b * Float64(b * b)))), Float64(-0.5 / b))) end
code[a_, b_, c_] := N[(c * N[(a * N[(-0.375 * N[(c / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \mathsf{fma}\left(a, -0.375 \cdot \frac{c}{b \cdot \left(b \cdot b\right)}, \frac{-0.5}{b}\right)
\end{array}
Initial program 18.1%
Taylor expanded in c around 0
sub-negN/A
distribute-rgt-inN/A
associate-*r/N/A
associate-*r*N/A
associate-*l/N/A
associate-*r/N/A
distribute-rgt-inN/A
lower-*.f64N/A
associate-*r/N/A
associate-*l/N/A
associate-*r*N/A
associate-*r/N/A
*-commutativeN/A
Simplified95.5%
Final simplification95.5%
(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 18.1%
Taylor expanded in b around inf
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6490.3
Simplified90.3%
(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 18.1%
Taylor expanded in b around inf
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6490.3
Simplified90.3%
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6490.0
Applied egg-rr90.0%
Final simplification90.0%
herbie shell --seed 2024219
(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)))