
(FPCore (a b c) :precision binary64 (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
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
public static double code(double a, double b, double c) {
return (-b - Math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
def code(a, b, c): return (-b - math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a)
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 tmp = code(a, b, c) tmp = (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a); 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]
\begin{array}{l}
\\
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
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
public static double code(double a, double b, double c) {
return (-b - Math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
def code(a, b, c): return (-b - math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a)
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 tmp = code(a, b, c) tmp = (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a); 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]
\begin{array}{l}
\\
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(if (<= b -6.4e-55)
(/ (- c) b)
(if (<= b 3.4e+76)
(* -0.5 (/ (+ b (sqrt (- (* b b) (* a (* c 4.0))))) a))
(/ (- b) a))))
double code(double a, double b, double c) {
double tmp;
if (b <= -6.4e-55) {
tmp = -c / b;
} else if (b <= 3.4e+76) {
tmp = -0.5 * ((b + sqrt(((b * b) - (a * (c * 4.0))))) / a);
} else {
tmp = -b / a;
}
return tmp;
}
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 <= (-6.4d-55)) then
tmp = -c / b
else if (b <= 3.4d+76) then
tmp = (-0.5d0) * ((b + sqrt(((b * b) - (a * (c * 4.0d0))))) / a)
else
tmp = -b / a
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -6.4e-55) {
tmp = -c / b;
} else if (b <= 3.4e+76) {
tmp = -0.5 * ((b + Math.sqrt(((b * b) - (a * (c * 4.0))))) / a);
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -6.4e-55: tmp = -c / b elif b <= 3.4e+76: tmp = -0.5 * ((b + math.sqrt(((b * b) - (a * (c * 4.0))))) / a) else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -6.4e-55) tmp = Float64(Float64(-c) / b); elseif (b <= 3.4e+76) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(Float64(Float64(b * b) - Float64(a * Float64(c * 4.0))))) / a)); else tmp = Float64(Float64(-b) / a); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -6.4e-55) tmp = -c / b; elseif (b <= 3.4e+76) tmp = -0.5 * ((b + sqrt(((b * b) - (a * (c * 4.0))))) / a); else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -6.4e-55], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 3.4e+76], N[(-0.5 * N[(N[(b + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(a * N[(c * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[((-b) / a), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -6.4 \cdot 10^{-55}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 3.4 \cdot 10^{+76}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{b \cdot b - a \cdot \left(c \cdot 4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -6.4000000000000003e-55Initial program 15.7%
Taylor expanded in b around -inf 92.0%
associate-*r/92.0%
neg-mul-192.0%
Simplified92.0%
if -6.4000000000000003e-55 < b < 3.3999999999999997e76Initial program 74.4%
Simplified74.4%
fma-udef74.4%
associate-*r*74.4%
metadata-eval74.4%
distribute-rgt-neg-in74.4%
*-commutative74.4%
+-commutative74.4%
sub-neg74.4%
*-commutative74.4%
associate-*l*74.4%
Applied egg-rr74.4%
if 3.3999999999999997e76 < b Initial program 47.9%
Taylor expanded in b around inf 97.0%
associate-*r/97.0%
mul-1-neg97.0%
Simplified97.0%
Final simplification85.5%
(FPCore (a b c)
:precision binary64
(if (<= b -4.8e-54)
(/ (- c) b)
(if (<= b 1.55e-72)
(* -0.5 (/ (+ b (sqrt (* c (* a -4.0)))) a))
(- (/ c b) (/ b a)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -4.8e-54) {
tmp = -c / b;
} else if (b <= 1.55e-72) {
tmp = -0.5 * ((b + sqrt((c * (a * -4.0)))) / a);
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
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 <= (-4.8d-54)) then
tmp = -c / b
else if (b <= 1.55d-72) then
tmp = (-0.5d0) * ((b + sqrt((c * (a * (-4.0d0))))) / a)
else
tmp = (c / b) - (b / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -4.8e-54) {
tmp = -c / b;
} else if (b <= 1.55e-72) {
tmp = -0.5 * ((b + Math.sqrt((c * (a * -4.0)))) / a);
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -4.8e-54: tmp = -c / b elif b <= 1.55e-72: tmp = -0.5 * ((b + math.sqrt((c * (a * -4.0)))) / a) else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -4.8e-54) tmp = Float64(Float64(-c) / b); elseif (b <= 1.55e-72) tmp = Float64(-0.5 * Float64(Float64(b + sqrt(Float64(c * Float64(a * -4.0)))) / a)); else tmp = Float64(Float64(c / b) - Float64(b / a)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -4.8e-54) tmp = -c / b; elseif (b <= 1.55e-72) tmp = -0.5 * ((b + sqrt((c * (a * -4.0)))) / a); else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -4.8e-54], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1.55e-72], N[(-0.5 * N[(N[(b + N[Sqrt[N[(c * N[(a * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4.8 \cdot 10^{-54}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 1.55 \cdot 10^{-72}:\\
\;\;\;\;-0.5 \cdot \frac{b + \sqrt{c \cdot \left(a \cdot -4\right)}}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -4.80000000000000026e-54Initial program 15.7%
Taylor expanded in b around -inf 92.0%
associate-*r/92.0%
neg-mul-192.0%
Simplified92.0%
if -4.80000000000000026e-54 < b < 1.5499999999999999e-72Initial program 69.6%
Simplified69.6%
Taylor expanded in a around inf 65.2%
*-commutative65.2%
associate-*r*65.3%
Simplified65.3%
if 1.5499999999999999e-72 < b Initial program 61.5%
Taylor expanded in b around inf 87.4%
mul-1-neg87.4%
unsub-neg87.4%
Simplified87.4%
Final simplification82.4%
(FPCore (a b c) :precision binary64 (if (<= b -5e-310) (/ (- c) b) (- (/ c b) (/ b a))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5e-310) {
tmp = -c / b;
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
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 <= (-5d-310)) then
tmp = -c / b
else
tmp = (c / b) - (b / a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -5e-310) {
tmp = -c / b;
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5e-310: tmp = -c / b else: tmp = (c / b) - (b / a) return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5e-310) tmp = Float64(Float64(-c) / b); else tmp = Float64(Float64(c / b) - Float64(b / a)); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5e-310) tmp = -c / b; else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], N[((-c) / b), $MachinePrecision], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -4.999999999999985e-310Initial program 33.1%
Taylor expanded in b around -inf 69.3%
associate-*r/69.3%
neg-mul-169.3%
Simplified69.3%
if -4.999999999999985e-310 < b Initial program 65.1%
Taylor expanded in b around inf 71.9%
mul-1-neg71.9%
unsub-neg71.9%
Simplified71.9%
Final simplification70.6%
(FPCore (a b c) :precision binary64 (if (<= b -106000000000.0) (/ c b) (/ (- b) a)))
double code(double a, double b, double c) {
double tmp;
if (b <= -106000000000.0) {
tmp = c / b;
} else {
tmp = -b / a;
}
return tmp;
}
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 <= (-106000000000.0d0)) then
tmp = c / b
else
tmp = -b / a
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -106000000000.0) {
tmp = c / b;
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -106000000000.0: tmp = c / b else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -106000000000.0) tmp = Float64(c / b); else tmp = Float64(Float64(-b) / a); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -106000000000.0) tmp = c / b; else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -106000000000.0], N[(c / b), $MachinePrecision], N[((-b) / a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -106000000000:\\
\;\;\;\;\frac{c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -1.06e11Initial program 13.2%
clear-num13.1%
inv-pow13.1%
Applied egg-rr3.7%
unpow-13.7%
fma-udef3.7%
*-commutative3.7%
associate-*l*3.7%
*-commutative3.7%
fma-def3.7%
Simplified3.7%
Taylor expanded in a around 0 32.6%
if -1.06e11 < b Initial program 63.8%
Taylor expanded in b around inf 51.6%
associate-*r/51.6%
mul-1-neg51.6%
Simplified51.6%
Final simplification46.1%
(FPCore (a b c) :precision binary64 (if (<= b -5.2e-295) (/ (- c) b) (/ (- b) a)))
double code(double a, double b, double c) {
double tmp;
if (b <= -5.2e-295) {
tmp = -c / b;
} else {
tmp = -b / a;
}
return tmp;
}
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 <= (-5.2d-295)) then
tmp = -c / b
else
tmp = -b / a
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -5.2e-295) {
tmp = -c / b;
} else {
tmp = -b / a;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5.2e-295: tmp = -c / b else: tmp = -b / a return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5.2e-295) tmp = Float64(Float64(-c) / b); else tmp = Float64(Float64(-b) / a); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5.2e-295) tmp = -c / b; else tmp = -b / a; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5.2e-295], N[((-c) / b), $MachinePrecision], N[((-b) / a), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5.2 \cdot 10^{-295}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -5.1999999999999997e-295Initial program 32.5%
Taylor expanded in b around -inf 69.8%
associate-*r/69.8%
neg-mul-169.8%
Simplified69.8%
if -5.1999999999999997e-295 < b Initial program 65.3%
Taylor expanded in b around inf 71.0%
associate-*r/71.0%
mul-1-neg71.0%
Simplified71.0%
Final simplification70.4%
(FPCore (a b c) :precision binary64 (/ b a))
double code(double a, double b, double c) {
return b / a;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = b / a
end function
public static double code(double a, double b, double c) {
return b / a;
}
def code(a, b, c): return b / a
function code(a, b, c) return Float64(b / a) end
function tmp = code(a, b, c) tmp = b / a; end
code[a_, b_, c_] := N[(b / a), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{a}
\end{array}
Initial program 49.2%
clear-num49.0%
inv-pow49.0%
Applied egg-rr29.8%
unpow-129.8%
fma-udef29.8%
*-commutative29.8%
associate-*l*29.8%
*-commutative29.8%
fma-def29.8%
Simplified29.8%
Taylor expanded in b around -inf 2.5%
Final simplification2.5%
(FPCore (a b c) :precision binary64 (/ c b))
double code(double a, double b, double c) {
return c / b;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = c / b
end function
public static double code(double a, double b, double c) {
return c / b;
}
def code(a, b, c): return c / b
function code(a, b, c) return Float64(c / b) end
function tmp = code(a, b, c) tmp = c / b; end
code[a_, b_, c_] := N[(c / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{b}
\end{array}
Initial program 49.2%
clear-num49.0%
inv-pow49.0%
Applied egg-rr29.8%
unpow-129.8%
fma-udef29.8%
*-commutative29.8%
associate-*l*29.8%
*-commutative29.8%
fma-def29.8%
Simplified29.8%
Taylor expanded in a around 0 11.6%
Final simplification11.6%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (sqrt (- (* b b) (* 4.0 (* a c))))))
(if (< b 0.0)
(/ c (* a (/ (+ (- b) t_0) (* 2.0 a))))
(/ (- (- b) t_0) (* 2.0 a)))))
double code(double a, double b, double c) {
double t_0 = sqrt(((b * b) - (4.0 * (a * c))));
double tmp;
if (b < 0.0) {
tmp = c / (a * ((-b + t_0) / (2.0 * a)));
} else {
tmp = (-b - t_0) / (2.0 * a);
}
return tmp;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
real(8) :: tmp
t_0 = sqrt(((b * b) - (4.0d0 * (a * c))))
if (b < 0.0d0) then
tmp = c / (a * ((-b + t_0) / (2.0d0 * a)))
else
tmp = (-b - t_0) / (2.0d0 * a)
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double t_0 = Math.sqrt(((b * b) - (4.0 * (a * c))));
double tmp;
if (b < 0.0) {
tmp = c / (a * ((-b + t_0) / (2.0 * a)));
} else {
tmp = (-b - t_0) / (2.0 * a);
}
return tmp;
}
def code(a, b, c): t_0 = math.sqrt(((b * b) - (4.0 * (a * c)))) tmp = 0 if b < 0.0: tmp = c / (a * ((-b + t_0) / (2.0 * a))) else: tmp = (-b - t_0) / (2.0 * a) return tmp
function code(a, b, c) t_0 = sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c)))) tmp = 0.0 if (b < 0.0) tmp = Float64(c / Float64(a * Float64(Float64(Float64(-b) + t_0) / Float64(2.0 * a)))); else tmp = Float64(Float64(Float64(-b) - t_0) / Float64(2.0 * a)); end return tmp end
function tmp_2 = code(a, b, c) t_0 = sqrt(((b * b) - (4.0 * (a * c)))); tmp = 0.0; if (b < 0.0) tmp = c / (a * ((-b + t_0) / (2.0 * a))); else tmp = (-b - t_0) / (2.0 * a); end tmp_2 = tmp; end
code[a_, b_, c_] := Block[{t$95$0 = N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[Less[b, 0.0], N[(c / N[(a * N[(N[((-b) + t$95$0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[((-b) - t$95$0), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}\\
\mathbf{if}\;b < 0:\\
\;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) + t_0}{2 \cdot a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(-b\right) - t_0}{2 \cdot a}\\
\end{array}
\end{array}
herbie shell --seed 2023215
(FPCore (a b c)
:name "quadm (p42, negative)"
:precision binary64
:herbie-target
(if (< b 0.0) (/ c (* a (/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))) (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
(/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))