
(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(Float64(4.0 * 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[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 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(Float64(4.0 * 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[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(if (<= b -5.4e+124)
(/ (- b) a)
(if (<= b 4.2e-132)
(/ (- (sqrt (fma (* -4.0 a) c (* b b))) b) (+ a a))
(if (<= b 5.2e+102)
(/ (/ (* (* -4.0 a) c) (+ (sqrt (fma b b (* (* a -4.0) c))) b)) (+ a a))
(/ (- c) b)))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5.4e+124) {
tmp = -b / a;
} else if (b <= 4.2e-132) {
tmp = (sqrt(fma((-4.0 * a), c, (b * b))) - b) / (a + a);
} else if (b <= 5.2e+102) {
tmp = (((-4.0 * a) * c) / (sqrt(fma(b, b, ((a * -4.0) * c))) + b)) / (a + a);
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -5.4e+124) tmp = Float64(Float64(-b) / a); elseif (b <= 4.2e-132) tmp = Float64(Float64(sqrt(fma(Float64(-4.0 * a), c, Float64(b * b))) - b) / Float64(a + a)); elseif (b <= 5.2e+102) tmp = Float64(Float64(Float64(Float64(-4.0 * a) * c) / Float64(sqrt(fma(b, b, Float64(Float64(a * -4.0) * c))) + b)) / Float64(a + a)); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -5.4e+124], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 4.2e-132], N[(N[(N[Sqrt[N[(N[(-4.0 * a), $MachinePrecision] * c + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a + a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 5.2e+102], N[(N[(N[(N[(-4.0 * a), $MachinePrecision] * c), $MachinePrecision] / N[(N[Sqrt[N[(b * b + N[(N[(a * -4.0), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision] / N[(a + a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5.4 \cdot 10^{+124}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 4.2 \cdot 10^{-132}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(-4 \cdot a, c, b \cdot b\right)} - b}{a + a}\\
\mathbf{elif}\;b \leq 5.2 \cdot 10^{+102}:\\
\;\;\;\;\frac{\frac{\left(-4 \cdot a\right) \cdot c}{\sqrt{\mathsf{fma}\left(b, b, \left(a \cdot -4\right) \cdot c\right)} + b}}{a + a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -5.39999999999999956e124Initial program 33.8%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6491.4
Applied rewrites91.4%
if -5.39999999999999956e124 < b < 4.2000000000000002e-132Initial program 84.3%
lift-neg.f64N/A
lift-+.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
lower-+.f64N/A
Applied rewrites84.3%
lift-*.f64N/A
count-2-revN/A
lower-+.f6484.3
Applied rewrites84.3%
if 4.2000000000000002e-132 < b < 5.20000000000000013e102Initial program 40.8%
lift-neg.f64N/A
lift-+.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
lower-+.f64N/A
Applied rewrites40.8%
lift-*.f64N/A
count-2-revN/A
lower-+.f6440.8
Applied rewrites40.8%
lift-neg.f64N/A
lift-+.f64N/A
lift-sqrt.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
flip-+N/A
lower-/.f64N/A
Applied rewrites40.5%
Taylor expanded in a around 0
associate-*r*N/A
lower-*.f64N/A
lower-*.f6476.4
Applied rewrites76.4%
if 5.20000000000000013e102 < b Initial program 2.3%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6488.2
Applied rewrites88.2%
Final simplification84.7%
(FPCore (a b c)
:precision binary64
(if (<= b -5.4e+124)
(/ (- b) a)
(if (<= b 2.1e-112)
(/ (- (sqrt (fma (* -4.0 a) c (* b b))) b) (+ a a))
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5.4e+124) {
tmp = -b / a;
} else if (b <= 2.1e-112) {
tmp = (sqrt(fma((-4.0 * a), c, (b * b))) - b) / (a + a);
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -5.4e+124) tmp = Float64(Float64(-b) / a); elseif (b <= 2.1e-112) tmp = Float64(Float64(sqrt(fma(Float64(-4.0 * a), c, Float64(b * b))) - b) / Float64(a + a)); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -5.4e+124], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 2.1e-112], N[(N[(N[Sqrt[N[(N[(-4.0 * a), $MachinePrecision] * c + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a + a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5.4 \cdot 10^{+124}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{-112}:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(-4 \cdot a, c, b \cdot b\right)} - b}{a + a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -5.39999999999999956e124Initial program 33.8%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6491.4
Applied rewrites91.4%
if -5.39999999999999956e124 < b < 2.1000000000000001e-112Initial program 84.1%
lift-neg.f64N/A
lift-+.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
lower-+.f64N/A
Applied rewrites84.1%
lift-*.f64N/A
count-2-revN/A
lower-+.f6484.1
Applied rewrites84.1%
if 2.1000000000000001e-112 < b Initial program 17.9%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6478.2
Applied rewrites78.2%
Final simplification83.1%
(FPCore (a b c)
:precision binary64
(if (<= b -0.0021)
(/ (- b) a)
(if (<= b 2.1e-112)
(/ (fma -0.5 b (sqrt (* (* c a) -1.0))) a)
(/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -0.0021) {
tmp = -b / a;
} else if (b <= 2.1e-112) {
tmp = fma(-0.5, b, sqrt(((c * a) * -1.0))) / a;
} else {
tmp = -c / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= -0.0021) tmp = Float64(Float64(-b) / a); elseif (b <= 2.1e-112) tmp = Float64(fma(-0.5, b, sqrt(Float64(Float64(c * a) * -1.0))) / a); else tmp = Float64(Float64(-c) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, -0.0021], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 2.1e-112], N[(N[(-0.5 * b + N[Sqrt[N[(N[(c * a), $MachinePrecision] * -1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -0.0021:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{-112}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-0.5, b, \sqrt{\left(c \cdot a\right) \cdot -1}\right)}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -0.00209999999999999987Initial program 60.3%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6488.3
Applied rewrites88.3%
if -0.00209999999999999987 < b < 2.1000000000000001e-112Initial program 80.6%
Taylor expanded in a around inf
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sqrt-unprodN/A
lower-sqrt.f64N/A
lower-*.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6430.8
Applied rewrites30.8%
Taylor expanded in a around -inf
*-commutativeN/A
lower-fma.f64N/A
lift-/.f64N/A
sqrt-unprodN/A
lower-sqrt.f64N/A
lower-*.f64N/A
lift-/.f6430.8
Applied rewrites30.8%
Taylor expanded in a around 0
lower-/.f64N/A
lower-fma.f64N/A
sqrt-unprodN/A
lower-sqrt.f64N/A
lower-*.f64N/A
*-commutativeN/A
lift-*.f6469.2
Applied rewrites69.2%
if 2.1000000000000001e-112 < b Initial program 17.9%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6478.2
Applied rewrites78.2%
(FPCore (a b c) :precision binary64 (if (<= b -1.8e-12) (/ (- b) a) (if (<= b 2.1e-112) (/ (sqrt (* (* a -4.0) c)) (+ a a)) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.8e-12) {
tmp = -b / a;
} else if (b <= 2.1e-112) {
tmp = sqrt(((a * -4.0) * c)) / (a + a);
} else {
tmp = -c / b;
}
return tmp;
}
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
real(8) :: tmp
if (b <= (-1.8d-12)) then
tmp = -b / a
else if (b <= 2.1d-112) then
tmp = sqrt(((a * (-4.0d0)) * c)) / (a + a)
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -1.8e-12) {
tmp = -b / a;
} else if (b <= 2.1e-112) {
tmp = Math.sqrt(((a * -4.0) * c)) / (a + a);
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.8e-12: tmp = -b / a elif b <= 2.1e-112: tmp = math.sqrt(((a * -4.0) * c)) / (a + a) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.8e-12) tmp = Float64(Float64(-b) / a); elseif (b <= 2.1e-112) tmp = Float64(sqrt(Float64(Float64(a * -4.0) * c)) / Float64(a + a)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.8e-12) tmp = -b / a; elseif (b <= 2.1e-112) tmp = sqrt(((a * -4.0) * c)) / (a + a); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.8e-12], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 2.1e-112], N[(N[Sqrt[N[(N[(a * -4.0), $MachinePrecision] * c), $MachinePrecision]], $MachinePrecision] / N[(a + a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.8 \cdot 10^{-12}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 2.1 \cdot 10^{-112}:\\
\;\;\;\;\frac{\sqrt{\left(a \cdot -4\right) \cdot c}}{a + a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.8e-12Initial program 61.2%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6487.4
Applied rewrites87.4%
if -1.8e-12 < b < 2.1000000000000001e-112Initial program 80.1%
lift-neg.f64N/A
lift-+.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
lower-+.f64N/A
Applied rewrites80.1%
lift-*.f64N/A
count-2-revN/A
lower-+.f6480.1
Applied rewrites80.1%
Taylor expanded in a around inf
sqrt-unprodN/A
*-commutativeN/A
associate-*r*N/A
lower-sqrt.f64N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6467.1
Applied rewrites67.1%
if 2.1000000000000001e-112 < b Initial program 17.9%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6478.2
Applied rewrites78.2%
(FPCore (a b c) :precision binary64 (if (<= b -5.5e-168) (/ (- b) a) (if (<= b 1.6e-112) (- (sqrt (/ (- c) a))) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -5.5e-168) {
tmp = -b / a;
} else if (b <= 1.6e-112) {
tmp = -sqrt((-c / a));
} else {
tmp = -c / b;
}
return tmp;
}
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
real(8) :: tmp
if (b <= (-5.5d-168)) then
tmp = -b / a
else if (b <= 1.6d-112) then
tmp = -sqrt((-c / a))
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -5.5e-168) {
tmp = -b / a;
} else if (b <= 1.6e-112) {
tmp = -Math.sqrt((-c / a));
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -5.5e-168: tmp = -b / a elif b <= 1.6e-112: tmp = -math.sqrt((-c / a)) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -5.5e-168) tmp = Float64(Float64(-b) / a); elseif (b <= 1.6e-112) tmp = Float64(-sqrt(Float64(Float64(-c) / a))); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -5.5e-168) tmp = -b / a; elseif (b <= 1.6e-112) tmp = -sqrt((-c / a)); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -5.5e-168], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 1.6e-112], (-N[Sqrt[N[((-c) / a), $MachinePrecision]], $MachinePrecision]), N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -5.5 \cdot 10^{-168}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 1.6 \cdot 10^{-112}:\\
\;\;\;\;-\sqrt{\frac{-c}{a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -5.4999999999999999e-168Initial program 67.1%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6473.1
Applied rewrites73.1%
if -5.4999999999999999e-168 < b < 1.59999999999999997e-112Initial program 78.2%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f645.3
Applied rewrites5.3%
Taylor expanded in a around -inf
Applied rewrites47.9%
if 1.59999999999999997e-112 < b Initial program 17.9%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6478.2
Applied rewrites78.2%
(FPCore (a b c) :precision binary64 (if (<= b -1.6e-215) (/ (- b) a) (if (<= b 5.4e-129) (sqrt (/ (- c) a)) (/ (- c) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= -1.6e-215) {
tmp = -b / a;
} else if (b <= 5.4e-129) {
tmp = sqrt((-c / a));
} else {
tmp = -c / b;
}
return tmp;
}
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
real(8) :: tmp
if (b <= (-1.6d-215)) then
tmp = -b / a
else if (b <= 5.4d-129) then
tmp = sqrt((-c / a))
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= -1.6e-215) {
tmp = -b / a;
} else if (b <= 5.4e-129) {
tmp = Math.sqrt((-c / a));
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= -1.6e-215: tmp = -b / a elif b <= 5.4e-129: tmp = math.sqrt((-c / a)) else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= -1.6e-215) tmp = Float64(Float64(-b) / a); elseif (b <= 5.4e-129) tmp = sqrt(Float64(Float64(-c) / a)); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= -1.6e-215) tmp = -b / a; elseif (b <= 5.4e-129) tmp = sqrt((-c / a)); else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, -1.6e-215], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 5.4e-129], N[Sqrt[N[((-c) / a), $MachinePrecision]], $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq -1.6 \cdot 10^{-215}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{elif}\;b \leq 5.4 \cdot 10^{-129}:\\
\;\;\;\;\sqrt{\frac{-c}{a}}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < -1.6000000000000001e-215Initial program 65.6%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6470.2
Applied rewrites70.2%
if -1.6000000000000001e-215 < b < 5.39999999999999998e-129Initial program 85.7%
Taylor expanded in a around inf
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sqrt-unprodN/A
lower-sqrt.f64N/A
lower-*.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6440.0
Applied rewrites40.0%
Taylor expanded in a around -inf
*-commutativeN/A
lower-fma.f64N/A
lift-/.f64N/A
sqrt-unprodN/A
lower-sqrt.f64N/A
lower-*.f64N/A
lift-/.f6440.0
Applied rewrites40.0%
Taylor expanded in a around -inf
sqrt-prodN/A
lower-sqrt.f64N/A
*-commutativeN/A
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6440.0
Applied rewrites40.0%
if 5.39999999999999998e-129 < b Initial program 21.0%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6475.6
Applied rewrites75.6%
(FPCore (a b c) :precision binary64 (if (<= b 9e-303) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 9e-303) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
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
real(8) :: tmp
if (b <= 9d-303) then
tmp = -b / a
else
tmp = -c / b
end if
code = tmp
end function
public static double code(double a, double b, double c) {
double tmp;
if (b <= 9e-303) {
tmp = -b / a;
} else {
tmp = -c / b;
}
return tmp;
}
def code(a, b, c): tmp = 0 if b <= 9e-303: tmp = -b / a else: tmp = -c / b return tmp
function code(a, b, c) tmp = 0.0 if (b <= 9e-303) tmp = Float64(Float64(-b) / a); else tmp = Float64(Float64(-c) / b); end return tmp end
function tmp_2 = code(a, b, c) tmp = 0.0; if (b <= 9e-303) tmp = -b / a; else tmp = -c / b; end tmp_2 = tmp; end
code[a_, b_, c_] := If[LessEqual[b, 9e-303], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 9 \cdot 10^{-303}:\\
\;\;\;\;\frac{-b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\
\end{array}
\end{array}
if b < 9.0000000000000002e-303Initial program 67.1%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6464.6
Applied rewrites64.6%
if 9.0000000000000002e-303 < b Initial program 33.0%
Taylor expanded in a around 0
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lower-neg.f6462.6
Applied rewrites62.6%
(FPCore (a b c) :precision binary64 (/ (- b) a))
double code(double a, double b, double c) {
return -b / 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 / 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(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 50.3%
Taylor expanded in b around -inf
associate-*r/N/A
mul-1-negN/A
lower-/.f64N/A
lift-neg.f6434.4
Applied rewrites34.4%
herbie shell --seed 2025084
(FPCore (a b c)
:name "The quadratic formula (r1)"
:precision binary64
:alt
(! :herbie-platform default (let ((d (- (* b b) (* (* 4 a) c)))) (let ((r1 (/ (+ (- b) (sqrt d)) (* 2 a)))) (let ((r2 (/ (- (- b) (sqrt d)) (* 2 a)))) (if (< b 0) r1 (/ c (* a r2)))))))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))