
(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 7 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 (* (* c c) (* (* a a) -1.125))))
(fma
-0.5625
(/ (* (pow c 3.0) (* a a)) (pow b 5.0))
(fma
-0.16666666666666666
(/ (+ (* t_0 t_0) (* 5.0625 (pow (* c a) 4.0))) (* a (pow b 7.0)))
(fma -0.5 (/ c b) (* -0.375 (/ (* c c) (/ (pow b 3.0) a))))))))
double code(double a, double b, double c) {
double t_0 = (c * c) * ((a * a) * -1.125);
return fma(-0.5625, ((pow(c, 3.0) * (a * a)) / pow(b, 5.0)), fma(-0.16666666666666666, (((t_0 * t_0) + (5.0625 * pow((c * a), 4.0))) / (a * pow(b, 7.0))), fma(-0.5, (c / b), (-0.375 * ((c * c) / (pow(b, 3.0) / a))))));
}
function code(a, b, c) t_0 = Float64(Float64(c * c) * Float64(Float64(a * a) * -1.125)) return fma(-0.5625, Float64(Float64((c ^ 3.0) * Float64(a * a)) / (b ^ 5.0)), fma(-0.16666666666666666, Float64(Float64(Float64(t_0 * t_0) + Float64(5.0625 * (Float64(c * a) ^ 4.0))) / Float64(a * (b ^ 7.0))), fma(-0.5, Float64(c / b), Float64(-0.375 * Float64(Float64(c * c) / Float64((b ^ 3.0) / a)))))) end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(c * c), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * -1.125), $MachinePrecision]), $MachinePrecision]}, N[(-0.5625 * N[(N[(N[Power[c, 3.0], $MachinePrecision] * N[(a * a), $MachinePrecision]), $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision] + N[(-0.16666666666666666 * N[(N[(N[(t$95$0 * t$95$0), $MachinePrecision] + N[(5.0625 * N[Power[N[(c * a), $MachinePrecision], 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * N[Power[b, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.5 * N[(c / b), $MachinePrecision] + N[(-0.375 * N[(N[(c * c), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(c \cdot c\right) \cdot \left(\left(a \cdot a\right) \cdot -1.125\right)\\
\mathsf{fma}\left(-0.5625, \frac{{c}^{3} \cdot \left(a \cdot a\right)}{{b}^{5}}, \mathsf{fma}\left(-0.16666666666666666, \frac{t_0 \cdot t_0 + 5.0625 \cdot {\left(c \cdot a\right)}^{4}}{a \cdot {b}^{7}}, \mathsf{fma}\left(-0.5, \frac{c}{b}, -0.375 \cdot \frac{c \cdot c}{\frac{{b}^{3}}{a}}\right)\right)\right)
\end{array}
\end{array}
Initial program 28.3%
/-rgt-identity28.3%
metadata-eval28.3%
associate-/l*28.3%
associate-*r/28.3%
*-commutative28.3%
associate-*l/28.3%
associate-*r/28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
neg-mul-128.3%
distribute-rgt-neg-in28.3%
times-frac28.3%
metadata-eval28.3%
neg-mul-128.3%
Simplified28.4%
Taylor expanded in b around inf 96.5%
fma-def96.5%
unpow296.5%
fma-def96.5%
Simplified96.5%
pow196.5%
pow-prod-down96.5%
Applied egg-rr96.5%
unpow196.5%
Simplified96.5%
unpow296.5%
associate-*l*96.5%
associate-*l*96.5%
Applied egg-rr96.5%
Final simplification96.5%
(FPCore (a b c) :precision binary64 (fma -0.5625 (* (* a a) (/ (pow c 3.0) (pow b 5.0))) (fma -0.375 (* c (* a (/ c (pow b 3.0)))) (/ -0.5 (/ b c)))))
double code(double a, double b, double c) {
return fma(-0.5625, ((a * a) * (pow(c, 3.0) / pow(b, 5.0))), fma(-0.375, (c * (a * (c / pow(b, 3.0)))), (-0.5 / (b / c))));
}
function code(a, b, c) return fma(-0.5625, Float64(Float64(a * a) * Float64((c ^ 3.0) / (b ^ 5.0))), fma(-0.375, Float64(c * Float64(a * Float64(c / (b ^ 3.0)))), Float64(-0.5 / Float64(b / c)))) end
code[a_, b_, c_] := N[(-0.5625 * N[(N[(a * a), $MachinePrecision] * N[(N[Power[c, 3.0], $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.375 * N[(c * N[(a * N[(c / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.5 / N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5625, \left(a \cdot a\right) \cdot \frac{{c}^{3}}{{b}^{5}}, \mathsf{fma}\left(-0.375, c \cdot \left(a \cdot \frac{c}{{b}^{3}}\right), \frac{-0.5}{\frac{b}{c}}\right)\right)
\end{array}
Initial program 28.3%
neg-sub028.3%
associate-+l-28.3%
sub0-neg28.3%
neg-mul-128.3%
associate-*r/28.3%
*-commutative28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
*-commutative28.3%
times-frac28.3%
Simplified28.4%
add-exp-log28.4%
Applied egg-rr28.4%
Taylor expanded in b around inf 94.9%
fma-def94.9%
unpow294.9%
associate-*l/94.9%
*-commutative94.9%
+-commutative94.9%
fma-def94.9%
unpow294.9%
associate-/l*94.9%
associate-*l/94.9%
*-commutative94.9%
associate-/r/94.9%
associate-*r/94.9%
associate-/l*94.7%
Simplified94.7%
Final simplification94.7%
(FPCore (a b c) :precision binary64 (fma -0.5625 (/ (pow c 3.0) (/ (pow b 5.0) (* a a))) (fma -0.5 (/ c b) (/ (* (* c c) (* a -0.375)) (pow b 3.0)))))
double code(double a, double b, double c) {
return fma(-0.5625, (pow(c, 3.0) / (pow(b, 5.0) / (a * a))), fma(-0.5, (c / b), (((c * c) * (a * -0.375)) / pow(b, 3.0))));
}
function code(a, b, c) return fma(-0.5625, Float64((c ^ 3.0) / Float64((b ^ 5.0) / Float64(a * a))), fma(-0.5, Float64(c / b), Float64(Float64(Float64(c * c) * Float64(a * -0.375)) / (b ^ 3.0)))) end
code[a_, b_, c_] := N[(-0.5625 * N[(N[Power[c, 3.0], $MachinePrecision] / N[(N[Power[b, 5.0], $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.5 * N[(c / b), $MachinePrecision] + N[(N[(N[(c * c), $MachinePrecision] * N[(a * -0.375), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-0.5625, \frac{{c}^{3}}{\frac{{b}^{5}}{a \cdot a}}, \mathsf{fma}\left(-0.5, \frac{c}{b}, \frac{\left(c \cdot c\right) \cdot \left(a \cdot -0.375\right)}{{b}^{3}}\right)\right)
\end{array}
Initial program 28.3%
/-rgt-identity28.3%
metadata-eval28.3%
associate-/l*28.3%
associate-*r/28.3%
*-commutative28.3%
associate-*l/28.3%
associate-*r/28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
neg-mul-128.3%
distribute-rgt-neg-in28.3%
times-frac28.3%
metadata-eval28.3%
neg-mul-128.3%
Simplified28.4%
Taylor expanded in b around inf 94.9%
fma-def94.9%
associate-/l*94.9%
unpow294.9%
fma-def94.9%
associate-*r/94.9%
*-commutative94.9%
associate-*r*94.9%
unpow294.9%
Simplified94.9%
Final simplification94.9%
(FPCore (a b c) :precision binary64 (+ (* -0.5 (/ c b)) (/ (* a -0.375) (/ (pow b 3.0) (* c c)))))
double code(double a, double b, double c) {
return (-0.5 * (c / b)) + ((a * -0.375) / (pow(b, 3.0) / (c * c)));
}
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)) + ((a * (-0.375d0)) / ((b ** 3.0d0) / (c * c)))
end function
public static double code(double a, double b, double c) {
return (-0.5 * (c / b)) + ((a * -0.375) / (Math.pow(b, 3.0) / (c * c)));
}
def code(a, b, c): return (-0.5 * (c / b)) + ((a * -0.375) / (math.pow(b, 3.0) / (c * c)))
function code(a, b, c) return Float64(Float64(-0.5 * Float64(c / b)) + Float64(Float64(a * -0.375) / Float64((b ^ 3.0) / Float64(c * c)))) end
function tmp = code(a, b, c) tmp = (-0.5 * (c / b)) + ((a * -0.375) / ((b ^ 3.0) / (c * c))); end
code[a_, b_, c_] := N[(N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision] + N[(N[(a * -0.375), $MachinePrecision] / N[(N[Power[b, 3.0], $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b} + \frac{a \cdot -0.375}{\frac{{b}^{3}}{c \cdot c}}
\end{array}
Initial program 28.3%
/-rgt-identity28.3%
metadata-eval28.3%
associate-/l*28.3%
associate-*r/28.3%
*-commutative28.3%
associate-*l/28.3%
associate-*r/28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
neg-mul-128.3%
distribute-rgt-neg-in28.3%
times-frac28.3%
metadata-eval28.3%
neg-mul-128.3%
Simplified28.4%
Taylor expanded in b around inf 92.1%
fma-def92.1%
associate-*r/92.1%
*-commutative92.1%
associate-*r*92.1%
unpow292.1%
Simplified92.1%
fma-udef92.1%
associate-/l*92.1%
*-commutative92.1%
Applied egg-rr92.1%
Final simplification92.1%
(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 28.3%
/-rgt-identity28.3%
metadata-eval28.3%
associate-/l*28.3%
associate-*r/28.3%
*-commutative28.3%
associate-*l/28.3%
associate-*r/28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
neg-mul-128.3%
distribute-rgt-neg-in28.3%
times-frac28.3%
metadata-eval28.3%
neg-mul-128.3%
Simplified28.4%
Taylor expanded in b around inf 92.1%
fma-def92.1%
associate-*r/92.1%
*-commutative92.1%
associate-*r*92.1%
unpow292.1%
Simplified92.1%
Taylor expanded in c around 0 83.4%
associate-*r/83.4%
associate-/l*83.2%
Simplified83.2%
associate-/r/83.1%
Applied egg-rr83.1%
Final simplification83.1%
(FPCore (a b c) :precision binary64 (/ -0.5 (/ b c)))
double code(double a, double b, double c) {
return -0.5 / (b / c);
}
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) / (b / c)
end function
public static double code(double a, double b, double c) {
return -0.5 / (b / c);
}
def code(a, b, c): return -0.5 / (b / c)
function code(a, b, c) return Float64(-0.5 / Float64(b / c)) end
function tmp = code(a, b, c) tmp = -0.5 / (b / c); end
code[a_, b_, c_] := N[(-0.5 / N[(b / c), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-0.5}{\frac{b}{c}}
\end{array}
Initial program 28.3%
/-rgt-identity28.3%
metadata-eval28.3%
associate-/l*28.3%
associate-*r/28.3%
*-commutative28.3%
associate-*l/28.3%
associate-*r/28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
neg-mul-128.3%
distribute-rgt-neg-in28.3%
times-frac28.3%
metadata-eval28.3%
neg-mul-128.3%
Simplified28.4%
Taylor expanded in b around inf 92.1%
fma-def92.1%
associate-*r/92.1%
*-commutative92.1%
associate-*r*92.1%
unpow292.1%
Simplified92.1%
Taylor expanded in c around 0 83.4%
associate-*r/83.4%
associate-/l*83.2%
Simplified83.2%
Final simplification83.2%
(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 28.3%
/-rgt-identity28.3%
metadata-eval28.3%
associate-/l*28.3%
associate-*r/28.3%
*-commutative28.3%
associate-*l/28.3%
associate-*r/28.3%
metadata-eval28.3%
metadata-eval28.3%
times-frac28.3%
neg-mul-128.3%
distribute-rgt-neg-in28.3%
times-frac28.3%
metadata-eval28.3%
neg-mul-128.3%
Simplified28.4%
Taylor expanded in b around inf 83.4%
associate-*r/83.4%
Simplified83.4%
Final simplification83.4%
herbie shell --seed 2023192
(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)))