Alternative 1 | |
---|---|
Error | 10.2 |
Cost | 7368 |
(FPCore (b a c) :precision binary64 (+ (- b) (/ (sqrt (- (* b b) (* (* 4.0 a) c))) (* 2.0 a))))
(FPCore (b a c) :precision binary64 (if (<= b -4.8e+50) (- (* (/ b a) -0.5) b) (if (<= b 1.04e+111) (- (/ (sqrt (+ (* b b) (* c (* a -4.0)))) (* a 2.0)) b) (- (* b (/ 0.5 a)) b))))
double code(double b, double a, double c) { return -b + (sqrt(((b * b) - ((4.0 * a) * c))) / (2.0 * a)); }
double code(double b, double a, double c) { double tmp; if (b <= -4.8e+50) { tmp = ((b / a) * -0.5) - b; } else if (b <= 1.04e+111) { tmp = (sqrt(((b * b) + (c * (a * -4.0)))) / (a * 2.0)) - b; } else { tmp = (b * (0.5 / a)) - b; } return tmp; }
real(8) function code(b, a, c) real(8), intent (in) :: b real(8), intent (in) :: a real(8), intent (in) :: c code = -b + (sqrt(((b * b) - ((4.0d0 * a) * c))) / (2.0d0 * a)) end function
real(8) function code(b, a, c) real(8), intent (in) :: b real(8), intent (in) :: a real(8), intent (in) :: c real(8) :: tmp if (b <= (-4.8d+50)) then tmp = ((b / a) * (-0.5d0)) - b else if (b <= 1.04d+111) then tmp = (sqrt(((b * b) + (c * (a * (-4.0d0))))) / (a * 2.0d0)) - b else tmp = (b * (0.5d0 / a)) - b end if code = tmp end function
public static double code(double b, double a, double c) { return -b + (Math.sqrt(((b * b) - ((4.0 * a) * c))) / (2.0 * a)); }
public static double code(double b, double a, double c) { double tmp; if (b <= -4.8e+50) { tmp = ((b / a) * -0.5) - b; } else if (b <= 1.04e+111) { tmp = (Math.sqrt(((b * b) + (c * (a * -4.0)))) / (a * 2.0)) - b; } else { tmp = (b * (0.5 / a)) - b; } return tmp; }
def code(b, a, c): return -b + (math.sqrt(((b * b) - ((4.0 * a) * c))) / (2.0 * a))
def code(b, a, c): tmp = 0 if b <= -4.8e+50: tmp = ((b / a) * -0.5) - b elif b <= 1.04e+111: tmp = (math.sqrt(((b * b) + (c * (a * -4.0)))) / (a * 2.0)) - b else: tmp = (b * (0.5 / a)) - b return tmp
function code(b, a, c) return Float64(Float64(-b) + Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c))) / Float64(2.0 * a))) end
function code(b, a, c) tmp = 0.0 if (b <= -4.8e+50) tmp = Float64(Float64(Float64(b / a) * -0.5) - b); elseif (b <= 1.04e+111) tmp = Float64(Float64(sqrt(Float64(Float64(b * b) + Float64(c * Float64(a * -4.0)))) / Float64(a * 2.0)) - b); else tmp = Float64(Float64(b * Float64(0.5 / a)) - b); end return tmp end
function tmp = code(b, a, c) tmp = -b + (sqrt(((b * b) - ((4.0 * a) * c))) / (2.0 * a)); end
function tmp_2 = code(b, a, c) tmp = 0.0; if (b <= -4.8e+50) tmp = ((b / a) * -0.5) - b; elseif (b <= 1.04e+111) tmp = (sqrt(((b * b) + (c * (a * -4.0)))) / (a * 2.0)) - b; else tmp = (b * (0.5 / a)) - b; end tmp_2 = tmp; end
code[b_, a_, c_] := N[((-b) + N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[b_, a_, c_] := If[LessEqual[b, -4.8e+50], N[(N[(N[(b / a), $MachinePrecision] * -0.5), $MachinePrecision] - b), $MachinePrecision], If[LessEqual[b, 1.04e+111], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] + N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision], N[(N[(b * N[(0.5 / a), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision]]]
\left(-b\right) + \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l} \mathbf{if}\;b \leq -4.8 \cdot 10^{+50}:\\ \;\;\;\;\frac{b}{a} \cdot -0.5 - b\\ \mathbf{elif}\;b \leq 1.04 \cdot 10^{+111}:\\ \;\;\;\;\frac{\sqrt{b \cdot b + c \cdot \left(a \cdot -4\right)}}{a \cdot 2} - b\\ \mathbf{else}:\\ \;\;\;\;b \cdot \frac{0.5}{a} - b\\ \end{array}
Results
if b < -4.8000000000000004e50
Initial program 37.9
Simplified37.9
[Start]37.9 | \[ \left(-b\right) + \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
---|---|
*-commutative [=>]37.9 | \[ \left(-b\right) + \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{a \cdot 2}}
\] |
Taylor expanded in b around -inf 0.9
Simplified0.9
[Start]0.9 | \[ \left(-b\right) + -0.5 \cdot \frac{b}{a}
\] |
---|---|
*-commutative [=>]0.9 | \[ \left(-b\right) + \color{blue}{\frac{b}{a} \cdot -0.5}
\] |
if -4.8000000000000004e50 < b < 1.04e111
Initial program 9.3
if 1.04e111 < b
Initial program 50.0
Simplified50.1
[Start]50.0 | \[ \left(-b\right) + \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\] |
---|---|
remove-double-neg [<=]50.0 | \[ \left(-b\right) + \color{blue}{\left(-\left(-\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)\right)}
\] |
+-commutative [=>]50.0 | \[ \color{blue}{\left(-\left(-\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)\right) + \left(-b\right)}
\] |
neg-mul-1 [=>]50.0 | \[ \color{blue}{-1 \cdot \left(-\frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)} + \left(-b\right)
\] |
distribute-neg-frac [=>]50.0 | \[ -1 \cdot \color{blue}{\frac{-\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}} + \left(-b\right)
\] |
associate-*r/ [=>]50.0 | \[ \color{blue}{\frac{-1 \cdot \left(-\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)}{2 \cdot a}} + \left(-b\right)
\] |
associate-*l/ [<=]50.1 | \[ \color{blue}{\frac{-1}{2 \cdot a} \cdot \left(-\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)} + \left(-b\right)
\] |
neg-mul-1 [=>]50.1 | \[ \frac{-1}{2 \cdot a} \cdot \color{blue}{\left(-1 \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}\right)} + \left(-b\right)
\] |
associate-*r* [=>]50.1 | \[ \color{blue}{\left(\frac{-1}{2 \cdot a} \cdot -1\right) \cdot \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}} + \left(-b\right)
\] |
*-commutative [<=]50.1 | \[ \color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} \cdot \left(\frac{-1}{2 \cdot a} \cdot -1\right)} + \left(-b\right)
\] |
fma-def [=>]50.1 | \[ \color{blue}{\mathsf{fma}\left(\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}, \frac{-1}{2 \cdot a} \cdot -1, -b\right)}
\] |
Taylor expanded in b around inf 0.1
Simplified0.1
[Start]0.1 | \[ \left(0.5 \cdot \frac{1}{a} - 1\right) \cdot b
\] |
---|---|
*-commutative [=>]0.1 | \[ \color{blue}{b \cdot \left(0.5 \cdot \frac{1}{a} - 1\right)}
\] |
associate-*r/ [=>]0.1 | \[ b \cdot \left(\color{blue}{\frac{0.5 \cdot 1}{a}} - 1\right)
\] |
metadata-eval [=>]0.1 | \[ b \cdot \left(\frac{\color{blue}{0.5}}{a} - 1\right)
\] |
sub-neg [=>]0.1 | \[ b \cdot \color{blue}{\left(\frac{0.5}{a} + \left(-1\right)\right)}
\] |
metadata-eval [=>]0.1 | \[ b \cdot \left(\frac{0.5}{a} + \color{blue}{-1}\right)
\] |
distribute-rgt-in [=>]0.1 | \[ \color{blue}{\frac{0.5}{a} \cdot b + -1 \cdot b}
\] |
associate-*l/ [=>]0.0 | \[ \color{blue}{\frac{0.5 \cdot b}{a}} + -1 \cdot b
\] |
associate-*r/ [<=]0.1 | \[ \color{blue}{0.5 \cdot \frac{b}{a}} + -1 \cdot b
\] |
mul-1-neg [=>]0.1 | \[ 0.5 \cdot \frac{b}{a} + \color{blue}{\left(-b\right)}
\] |
sub-neg [<=]0.1 | \[ \color{blue}{0.5 \cdot \frac{b}{a} - b}
\] |
associate-*r/ [=>]0.0 | \[ \color{blue}{\frac{0.5 \cdot b}{a}} - b
\] |
associate-*l/ [<=]0.1 | \[ \color{blue}{\frac{0.5}{a} \cdot b} - b
\] |
*-commutative [=>]0.1 | \[ \color{blue}{b \cdot \frac{0.5}{a}} - b
\] |
Final simplification6.0
Alternative 1 | |
---|---|
Error | 10.2 |
Cost | 7368 |
Alternative 2 | |
---|---|
Error | 16.1 |
Cost | 7048 |
Alternative 3 | |
---|---|
Error | 29.4 |
Cost | 848 |
Alternative 4 | |
---|---|
Error | 29.0 |
Cost | 848 |
Alternative 5 | |
---|---|
Error | 29.1 |
Cost | 848 |
Alternative 6 | |
---|---|
Error | 29.3 |
Cost | 584 |
Alternative 7 | |
---|---|
Error | 18.9 |
Cost | 580 |
Alternative 8 | |
---|---|
Error | 18.9 |
Cost | 580 |
Alternative 9 | |
---|---|
Error | 37.7 |
Cost | 128 |
herbie shell --seed 1
(FPCore (b a c)
:name "-b + sqrt(b*b - 4*a*c)/(2 * a)"
:precision binary64
:pre (and (and (and (<= -1.78e+308 b) (<= b 1.79e+308)) (and (<= -1.78e+308 a) (<= a 1.79e+308))) (and (<= -1.79e+308 c) (<= c 1.79e+308)))
(+ (- b) (/ (sqrt (- (* b b) (* (* 4.0 a) c))) (* 2.0 a))))