| Alternative 1 | |
|---|---|
| Error | 10.5 |
| Cost | 7624 |
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
(FPCore (a b c)
:precision binary64
(let* ((t_0 (* a (* c -4.0))))
(if (<= b -4.6e+103)
(/ (fma 2.0 (/ c (/ b a)) (* b -2.0)) (* 2.0 a))
(if (<= b -1.4e-292)
(* (/ (- b (sqrt (+ (* b b) t_0))) a) -0.5)
(if (<= b 2.1e-10)
(* c (/ -2.0 (+ b (hypot b (sqrt t_0)))))
(/ (- c) 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) {
double t_0 = a * (c * -4.0);
double tmp;
if (b <= -4.6e+103) {
tmp = fma(2.0, (c / (b / a)), (b * -2.0)) / (2.0 * a);
} else if (b <= -1.4e-292) {
tmp = ((b - sqrt(((b * b) + t_0))) / a) * -0.5;
} else if (b <= 2.1e-10) {
tmp = c * (-2.0 / (b + hypot(b, sqrt(t_0))));
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * a)) end
function code(a, b, c) t_0 = Float64(a * Float64(c * -4.0)) tmp = 0.0 if (b <= -4.6e+103) tmp = Float64(fma(2.0, Float64(c / Float64(b / a)), Float64(b * -2.0)) / Float64(2.0 * a)); elseif (b <= -1.4e-292) tmp = Float64(Float64(Float64(b - sqrt(Float64(Float64(b * b) + t_0))) / a) * -0.5); elseif (b <= 2.1e-10) tmp = Float64(c * Float64(-2.0 / Float64(b + hypot(b, sqrt(t_0))))); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := Block[{t$95$0 = N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, -4.6e+103], N[(N[(2.0 * N[(c / N[(b / a), $MachinePrecision]), $MachinePrecision] + N[(b * -2.0), $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, -1.4e-292], N[(N[(N[(b - N[Sqrt[N[(N[(b * b), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision] * -0.5), $MachinePrecision], If[LessEqual[b, 2.1e-10], N[(c * N[(-2.0 / N[(b + N[Sqrt[b ^ 2 + N[Sqrt[t$95$0], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
t_0 := a \cdot \left(c \cdot -4\right)\\
\mathbf{if}\;b \leq -4.6 \cdot 10^{+103}:\\
\;\;\;\;\frac{\mathsf{fma}\left(2, \frac{c}{\frac{b}{a}}, b \cdot -2\right)}{2 \cdot a}\\
\mathbf{elif}\;b \leq -1.4 \cdot 10^{-292}:\\
\;\;\;\;\frac{b - \sqrt{b \cdot b + t_0}}{a} \cdot -0.5\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{-10}:\\
\;\;\;\;c \cdot \frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{t_0}\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
| Original | 33.8 |
|---|---|
| Target | 21.5 |
| Herbie | 8.3 |
if b < -4.60000000000000017e103Initial program 49.2
Taylor expanded in b around -inf 11.0
Simplified3.4
[Start]11.0 | \[ \frac{2 \cdot \frac{c \cdot a}{b} + -2 \cdot b}{2 \cdot a}
\] |
|---|---|
fma-def [=>]11.0 | \[ \frac{\color{blue}{\mathsf{fma}\left(2, \frac{c \cdot a}{b}, -2 \cdot b\right)}}{2 \cdot a}
\] |
associate-/l* [=>]3.4 | \[ \frac{\mathsf{fma}\left(2, \color{blue}{\frac{c}{\frac{b}{a}}}, -2 \cdot b\right)}{2 \cdot a}
\] |
*-commutative [=>]3.4 | \[ \frac{\mathsf{fma}\left(2, \frac{c}{\frac{b}{a}}, \color{blue}{b \cdot -2}\right)}{2 \cdot a}
\] |
if -4.60000000000000017e103 < b < -1.4000000000000001e-292Initial program 9.4
Simplified9.4
[Start]9.4 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
associate-/r* [=>]9.4 | \[ \color{blue}{\frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{a}}
\] |
/-rgt-identity [<=]9.4 | \[ \frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{\color{blue}{\frac{a}{1}}}
\] |
metadata-eval [<=]9.4 | \[ \frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{\frac{a}{\color{blue}{-1 \cdot -1}}}
\] |
associate-/l/ [<=]9.4 | \[ \frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2}}{\color{blue}{\frac{\frac{a}{-1}}{-1}}}
\] |
associate-/l* [<=]9.4 | \[ \color{blue}{\frac{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2} \cdot -1}{\frac{a}{-1}}}
\] |
associate-*r/ [<=]9.5 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2} \cdot \frac{-1}{\frac{a}{-1}}}
\] |
times-frac [<=]9.4 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot -1}{2 \cdot \frac{a}{-1}}}
\] |
*-commutative [=>]9.4 | \[ \frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot -1}{\color{blue}{\frac{a}{-1} \cdot 2}}
\] |
times-frac [=>]9.4 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{a}{-1}} \cdot \frac{-1}{2}}
\] |
Applied egg-rr9.4
if -1.4000000000000001e-292 < b < 2.1e-10Initial program 23.7
Simplified23.8
[Start]23.7 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\] |
|---|---|
/-rgt-identity [<=]23.7 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{\frac{2 \cdot a}{1}}}
\] |
metadata-eval [<=]23.7 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{2 \cdot a}{\color{blue}{--1}}}
\] |
*-commutative [=>]23.7 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\frac{\color{blue}{a \cdot 2}}{--1}}
\] |
associate-/l* [=>]23.7 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{\frac{a}{\frac{--1}{2}}}}
\] |
associate-/l* [<=]23.7 | \[ \color{blue}{\frac{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{--1}{2}}{a}}
\] |
associate-*r/ [<=]23.8 | \[ \color{blue}{\left(\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\right) \cdot \frac{\frac{--1}{2}}{a}}
\] |
/-rgt-identity [<=]23.8 | \[ \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
metadata-eval [<=]23.8 | \[ \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{\color{blue}{--1}} \cdot \frac{\frac{--1}{2}}{a}
\] |
Applied egg-rr24.2
Simplified29.4
[Start]24.2 | \[ \frac{\left(-\left(\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -4\right)\right) - b \cdot b\right)\right) \cdot \frac{0.5}{a}}{-\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right)}
\] |
|---|---|
associate-/l* [=>]29.4 | \[ \color{blue}{\frac{-\left(\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -4\right)\right) - b \cdot b\right)}{\frac{-\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right)}{\frac{0.5}{a}}}}
\] |
fma-def [<=]29.4 | \[ \frac{-\left(\color{blue}{\left(b \cdot b + a \cdot \left(c \cdot -4\right)\right)} - b \cdot b\right)}{\frac{-\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right)}{\frac{0.5}{a}}}
\] |
+-commutative [=>]29.4 | \[ \frac{-\left(\color{blue}{\left(a \cdot \left(c \cdot -4\right) + b \cdot b\right)} - b \cdot b\right)}{\frac{-\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right)}{\frac{0.5}{a}}}
\] |
fma-def [=>]29.4 | \[ \frac{-\left(\color{blue}{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)} - b \cdot b\right)}{\frac{-\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right)}{\frac{0.5}{a}}}
\] |
Applied egg-rr24.1
Simplified20.7
[Start]24.1 | \[ \frac{1}{b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a \cdot 2}
\] |
|---|---|
associate-/r* [=>]24.1 | \[ \frac{1}{b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)} \cdot \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}}{2}}
\] |
associate-*r/ [=>]24.1 | \[ \color{blue}{\frac{\frac{1}{b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}}{2}}
\] |
associate-*l/ [<=]24.1 | \[ \color{blue}{\frac{\frac{1}{b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)}}{2} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}}
\] |
associate-/r* [<=]24.1 | \[ \color{blue}{\frac{1}{\left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right) \cdot 2}} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
*-commutative [=>]24.1 | \[ \frac{1}{\color{blue}{2 \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)\right)}} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
associate-/r* [=>]24.1 | \[ \color{blue}{\frac{\frac{1}{2}}{b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)}} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
metadata-eval [=>]24.1 | \[ \frac{\color{blue}{0.5}}{b + \mathsf{hypot}\left(b, \sqrt{a \cdot \left(c \cdot -4\right)}\right)} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
associate-*r* [=>]24.2 | \[ \frac{0.5}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(a \cdot c\right) \cdot -4}}\right)} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
*-commutative [<=]24.2 | \[ \frac{0.5}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(c \cdot a\right)} \cdot -4}\right)} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
*-commutative [<=]24.2 | \[ \frac{0.5}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{-4 \cdot \left(c \cdot a\right)}}\right)} \cdot \frac{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right) - b \cdot b}{a}
\] |
Applied egg-rr53.2
Simplified13.0
[Start]53.2 | \[ e^{\mathsf{log1p}\left(\frac{-2}{a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)\right)} \cdot \left(a \cdot c\right)\right)} - 1
\] |
|---|---|
expm1-def [=>]36.8 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{-2}{a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)\right)} \cdot \left(a \cdot c\right)\right)\right)}
\] |
expm1-log1p [=>]26.6 | \[ \color{blue}{\frac{-2}{a \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)\right)} \cdot \left(a \cdot c\right)}
\] |
*-commutative [=>]26.6 | \[ \frac{-2}{\color{blue}{\left(b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)\right) \cdot a}} \cdot \left(a \cdot c\right)
\] |
associate-/r* [=>]26.4 | \[ \color{blue}{\frac{\frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}{a}} \cdot \left(a \cdot c\right)
\] |
associate-/r/ [<=]20.7 | \[ \color{blue}{\frac{\frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}{\frac{a}{a \cdot c}}}
\] |
associate-/r* [=>]13.1 | \[ \frac{\frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}{\color{blue}{\frac{\frac{a}{a}}{c}}}
\] |
associate-/r/ [=>]13.0 | \[ \color{blue}{\frac{\frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}{\frac{a}{a}} \cdot c}
\] |
*-commutative [=>]13.0 | \[ \color{blue}{c \cdot \frac{\frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}{\frac{a}{a}}}
\] |
*-inverses [=>]13.0 | \[ c \cdot \frac{\frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}{\color{blue}{1}}
\] |
associate-/l/ [=>]13.0 | \[ c \cdot \color{blue}{\frac{-2}{1 \cdot \left(b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)\right)}}
\] |
associate-/r* [=>]13.0 | \[ c \cdot \color{blue}{\frac{\frac{-2}{1}}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}}
\] |
metadata-eval [=>]13.0 | \[ c \cdot \frac{\color{blue}{-2}}{b + \mathsf{hypot}\left(b, \sqrt{c \cdot \left(a \cdot -4\right)}\right)}
\] |
associate-*r* [=>]13.0 | \[ c \cdot \frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(c \cdot a\right) \cdot -4}}\right)}
\] |
*-commutative [=>]13.0 | \[ c \cdot \frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{\left(a \cdot c\right)} \cdot -4}\right)}
\] |
associate-*l* [=>]13.0 | \[ c \cdot \frac{-2}{b + \mathsf{hypot}\left(b, \sqrt{\color{blue}{a \cdot \left(c \cdot -4\right)}}\right)}
\] |
if 2.1e-10 < b Initial program 55.3
Taylor expanded in b around inf 6.6
Simplified6.6
[Start]6.6 | \[ -1 \cdot \frac{c}{b}
\] |
|---|---|
associate-*r/ [=>]6.6 | \[ \color{blue}{\frac{-1 \cdot c}{b}}
\] |
neg-mul-1 [<=]6.6 | \[ \frac{\color{blue}{-c}}{b}
\] |
Final simplification8.3
| Alternative 1 | |
|---|---|
| Error | 10.5 |
| Cost | 7624 |
| Alternative 2 | |
|---|---|
| Error | 14.2 |
| Cost | 7368 |
| Alternative 3 | |
|---|---|
| Error | 14.1 |
| Cost | 7368 |
| Alternative 4 | |
|---|---|
| Error | 23.5 |
| Cost | 388 |
| Alternative 5 | |
|---|---|
| Error | 45.5 |
| Cost | 256 |
herbie shell --seed 2023053
(FPCore (a b c)
:name "quadp (p42, positive)"
:precision binary64
:herbie-target
(if (< b 0.0) (/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)) (/ c (* a (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))))
(/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))