
(FPCore (a b c) :precision binary32 (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
float code(float a, float b, float c) {
return (-b - sqrtf(((b * b) - (4.0f * (a * c))))) / (2.0f * a);
}
real(4) function code(a, b, c)
real(4), intent (in) :: a
real(4), intent (in) :: b
real(4), intent (in) :: c
code = (-b - sqrt(((b * b) - (4.0e0 * (a * c))))) / (2.0e0 * a)
end function
function code(a, b, c) return Float32(Float32(Float32(-b) - sqrt(Float32(Float32(b * b) - Float32(Float32(4.0) * Float32(a * c))))) / Float32(Float32(2.0) * a)) end
function tmp = code(a, b, c) tmp = (-b - sqrt(((b * b) - (single(4.0) * (a * c))))) / (single(2.0) * a); end
\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 binary32 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary32 (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
float code(float a, float b, float c) {
return (-b - sqrtf(((b * b) - (4.0f * (a * c))))) / (2.0f * a);
}
real(4) function code(a, b, c)
real(4), intent (in) :: a
real(4), intent (in) :: b
real(4), intent (in) :: c
code = (-b - sqrt(((b * b) - (4.0e0 * (a * c))))) / (2.0e0 * a)
end function
function code(a, b, c) return Float32(Float32(Float32(-b) - sqrt(Float32(Float32(b * b) - Float32(Float32(4.0) * Float32(a * c))))) / Float32(Float32(2.0) * a)) end
function tmp = code(a, b, c) tmp = (-b - sqrt(((b * b) - (single(4.0) * (a * c))))) / (single(2.0) * a); end
\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 binary32
(if (<= b -9.999999974752427e-7)
(/ c (- b))
(if (<= b 100000000376832.0)
(/ (+ (sqrt (dotprod (* -4.0 c) a b b)) b) (* -2.0 a))
(- (/ c b) (/ b a)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.999999974752427 \cdot 10^{-7}:\\
\;\;\;\;\frac{c}{-b}\\
\mathbf{elif}\;b \leq 100000000376832:\\
\;\;\;\;\frac{\sqrt{\mathsf{dotprod}\left(\left(-4 \cdot c\right), a, b, b\right)} + b}{-2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -9.99999997e-7Initial program 13.1%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3289.3
Applied rewrites89.3%
if -9.99999997e-7 < b < 1e14Initial program 77.6%
Applied rewrites77.3%
Applied rewrites77.6%
lift-dotprod.f32N/A
+-commutativeN/A
lift-*.f32N/A
associate-*l*N/A
lift-*.f32N/A
*-commutativeN/A
+-commutativeN/A
lift-dotprod.f3277.7
lift-*.f32N/A
*-commutativeN/A
lower-*.f3277.7
Applied rewrites77.7%
if 1e14 < b Initial program 52.6%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-/.f3299.6
Applied rewrites99.6%
Applied rewrites99.6%
Final simplification86.8%
(FPCore (a b c)
:precision binary32
(if (<= b -9.999999974752427e-7)
(/ c (- b))
(if (<= b 100000000376832.0)
(/ (+ (sqrt (dotprod -4.0 (* c a) b b)) b) (* -2.0 a))
(- (/ c b) (/ b a)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -9.999999974752427 \cdot 10^{-7}:\\
\;\;\;\;\frac{c}{-b}\\
\mathbf{elif}\;b \leq 100000000376832:\\
\;\;\;\;\frac{\sqrt{\mathsf{dotprod}\left(-4, \left(c \cdot a\right), b, b\right)} + b}{-2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -9.99999997e-7Initial program 13.1%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3289.3
Applied rewrites89.3%
if -9.99999997e-7 < b < 1e14Initial program 77.6%
Applied rewrites77.6%
if 1e14 < b Initial program 52.6%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-/.f3299.6
Applied rewrites99.6%
Applied rewrites99.6%
Final simplification86.8%
(FPCore (a b c)
:precision binary32
(if (<= b -1.999999987845058e-8)
(/ (fma a (square (/ c b)) c) (- b))
(if (<= b 1.9499999837080395e-7)
(/ (+ b (sqrt (* (* -4.0 c) a))) (* 2.0 (- a)))
(- (/ c b) (/ b a)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.999999987845058 \cdot 10^{-8}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \mathsf{square}\left(\left(\frac{c}{b}\right)\right), c\right)}{-b}\\
\mathbf{elif}\;b \leq 1.9499999837080395 \cdot 10^{-7}:\\
\;\;\;\;\frac{b + \sqrt{\left(-4 \cdot c\right) \cdot a}}{2 \cdot \left(-a\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -1.99999999e-8Initial program 17.5%
Applied rewrites17.5%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
unpow2N/A
unpow2N/A
times-fracN/A
lower-square.f32N/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3287.4
Applied rewrites87.4%
if -1.99999999e-8 < b < 1.94999998e-7Initial program 73.5%
Applied rewrites73.5%
Taylor expanded in a around inf
*-commutativeN/A
associate-*r*N/A
lower-*.f32N/A
lower-*.f3266.6
Applied rewrites66.6%
if 1.94999998e-7 < b Initial program 64.7%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-/.f3292.9
Applied rewrites92.9%
Applied rewrites92.9%
Final simplification83.2%
(FPCore (a b c)
:precision binary32
(if (<= b -1.999999987845058e-8)
(/ (fma a (square (/ c b)) c) (- b))
(if (<= b 1.9499999837080395e-7)
(/ (+ (sqrt (* (* c a) -4.0)) b) (* -2.0 a))
(- (/ c b) (/ b a)))))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.999999987845058 \cdot 10^{-8}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \mathsf{square}\left(\left(\frac{c}{b}\right)\right), c\right)}{-b}\\
\mathbf{elif}\;b \leq 1.9499999837080395 \cdot 10^{-7}:\\
\;\;\;\;\frac{\sqrt{\left(c \cdot a\right) \cdot -4} + b}{-2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -1.99999999e-8Initial program 17.5%
Applied rewrites17.5%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f32N/A
unpow2N/A
unpow2N/A
times-fracN/A
lower-square.f32N/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3287.4
Applied rewrites87.4%
if -1.99999999e-8 < b < 1.94999998e-7Initial program 73.5%
Applied rewrites73.4%
Applied rewrites73.5%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f32N/A
*-commutativeN/A
lower-*.f3266.6
Applied rewrites66.6%
if 1.94999998e-7 < b Initial program 64.7%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-/.f3292.9
Applied rewrites92.9%
Applied rewrites92.9%
Final simplification83.2%
(FPCore (a b c) :precision binary32 (if (<= b -4.999999955487895e-38) (/ c (- b)) (- (/ c b) (/ b a))))
float code(float a, float b, float c) {
float tmp;
if (b <= -4.999999955487895e-38f) {
tmp = c / -b;
} else {
tmp = (c / b) - (b / a);
}
return tmp;
}
real(4) function code(a, b, c)
real(4), intent (in) :: a
real(4), intent (in) :: b
real(4), intent (in) :: c
real(4) :: tmp
if (b <= (-4.999999955487895e-38)) then
tmp = c / -b
else
tmp = (c / b) - (b / a)
end if
code = tmp
end function
function code(a, b, c) tmp = Float32(0.0) if (b <= Float32(-4.999999955487895e-38)) tmp = Float32(c / Float32(-b)); else tmp = Float32(Float32(c / b) - Float32(b / a)); end return tmp end
function tmp_2 = code(a, b, c) tmp = single(0.0); if (b <= single(-4.999999955487895e-38)) tmp = c / -b; else tmp = (c / b) - (b / a); end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4.999999955487895 \cdot 10^{-38}:\\
\;\;\;\;\frac{c}{-b}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\
\end{array}
\end{array}
if b < -4.99999996e-38Initial program 34.2%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3267.1
Applied rewrites67.1%
if -4.99999996e-38 < b Initial program 67.4%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-/.f3276.1
Applied rewrites76.1%
Applied rewrites76.1%
Final simplification71.0%
(FPCore (a b c) :precision binary32 (if (<= b -4.999999955487895e-38) (/ c (- b)) (/ (- b) a)))
float code(float a, float b, float c) {
float tmp;
if (b <= -4.999999955487895e-38f) {
tmp = c / -b;
} else {
tmp = -b / a;
}
return tmp;
}
real(4) function code(a, b, c)
real(4), intent (in) :: a
real(4), intent (in) :: b
real(4), intent (in) :: c
real(4) :: tmp
if (b <= (-4.999999955487895e-38)) then
tmp = c / -b
else
tmp = -b / a
end if
code = tmp
end function
function code(a, b, c) tmp = Float32(0.0) if (b <= Float32(-4.999999955487895e-38)) tmp = Float32(c / Float32(-b)); else tmp = Float32(Float32(-b) / a); end return tmp end
function tmp_2 = code(a, b, c) tmp = single(0.0); if (b <= single(-4.999999955487895e-38)) tmp = c / -b; else tmp = -b / a; end tmp_2 = tmp; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -4.999999955487895 \cdot 10^{-38}:\\
\;\;\;\;\frac{c}{-b}\\
\mathbf{else}:\\
\;\;\;\;\frac{-b}{a}\\
\end{array}
\end{array}
if b < -4.99999996e-38Initial program 34.2%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3267.1
Applied rewrites67.1%
if -4.99999996e-38 < b Initial program 67.4%
Taylor expanded in a around 0
associate-*r/N/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3275.2
Applied rewrites75.2%
(FPCore (a b c) :precision binary32 (/ c (- b)))
float code(float a, float b, float c) {
return c / -b;
}
real(4) function code(a, b, c)
real(4), intent (in) :: a
real(4), intent (in) :: b
real(4), intent (in) :: c
code = c / -b
end function
function code(a, b, c) return Float32(c / Float32(-b)) end
function tmp = code(a, b, c) tmp = c / -b; end
\begin{array}{l}
\\
\frac{c}{-b}
\end{array}
Initial program 48.6%
Taylor expanded in b around -inf
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f32N/A
mul-1-negN/A
lower-neg.f3240.1
Applied rewrites40.1%
(FPCore (a b c) :precision binary32 (/ c b))
float code(float a, float b, float c) {
return c / b;
}
real(4) function code(a, b, c)
real(4), intent (in) :: a
real(4), intent (in) :: b
real(4), intent (in) :: c
code = c / b
end function
function code(a, b, c) return Float32(c / b) end
function tmp = code(a, b, c) tmp = c / b; end
\begin{array}{l}
\\
\frac{c}{b}
\end{array}
Initial program 48.6%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f32N/A
lower-/.f32N/A
lower-/.f3235.6
Applied rewrites35.6%
Taylor expanded in a around inf
Applied rewrites12.9%
herbie shell --seed 2024341
(FPCore (a b c)
:name "quadm (p42, negative)"
:precision binary32
:herbie-expected 10
:alt
(! :herbie-platform default (let ((sqtD (let ((x (* (sqrt (fabs a)) (sqrt (fabs c))))) (if (== (copysign a c) a) (* (sqrt (- (fabs (/ b 2)) x)) (sqrt (+ (fabs (/ b 2)) x))) (hypot (/ b 2) x))))) (if (< b 0) (/ c (- sqtD (/ b 2))) (/ (+ (/ b 2) sqtD) (- a)))))
(/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))