| Alternative 1 | |
|---|---|
| Accuracy | 99.3% |
| Cost | 8128 |

(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
(FPCore (a b c) :precision binary64 (/ (/ (* c (* a -4.0)) (+ b (sqrt (+ (* -8.0 (* c a)) (+ (* b b) (* (* c a) 4.0)))))) (* a 2.0)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
double code(double a, double b, double c) {
return ((c * (a * -4.0)) / (b + sqrt(((-8.0 * (c * a)) + ((b * b) + ((c * a) * 4.0)))))) / (a * 2.0);
}
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) - ((4.0d0 * a) * c)))) / (2.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
code = ((c * (a * (-4.0d0))) / (b + sqrt((((-8.0d0) * (c * a)) + ((b * b) + ((c * a) * 4.0d0)))))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
public static double code(double a, double b, double c) {
return ((c * (a * -4.0)) / (b + Math.sqrt(((-8.0 * (c * a)) + ((b * b) + ((c * a) * 4.0)))))) / (a * 2.0);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
def code(a, b, c): return ((c * (a * -4.0)) / (b + math.sqrt(((-8.0 * (c * a)) + ((b * b) + ((c * a) * 4.0)))))) / (a * 2.0)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * -4.0)) / Float64(b + sqrt(Float64(Float64(-8.0 * Float64(c * a)) + Float64(Float64(b * b) + Float64(Float64(c * a) * 4.0)))))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
function tmp = code(a, b, c) tmp = ((c * (a * -4.0)) / (b + sqrt(((-8.0 * (c * a)) + ((b * b) + ((c * a) * 4.0)))))) / (a * 2.0); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := N[(N[(N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision] / N[(b + N[Sqrt[N[(N[(-8.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] + N[(N[(c * a), $MachinePrecision] * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\frac{\frac{c \cdot \left(a \cdot -4\right)}{b + \sqrt{-8 \cdot \left(c \cdot a\right) + \left(b \cdot b + \left(c \cdot a\right) \cdot 4\right)}}}{a \cdot 2}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
Initial program 54.9%
Simplified55.0%
[Start]54.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
*-commutative [=>]54.9% | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{a \cdot 2}}
\] |
+-commutative [=>]54.9% | \[ \frac{\color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + \left(-b\right)}}{a \cdot 2}
\] |
unsub-neg [=>]54.9% | \[ \frac{\color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}}{a \cdot 2}
\] |
fma-neg [=>]55.0% | \[ \frac{\sqrt{\color{blue}{\mathsf{fma}\left(b, b, -\left(4 \cdot a\right) \cdot c\right)}} - b}{a \cdot 2}
\] |
associate-*l* [=>]55.0% | \[ \frac{\sqrt{\mathsf{fma}\left(b, b, -\color{blue}{4 \cdot \left(a \cdot c\right)}\right)} - b}{a \cdot 2}
\] |
*-commutative [=>]55.0% | \[ \frac{\sqrt{\mathsf{fma}\left(b, b, -\color{blue}{\left(a \cdot c\right) \cdot 4}\right)} - b}{a \cdot 2}
\] |
distribute-rgt-neg-in [=>]55.0% | \[ \frac{\sqrt{\mathsf{fma}\left(b, b, \color{blue}{\left(a \cdot c\right) \cdot \left(-4\right)}\right)} - b}{a \cdot 2}
\] |
metadata-eval [=>]55.0% | \[ \frac{\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot \color{blue}{-4}\right)} - b}{a \cdot 2}
\] |
Applied egg-rr54.9%
[Start]55.0% | \[ \frac{\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot -4\right)} - b}{a \cdot 2}
\] |
|---|---|
fma-udef [=>]54.9% | \[ \frac{\sqrt{\color{blue}{b \cdot b + \left(a \cdot c\right) \cdot -4}} - b}{a \cdot 2}
\] |
*-commutative [=>]54.9% | \[ \frac{\sqrt{b \cdot b + \color{blue}{-4 \cdot \left(a \cdot c\right)}} - b}{a \cdot 2}
\] |
metadata-eval [<=]54.9% | \[ \frac{\sqrt{b \cdot b + \color{blue}{\left(-4\right)} \cdot \left(a \cdot c\right)} - b}{a \cdot 2}
\] |
cancel-sign-sub-inv [<=]54.9% | \[ \frac{\sqrt{\color{blue}{b \cdot b - 4 \cdot \left(a \cdot c\right)}} - b}{a \cdot 2}
\] |
associate-*l* [<=]54.9% | \[ \frac{\sqrt{b \cdot b - \color{blue}{\left(4 \cdot a\right) \cdot c}} - b}{a \cdot 2}
\] |
*-un-lft-identity [=>]54.9% | \[ \frac{\sqrt{b \cdot b - \color{blue}{1 \cdot \left(\left(4 \cdot a\right) \cdot c\right)}} - b}{a \cdot 2}
\] |
prod-diff [=>]55.0% | \[ \frac{\sqrt{\color{blue}{\mathsf{fma}\left(b, b, -\left(\left(4 \cdot a\right) \cdot c\right) \cdot 1\right) + \mathsf{fma}\left(-\left(4 \cdot a\right) \cdot c, 1, \left(\left(4 \cdot a\right) \cdot c\right) \cdot 1\right)}} - b}{a \cdot 2}
\] |
Simplified54.8%
[Start]54.9% | \[ \frac{\sqrt{\mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right) + \mathsf{fma}\left(a \cdot \left(c \cdot -4\right), 1, \left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
|---|---|
+-commutative [=>]54.9% | \[ \frac{\sqrt{\color{blue}{\mathsf{fma}\left(a \cdot \left(c \cdot -4\right), 1, \left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right) + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)}} - b}{a \cdot 2}
\] |
fma-udef [=>]54.9% | \[ \frac{\sqrt{\color{blue}{\left(\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1 + \left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
*-rgt-identity [=>]54.9% | \[ \frac{\sqrt{\left(\color{blue}{a \cdot \left(c \cdot -4\right)} + \left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right) + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
*-rgt-identity [=>]54.9% | \[ \frac{\sqrt{\left(a \cdot \left(c \cdot -4\right) + \color{blue}{a \cdot \left(c \cdot -4\right)}\right) + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
count-2 [=>]54.9% | \[ \frac{\sqrt{\color{blue}{2 \cdot \left(a \cdot \left(c \cdot -4\right)\right)} + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
*-commutative [=>]54.9% | \[ \frac{\sqrt{2 \cdot \color{blue}{\left(\left(c \cdot -4\right) \cdot a\right)} + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
*-commutative [=>]54.9% | \[ \frac{\sqrt{2 \cdot \left(\color{blue}{\left(-4 \cdot c\right)} \cdot a\right) + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
associate-*r* [<=]54.9% | \[ \frac{\sqrt{2 \cdot \color{blue}{\left(-4 \cdot \left(c \cdot a\right)\right)} + \mathsf{fma}\left(b, b, -\left(a \cdot \left(c \cdot -4\right)\right) \cdot 1\right)} - b}{a \cdot 2}
\] |
*-rgt-identity [=>]54.9% | \[ \frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \mathsf{fma}\left(b, b, -\color{blue}{a \cdot \left(c \cdot -4\right)}\right)} - b}{a \cdot 2}
\] |
fma-neg [<=]54.8% | \[ \frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \color{blue}{\left(b \cdot b - a \cdot \left(c \cdot -4\right)\right)}} - b}{a \cdot 2}
\] |
*-commutative [=>]54.8% | \[ \frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - \color{blue}{\left(c \cdot -4\right) \cdot a}\right)} - b}{a \cdot 2}
\] |
*-commutative [=>]54.8% | \[ \frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - \color{blue}{\left(-4 \cdot c\right)} \cdot a\right)} - b}{a \cdot 2}
\] |
associate-*r* [<=]54.8% | \[ \frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - \color{blue}{-4 \cdot \left(c \cdot a\right)}\right)} - b}{a \cdot 2}
\] |
Applied egg-rr56.2%
[Start]54.8% | \[ \frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} - b}{a \cdot 2}
\] |
|---|---|
flip-- [=>]55.1% | \[ \frac{\color{blue}{\frac{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} \cdot \sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}}{a \cdot 2}
\] |
add-sqr-sqrt [<=]56.2% | \[ \frac{\frac{\color{blue}{\left(2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)\right)} - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
associate-*r* [=>]56.2% | \[ \frac{\frac{\left(\color{blue}{\left(2 \cdot -4\right) \cdot \left(c \cdot a\right)} + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)\right) - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
metadata-eval [=>]56.2% | \[ \frac{\frac{\left(\color{blue}{-8} \cdot \left(c \cdot a\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)\right) - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
*-commutative [=>]56.2% | \[ \frac{\frac{\left(-8 \cdot \color{blue}{\left(a \cdot c\right)} + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)\right) - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
cancel-sign-sub-inv [=>]56.2% | \[ \frac{\frac{\left(-8 \cdot \left(a \cdot c\right) + \color{blue}{\left(b \cdot b + \left(--4\right) \cdot \left(c \cdot a\right)\right)}\right) - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
metadata-eval [=>]56.2% | \[ \frac{\frac{\left(-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + \color{blue}{4} \cdot \left(c \cdot a\right)\right)\right) - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
*-commutative [=>]56.2% | \[ \frac{\frac{\left(-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \color{blue}{\left(a \cdot c\right)}\right)\right) - b \cdot b}{\sqrt{2 \cdot \left(-4 \cdot \left(c \cdot a\right)\right) + \left(b \cdot b - -4 \cdot \left(c \cdot a\right)\right)} + b}}{a \cdot 2}
\] |
Taylor expanded in a around 0 99.4%
Simplified99.4%
[Start]99.4% | \[ \frac{\frac{a \cdot \left(-8 \cdot c + 4 \cdot c\right)}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
|---|---|
distribute-rgt-in [=>]99.4% | \[ \frac{\frac{\color{blue}{\left(-8 \cdot c\right) \cdot a + \left(4 \cdot c\right) \cdot a}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
associate-*r* [<=]99.4% | \[ \frac{\frac{\color{blue}{-8 \cdot \left(c \cdot a\right)} + \left(4 \cdot c\right) \cdot a}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
associate-*r* [<=]99.4% | \[ \frac{\frac{-8 \cdot \left(c \cdot a\right) + \color{blue}{4 \cdot \left(c \cdot a\right)}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
+-commutative [=>]99.4% | \[ \frac{\frac{\color{blue}{4 \cdot \left(c \cdot a\right) + -8 \cdot \left(c \cdot a\right)}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
*-commutative [<=]99.4% | \[ \frac{\frac{\color{blue}{\left(c \cdot a\right) \cdot 4} + -8 \cdot \left(c \cdot a\right)}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
associate-*l* [=>]99.4% | \[ \frac{\frac{\color{blue}{c \cdot \left(a \cdot 4\right)} + -8 \cdot \left(c \cdot a\right)}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
*-commutative [<=]99.4% | \[ \frac{\frac{c \cdot \color{blue}{\left(4 \cdot a\right)} + -8 \cdot \left(c \cdot a\right)}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
*-commutative [=>]99.4% | \[ \frac{\frac{\color{blue}{\left(4 \cdot a\right) \cdot c} + -8 \cdot \left(c \cdot a\right)}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
*-commutative [=>]99.4% | \[ \frac{\frac{\left(4 \cdot a\right) \cdot c + -8 \cdot \color{blue}{\left(a \cdot c\right)}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
associate-*l* [<=]99.4% | \[ \frac{\frac{\left(4 \cdot a\right) \cdot c + \color{blue}{\left(-8 \cdot a\right) \cdot c}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
distribute-rgt-in [<=]99.4% | \[ \frac{\frac{\color{blue}{c \cdot \left(4 \cdot a + -8 \cdot a\right)}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
distribute-rgt-out [=>]99.4% | \[ \frac{\frac{c \cdot \color{blue}{\left(a \cdot \left(4 + -8\right)\right)}}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
metadata-eval [=>]99.4% | \[ \frac{\frac{c \cdot \left(a \cdot \color{blue}{-4}\right)}{\sqrt{-8 \cdot \left(a \cdot c\right) + \left(b \cdot b + 4 \cdot \left(a \cdot c\right)\right)} + b}}{a \cdot 2}
\] |
Final simplification99.4%
| Alternative 1 | |
|---|---|
| Accuracy | 99.3% |
| Cost | 8128 |
| Alternative 2 | |
|---|---|
| Accuracy | 85.4% |
| Cost | 14788 |
| Alternative 3 | |
|---|---|
| Accuracy | 85.3% |
| Cost | 7492 |
| Alternative 4 | |
|---|---|
| Accuracy | 81.9% |
| Cost | 1344 |
| Alternative 5 | |
|---|---|
| Accuracy | 64.3% |
| Cost | 256 |
herbie shell --seed 2023165
(FPCore (a b c)
:name "Quadratic roots, narrow range"
:precision binary64
:pre (and (and (and (< 1.0536712127723509e-8 a) (< a 94906265.62425156)) (and (< 1.0536712127723509e-8 b) (< b 94906265.62425156))) (and (< 1.0536712127723509e-8 c) (< c 94906265.62425156)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))