| Alternative 1 | |
|---|---|
| Error | 14.6% |
| Cost | 14788 |
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
(FPCore (a b c) :precision binary64 (* -2.0 (/ c (+ b (sqrt (fma c (* a -4.0) (* b b)))))))
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 -2.0 * (c / (b + sqrt(fma(c, (a * -4.0), (b * b)))));
}
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(-2.0 * Float64(c / Float64(b + sqrt(fma(c, Float64(a * -4.0), Float64(b * b)))))) 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[(-2.0 * N[(c / N[(b + N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
-2 \cdot \frac{c}{b + \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}}
Initial program 44.46
Simplified44.46
[Start]44.46 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
|---|---|
*-commutative [=>]44.46 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{a \cdot 2}}
\] |
Applied egg-rr43.26
Simplified42.96
[Start]43.26 | \[ \frac{\frac{\frac{b \cdot b - \mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}{\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}{-\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}{a \cdot 2}
\] |
|---|---|
associate-/l/ [=>]43.26 | \[ \frac{\color{blue}{\frac{b \cdot b - \mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}{\left(-\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}\right) \cdot \sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}}{a \cdot 2}
\] |
fma-def [<=]42.98 | \[ \frac{\frac{b \cdot b - \color{blue}{\left(b \cdot b + c \cdot \left(a \cdot -4\right)\right)}}{\left(-\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}\right) \cdot \sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}{a \cdot 2}
\] |
+-commutative [=>]42.98 | \[ \frac{\frac{b \cdot b - \color{blue}{\left(c \cdot \left(a \cdot -4\right) + b \cdot b\right)}}{\left(-\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}\right) \cdot \sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}{a \cdot 2}
\] |
fma-def [=>]42.97 | \[ \frac{\frac{b \cdot b - \color{blue}{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}}{\left(-\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}\right) \cdot \sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}{a \cdot 2}
\] |
distribute-lft-neg-in [<=]42.97 | \[ \frac{\frac{b \cdot b - \mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}{\color{blue}{-\sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}} \cdot \sqrt{b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}}}}}{a \cdot 2}
\] |
rem-square-sqrt [=>]42.96 | \[ \frac{\frac{b \cdot b - \mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}{-\color{blue}{\left(b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -4\right)\right)}\right)}}}{a \cdot 2}
\] |
fma-def [<=]42.96 | \[ \frac{\frac{b \cdot b - \mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}{-\left(b + \sqrt{\color{blue}{b \cdot b + c \cdot \left(a \cdot -4\right)}}\right)}}{a \cdot 2}
\] |
+-commutative [=>]42.96 | \[ \frac{\frac{b \cdot b - \mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}{-\left(b + \sqrt{\color{blue}{c \cdot \left(a \cdot -4\right) + b \cdot b}}\right)}}{a \cdot 2}
\] |
fma-def [=>]42.96 | \[ \frac{\frac{b \cdot b - \mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}{-\left(b + \sqrt{\color{blue}{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}}\right)}}{a \cdot 2}
\] |
Taylor expanded in b around 0 0.71
Applied egg-rr0.81
Simplified0.71
[Start]0.81 | \[ \frac{\frac{-4}{\frac{b + \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}}{a}} \cdot c}{a \cdot 2}
\] |
|---|---|
associate-*l/ [=>]0.71 | \[ \frac{\color{blue}{\frac{-4 \cdot c}{\frac{b + \sqrt{\mathsf{fma}\left(c, a \cdot -4, b \cdot b\right)}}{a}}}}{a \cdot 2}
\] |
Applied egg-rr38.46
Simplified0.44
[Start]38.46 | \[ e^{\mathsf{log1p}\left(\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}} \cdot \left(a \cdot \frac{0.5}{a}\right)\right)} - 1
\] |
|---|---|
expm1-def [=>]16.16 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}} \cdot \left(a \cdot \frac{0.5}{a}\right)\right)\right)}
\] |
expm1-log1p [=>]0.6 | \[ \color{blue}{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}} \cdot \left(a \cdot \frac{0.5}{a}\right)}
\] |
associate-*r* [=>]0.72 | \[ \color{blue}{\left(\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}} \cdot a\right) \cdot \frac{0.5}{a}}
\] |
*-commutative [=>]0.72 | \[ \color{blue}{\left(a \cdot \frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}\right)} \cdot \frac{0.5}{a}
\] |
associate-*r/ [=>]0.54 | \[ \color{blue}{\frac{\left(a \cdot \frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}\right) \cdot 0.5}{a}}
\] |
associate-*l/ [<=]0.54 | \[ \color{blue}{\frac{a \cdot \frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}}{a} \cdot 0.5}
\] |
*-commutative [<=]0.54 | \[ \frac{\color{blue}{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}} \cdot a}}{a} \cdot 0.5
\] |
associate-/l* [=>]0.44 | \[ \color{blue}{\frac{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}}{\frac{a}{a}}} \cdot 0.5
\] |
*-inverses [=>]0.44 | \[ \frac{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}}{\color{blue}{1}} \cdot 0.5
\] |
associate-*l/ [=>]0.44 | \[ \color{blue}{\frac{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}} \cdot 0.5}{1}}
\] |
associate-/l* [=>]0.44 | \[ \color{blue}{\frac{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}}{\frac{1}{0.5}}}
\] |
metadata-eval [=>]0.44 | \[ \frac{\frac{-4 \cdot c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}}{\color{blue}{2}}
\] |
associate-/l/ [=>]0.44 | \[ \color{blue}{\frac{-4 \cdot c}{2 \cdot \left(b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}\right)}}
\] |
times-frac [=>]0.44 | \[ \color{blue}{\frac{-4}{2} \cdot \frac{c}{b + \sqrt{\mathsf{fma}\left(c, -4 \cdot a, b \cdot b\right)}}}
\] |
Final simplification0.44
| Alternative 1 | |
|---|---|
| Error | 14.6% |
| Cost | 14788 |
| Alternative 2 | |
|---|---|
| Error | 0.69% |
| Cost | 7744 |
| Alternative 3 | |
|---|---|
| Error | 15.3% |
| Cost | 7492 |
| Alternative 4 | |
|---|---|
| Error | 17.83% |
| Cost | 1344 |
| Alternative 5 | |
|---|---|
| Error | 17.84% |
| Cost | 1216 |
| Alternative 6 | |
|---|---|
| Error | 18.35% |
| Cost | 1024 |
| Alternative 7 | |
|---|---|
| Error | 35.69% |
| Cost | 256 |
| Alternative 8 | |
|---|---|
| Error | 98.4% |
| Cost | 192 |
herbie shell --seed 2023103
(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)))