| Alternative 1 | |
|---|---|
| Accuracy | 84.7% |
| Cost | 7624 |

(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))
(FPCore (a b c)
:precision binary64
(if (<= b -9.2e+95)
(/ (* b -0.6666666666666666) a)
(if (<= b 1.12e-11)
(/ (- (sqrt (- (* b b) (* (* a 3.0) c))) b) (* a 3.0))
(/ (* c -0.5) b))))double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
double code(double a, double b, double c) {
double tmp;
if (b <= -9.2e+95) {
tmp = (b * -0.6666666666666666) / a;
} else if (b <= 1.12e-11) {
tmp = (sqrt(((b * b) - ((a * 3.0) * c))) - b) / (a * 3.0);
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
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
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: tmp
if (b <= (-9.2d+95)) then
tmp = (b * (-0.6666666666666666d0)) / a
else if (b <= 1.12d-11) then
tmp = (sqrt(((b * b) - ((a * 3.0d0) * c))) - b) / (a * 3.0d0)
else
tmp = (c * (-0.5d0)) / b
end if
code = tmp
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);
}
public static double code(double a, double b, double c) {
double tmp;
if (b <= -9.2e+95) {
tmp = (b * -0.6666666666666666) / a;
} else if (b <= 1.12e-11) {
tmp = (Math.sqrt(((b * b) - ((a * 3.0) * c))) - b) / (a * 3.0);
} else {
tmp = (c * -0.5) / b;
}
return tmp;
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
def code(a, b, c): tmp = 0 if b <= -9.2e+95: tmp = (b * -0.6666666666666666) / a elif b <= 1.12e-11: tmp = (math.sqrt(((b * b) - ((a * 3.0) * c))) - b) / (a * 3.0) else: tmp = (c * -0.5) / b return tmp
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 code(a, b, c) tmp = 0.0 if (b <= -9.2e+95) tmp = Float64(Float64(b * -0.6666666666666666) / a); elseif (b <= 1.12e-11) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 3.0) * c))) - b) / Float64(a * 3.0)); else tmp = Float64(Float64(c * -0.5) / b); end return tmp end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a); end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -9.2e+95) tmp = (b * -0.6666666666666666) / a; elseif (b <= 1.12e-11) tmp = (sqrt(((b * b) - ((a * 3.0) * c))) - b) / (a * 3.0); else tmp = (c * -0.5) / b; end tmp_2 = tmp; 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]
code[a_, b_, c_] := If[LessEqual[b, -9.2e+95], N[(N[(b * -0.6666666666666666), $MachinePrecision] / a), $MachinePrecision], If[LessEqual[b, 1.12e-11], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(a * 3.0), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision], N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a}
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.2 \cdot 10^{+95}:\\
\;\;\;\;\frac{b \cdot -0.6666666666666666}{a}\\
\mathbf{elif}\;b \leq 1.12 \cdot 10^{-11}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 3\right) \cdot c} - b}{a \cdot 3}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot -0.5}{b}\\
\end{array}
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
if b < -9.19999999999999989e95Initial program 45.0%
Simplified45.0%
[Start]45.0% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]45.0% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{\color{blue}{\frac{3 \cdot a}{1}}}
\] |
metadata-eval [<=]45.0% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{\frac{3 \cdot a}{\color{blue}{--1}}}
\] |
associate-/r/ [=>]45.0% | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \cdot \left(--1\right)}
\] |
metadata-eval [=>]45.0% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \cdot \color{blue}{1}
\] |
metadata-eval [<=]45.0% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \cdot \color{blue}{\frac{-1}{-1}}
\] |
times-frac [<=]45.0% | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}\right) \cdot -1}{\left(3 \cdot a\right) \cdot -1}}
\] |
*-commutative [<=]45.0% | \[ \frac{\left(\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}\right) \cdot -1}{\color{blue}{-1 \cdot \left(3 \cdot a\right)}}
\] |
times-frac [=>]45.0% | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{-1} \cdot \frac{-1}{3 \cdot a}}
\] |
associate-/r* [=>]45.0% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{-1} \cdot \color{blue}{\frac{\frac{-1}{3}}{a}}
\] |
Applied egg-rr47.7%
[Start]45.0% | \[ \left(b - \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -3\right)}\right) \cdot \frac{-0.3333333333333333}{a}
\] |
|---|---|
associate-*r/ [=>]45.0% | \[ \color{blue}{\frac{\left(b - \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -3\right)}\right) \cdot -0.3333333333333333}{a}}
\] |
fma-udef [=>]45.0% | \[ \frac{\left(b - \sqrt{\color{blue}{b \cdot b + \left(a \cdot c\right) \cdot -3}}\right) \cdot -0.3333333333333333}{a}
\] |
associate-*r* [<=]45.0% | \[ \frac{\left(b - \sqrt{b \cdot b + \color{blue}{a \cdot \left(c \cdot -3\right)}}\right) \cdot -0.3333333333333333}{a}
\] |
add-sqr-sqrt [=>]25.4% | \[ \frac{\left(b - \sqrt{b \cdot b + \color{blue}{\sqrt{a \cdot \left(c \cdot -3\right)} \cdot \sqrt{a \cdot \left(c \cdot -3\right)}}}\right) \cdot -0.3333333333333333}{a}
\] |
hypot-def [=>]47.7% | \[ \frac{\left(b - \color{blue}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -3\right)}\right)}\right) \cdot -0.3333333333333333}{a}
\] |
Taylor expanded in b around -inf 93.9%
Simplified93.9%
[Start]93.9% | \[ \frac{-0.6666666666666666 \cdot b}{a}
\] |
|---|---|
*-commutative [=>]93.9% | \[ \frac{\color{blue}{b \cdot -0.6666666666666666}}{a}
\] |
if -9.19999999999999989e95 < b < 1.1200000000000001e-11Initial program 78.3%
if 1.1200000000000001e-11 < b Initial program 16.9%
Simplified16.9%
[Start]16.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]16.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{\color{blue}{\frac{3 \cdot a}{1}}}
\] |
metadata-eval [<=]16.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{\frac{3 \cdot a}{\color{blue}{--1}}}
\] |
associate-/r/ [=>]16.9% | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \cdot \left(--1\right)}
\] |
metadata-eval [=>]16.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \cdot \color{blue}{1}
\] |
metadata-eval [<=]16.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \cdot \color{blue}{\frac{-1}{-1}}
\] |
times-frac [<=]16.9% | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}\right) \cdot -1}{\left(3 \cdot a\right) \cdot -1}}
\] |
*-commutative [<=]16.9% | \[ \frac{\left(\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}\right) \cdot -1}{\color{blue}{-1 \cdot \left(3 \cdot a\right)}}
\] |
times-frac [=>]16.9% | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{-1} \cdot \frac{-1}{3 \cdot a}}
\] |
associate-/r* [=>]16.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{-1} \cdot \color{blue}{\frac{\frac{-1}{3}}{a}}
\] |
Applied egg-rr33.1%
[Start]16.9% | \[ \left(b - \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -3\right)}\right) \cdot \frac{-0.3333333333333333}{a}
\] |
|---|---|
associate-*r/ [=>]16.9% | \[ \color{blue}{\frac{\left(b - \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -3\right)}\right) \cdot -0.3333333333333333}{a}}
\] |
fma-udef [=>]16.9% | \[ \frac{\left(b - \sqrt{\color{blue}{b \cdot b + \left(a \cdot c\right) \cdot -3}}\right) \cdot -0.3333333333333333}{a}
\] |
associate-*r* [<=]16.9% | \[ \frac{\left(b - \sqrt{b \cdot b + \color{blue}{a \cdot \left(c \cdot -3\right)}}\right) \cdot -0.3333333333333333}{a}
\] |
add-sqr-sqrt [=>]11.4% | \[ \frac{\left(b - \sqrt{b \cdot b + \color{blue}{\sqrt{a \cdot \left(c \cdot -3\right)} \cdot \sqrt{a \cdot \left(c \cdot -3\right)}}}\right) \cdot -0.3333333333333333}{a}
\] |
hypot-def [=>]33.1% | \[ \frac{\left(b - \color{blue}{\mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -3\right)}\right)}\right) \cdot -0.3333333333333333}{a}
\] |
Taylor expanded in b around inf 0.0%
Simplified91.9%
[Start]0.0% | \[ 0.16666666666666666 \cdot \frac{c \cdot {\left(\sqrt{-3}\right)}^{2}}{b}
\] |
|---|---|
associate-*r/ [=>]0.0% | \[ \color{blue}{\frac{0.16666666666666666 \cdot \left(c \cdot {\left(\sqrt{-3}\right)}^{2}\right)}{b}}
\] |
*-commutative [=>]0.0% | \[ \frac{0.16666666666666666 \cdot \color{blue}{\left({\left(\sqrt{-3}\right)}^{2} \cdot c\right)}}{b}
\] |
associate-*r* [=>]0.0% | \[ \frac{\color{blue}{\left(0.16666666666666666 \cdot {\left(\sqrt{-3}\right)}^{2}\right) \cdot c}}{b}
\] |
unpow2 [=>]0.0% | \[ \frac{\left(0.16666666666666666 \cdot \color{blue}{\left(\sqrt{-3} \cdot \sqrt{-3}\right)}\right) \cdot c}{b}
\] |
rem-square-sqrt [=>]91.9% | \[ \frac{\left(0.16666666666666666 \cdot \color{blue}{-3}\right) \cdot c}{b}
\] |
metadata-eval [=>]91.9% | \[ \frac{\color{blue}{-0.5} \cdot c}{b}
\] |
*-commutative [=>]91.9% | \[ \frac{\color{blue}{c \cdot -0.5}}{b}
\] |
Final simplification85.8%
| Alternative 1 | |
|---|---|
| Accuracy | 84.7% |
| Cost | 7624 |
| Alternative 2 | |
|---|---|
| Accuracy | 84.7% |
| Cost | 7624 |
| Alternative 3 | |
|---|---|
| Accuracy | 79.8% |
| Cost | 7368 |
| Alternative 4 | |
|---|---|
| Accuracy | 79.8% |
| Cost | 7368 |
| Alternative 5 | |
|---|---|
| Accuracy | 68.1% |
| Cost | 452 |
| Alternative 6 | |
|---|---|
| Accuracy | 68.1% |
| Cost | 452 |
| Alternative 7 | |
|---|---|
| Accuracy | 68.1% |
| Cost | 452 |
| Alternative 8 | |
|---|---|
| Accuracy | 34.7% |
| Cost | 320 |
herbie shell --seed 2023167
(FPCore (a b c)
:name "Cubic critical"
:precision binary64
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))