
(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);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(a, b, c)
use fmin_fmax_functions
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 5 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);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(a, b, c)
use fmin_fmax_functions
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 -3.2e-96)
(/ (- c) b)
(if (<= b 3.1e+88)
(/ (+ b (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 (- a)))
(fma (/ b a) -1.0 (/ c b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.2e-96) {
tmp = -c / b;
} else if (b <= 3.1e+88) {
tmp = (b + sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * -a);
} else {
tmp = fma((b / a), -1.0, (c / b));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -3.2e-96) tmp = Float64(Float64(-c) / b); elseif (b <= 3.1e+88) tmp = Float64(Float64(b + sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * Float64(-a))); else tmp = fma(Float64(b / a), -1.0, Float64(c / b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -3.2e-96], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 3.1e+88], 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], N[(N[(b / a), $MachinePrecision] * -1.0 + N[(c / b), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.2 \cdot 10^{-96}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 3.1 \cdot 10^{+88}:\\
\;\;\;\;\frac{b + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot \left(-a\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{b}{a}, -1, \frac{c}{b}\right)\\
\end{array}
\end{array}
if b < -3.20000000000000012e-96Initial program 12.3%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f6491.5
Applied rewrites91.5%
if -3.20000000000000012e-96 < b < 3.1000000000000001e88Initial program 77.1%
if 3.1000000000000001e88 < b Initial program 62.6%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f6492.1
Applied rewrites92.1%
Final simplification86.1%
(FPCore (a b c)
:precision binary64
(if (<= b -3.2e-96)
(/ (- c) b)
(if (<= b 1.25e-140)
(* (fma (/ (/ b a) c) 0.5 (- (sqrt (* (/ (pow a -1.0) c) -1.0)))) (- c))
(fma (/ b a) -1.0 (/ c b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.2e-96) {
tmp = -c / b;
} else if (b <= 1.25e-140) {
tmp = fma(((b / a) / c), 0.5, -sqrt(((pow(a, -1.0) / c) * -1.0))) * -c;
} else {
tmp = fma((b / a), -1.0, (c / b));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -3.2e-96) tmp = Float64(Float64(-c) / b); elseif (b <= 1.25e-140) tmp = Float64(fma(Float64(Float64(b / a) / c), 0.5, Float64(-sqrt(Float64(Float64((a ^ -1.0) / c) * -1.0)))) * Float64(-c)); else tmp = fma(Float64(b / a), -1.0, Float64(c / b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -3.2e-96], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 1.25e-140], N[(N[(N[(N[(b / a), $MachinePrecision] / c), $MachinePrecision] * 0.5 + (-N[Sqrt[N[(N[(N[Power[a, -1.0], $MachinePrecision] / c), $MachinePrecision] * -1.0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision] * (-c)), $MachinePrecision], N[(N[(b / a), $MachinePrecision] * -1.0 + N[(c / b), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.2 \cdot 10^{-96}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 1.25 \cdot 10^{-140}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\frac{b}{a}}{c}, 0.5, -\sqrt{\frac{{a}^{-1}}{c} \cdot -1}\right) \cdot \left(-c\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{b}{a}, -1, \frac{c}{b}\right)\\
\end{array}
\end{array}
if b < -3.20000000000000012e-96Initial program 12.3%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f6491.5
Applied rewrites91.5%
if -3.20000000000000012e-96 < b < 1.25000000000000004e-140Initial program 75.7%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites73.9%
lift-*.f64N/A
lift-pow.f64N/A
unpow-1N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f64N/A
inv-powN/A
lower-pow.f6475.4
Applied rewrites75.4%
if 1.25000000000000004e-140 < b Initial program 69.7%
Taylor expanded in c around 0
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f6474.9
Applied rewrites74.9%
Final simplification81.3%
(FPCore (a b c)
:precision binary64
(if (<= b -3.2e-96)
(/ (- c) b)
(if (<= b 14.5)
(* (fma (/ (/ b a) c) 0.5 (- (sqrt (* (/ (pow a -1.0) c) -1.0)))) (- c))
(fma -0.5 (/ b a) (- (sqrt (/ (- c) a)))))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.2e-96) {
tmp = -c / b;
} else if (b <= 14.5) {
tmp = fma(((b / a) / c), 0.5, -sqrt(((pow(a, -1.0) / c) * -1.0))) * -c;
} else {
tmp = fma(-0.5, (b / a), -sqrt((-c / a)));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -3.2e-96) tmp = Float64(Float64(-c) / b); elseif (b <= 14.5) tmp = Float64(fma(Float64(Float64(b / a) / c), 0.5, Float64(-sqrt(Float64(Float64((a ^ -1.0) / c) * -1.0)))) * Float64(-c)); else tmp = fma(-0.5, Float64(b / a), Float64(-sqrt(Float64(Float64(-c) / a)))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -3.2e-96], N[((-c) / b), $MachinePrecision], If[LessEqual[b, 14.5], N[(N[(N[(N[(b / a), $MachinePrecision] / c), $MachinePrecision] * 0.5 + (-N[Sqrt[N[(N[(N[Power[a, -1.0], $MachinePrecision] / c), $MachinePrecision] * -1.0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision] * (-c)), $MachinePrecision], N[(-0.5 * N[(b / a), $MachinePrecision] + (-N[Sqrt[N[((-c) / a), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.2 \cdot 10^{-96}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{elif}\;b \leq 14.5:\\
\;\;\;\;\mathsf{fma}\left(\frac{\frac{b}{a}}{c}, 0.5, -\sqrt{\frac{{a}^{-1}}{c} \cdot -1}\right) \cdot \left(-c\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-0.5, \frac{b}{a}, -\sqrt{\frac{-c}{a}}\right)\\
\end{array}
\end{array}
if b < -3.20000000000000012e-96Initial program 12.3%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f6491.5
Applied rewrites91.5%
if -3.20000000000000012e-96 < b < 14.5Initial program 77.6%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites62.2%
lift-*.f64N/A
lift-pow.f64N/A
unpow-1N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f64N/A
inv-powN/A
lower-pow.f6463.3
Applied rewrites63.3%
if 14.5 < b Initial program 66.4%
Taylor expanded in a around -inf
lower-fma.f64N/A
lower-/.f64N/A
sqrt-unprodN/A
lower-sqrt.f64N/A
lower-*.f64N/A
lower-/.f6427.2
Applied rewrites27.2%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
sqrt-prodN/A
lower-sqrt.f64N/A
*-commutativeN/A
mul-1-negN/A
lower-neg.f64N/A
lift-/.f6431.2
Applied rewrites31.2%
Final simplification63.7%
(FPCore (a b c) :precision binary64 (if (<= b -3.2e-96) (/ (- c) b) (* (fma (/ (/ b a) c) 0.5 (- (sqrt (* (/ (pow a -1.0) c) -1.0)))) (- c))))
double code(double a, double b, double c) {
double tmp;
if (b <= -3.2e-96) {
tmp = -c / b;
} else {
tmp = fma(((b / a) / c), 0.5, -sqrt(((pow(a, -1.0) / c) * -1.0))) * -c;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -3.2e-96) tmp = Float64(Float64(-c) / b); else tmp = Float64(fma(Float64(Float64(b / a) / c), 0.5, Float64(-sqrt(Float64(Float64((a ^ -1.0) / c) * -1.0)))) * Float64(-c)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -3.2e-96], N[((-c) / b), $MachinePrecision], N[(N[(N[(N[(b / a), $MachinePrecision] / c), $MachinePrecision] * 0.5 + (-N[Sqrt[N[(N[(N[Power[a, -1.0], $MachinePrecision] / c), $MachinePrecision] * -1.0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision] * (-c)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -3.2 \cdot 10^{-96}:\\
\;\;\;\;\frac{-c}{b}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{\frac{b}{a}}{c}, 0.5, -\sqrt{\frac{{a}^{-1}}{c} \cdot -1}\right) \cdot \left(-c\right)\\
\end{array}
\end{array}
if b < -3.20000000000000012e-96Initial program 12.3%
Taylor expanded in b around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f6491.5
Applied rewrites91.5%
if -3.20000000000000012e-96 < b Initial program 71.7%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites43.1%
lift-*.f64N/A
lift-pow.f64N/A
unpow-1N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f64N/A
inv-powN/A
lower-pow.f6443.5
Applied rewrites43.5%
Final simplification61.9%
(FPCore (a b c) :precision binary64 (* (fma (/ (/ b a) c) 0.5 (- (sqrt (* (/ (pow a -1.0) c) -1.0)))) (- c)))
double code(double a, double b, double c) {
return fma(((b / a) / c), 0.5, -sqrt(((pow(a, -1.0) / c) * -1.0))) * -c;
}
function code(a, b, c) return Float64(fma(Float64(Float64(b / a) / c), 0.5, Float64(-sqrt(Float64(Float64((a ^ -1.0) / c) * -1.0)))) * Float64(-c)) end
code[a_, b_, c_] := N[(N[(N[(N[(b / a), $MachinePrecision] / c), $MachinePrecision] * 0.5 + (-N[Sqrt[N[(N[(N[Power[a, -1.0], $MachinePrecision] / c), $MachinePrecision] * -1.0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision] * (-c)), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{\frac{b}{a}}{c}, 0.5, -\sqrt{\frac{{a}^{-1}}{c} \cdot -1}\right) \cdot \left(-c\right)
\end{array}
Initial program 49.0%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites29.3%
lift-*.f64N/A
lift-pow.f64N/A
unpow-1N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f64N/A
inv-powN/A
lower-pow.f6429.7
Applied rewrites29.7%
Final simplification29.7%
herbie shell --seed 2025057
(FPCore (a b c)
:name "The quadratic formula (r2)"
:precision binary64
:alt
(! :herbie-platform default (let ((d (sqrt (- (* b b) (* 4 (* a c)))))) (let ((r1 (/ (+ (- b) d) (* 2 a)))) (let ((r2 (/ (- (- b) d) (* 2 a)))) (if (< b 0) (/ c (* a r1)) r2)))))
(/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))