
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
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, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
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, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma d b (* c a)) (fma d d (* c c)))))
(if (<= d -8.6e+76)
(/ (+ (fma (/ c d) a (- (* b (pow (/ c d) 2.0)))) b) d)
(if (<= d -9.6e-160)
t_0
(if (<= d 4.7e-79)
(- (/ (fma (/ (fma d b (- (/ (* (* d d) a) c))) c) -1.0 (- a)) c))
(if (<= d 8.6e+102) t_0 (/ (fma a (/ c d) b) d)))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, b, (c * a)) / fma(d, d, (c * c));
double tmp;
if (d <= -8.6e+76) {
tmp = (fma((c / d), a, -(b * pow((c / d), 2.0))) + b) / d;
} else if (d <= -9.6e-160) {
tmp = t_0;
} else if (d <= 4.7e-79) {
tmp = -(fma((fma(d, b, -(((d * d) * a) / c)) / c), -1.0, -a) / c);
} else if (d <= 8.6e+102) {
tmp = t_0;
} else {
tmp = fma(a, (c / d), b) / d;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, b, Float64(c * a)) / fma(d, d, Float64(c * c))) tmp = 0.0 if (d <= -8.6e+76) tmp = Float64(Float64(fma(Float64(c / d), a, Float64(-Float64(b * (Float64(c / d) ^ 2.0)))) + b) / d); elseif (d <= -9.6e-160) tmp = t_0; elseif (d <= 4.7e-79) tmp = Float64(-Float64(fma(Float64(fma(d, b, Float64(-Float64(Float64(Float64(d * d) * a) / c))) / c), -1.0, Float64(-a)) / c)); elseif (d <= 8.6e+102) tmp = t_0; else tmp = Float64(fma(a, Float64(c / d), b) / d); end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -8.6e+76], N[(N[(N[(N[(c / d), $MachinePrecision] * a + (-N[(b * N[Power[N[(c / d), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision])), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -9.6e-160], t$95$0, If[LessEqual[d, 4.7e-79], (-N[(N[(N[(N[(d * b + (-N[(N[(N[(d * d), $MachinePrecision] * a), $MachinePrecision] / c), $MachinePrecision])), $MachinePrecision] / c), $MachinePrecision] * -1.0 + (-a)), $MachinePrecision] / c), $MachinePrecision]), If[LessEqual[d, 8.6e+102], t$95$0, N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{if}\;d \leq -8.6 \cdot 10^{+76}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, -b \cdot {\left(\frac{c}{d}\right)}^{2}\right) + b}{d}\\
\mathbf{elif}\;d \leq -9.6 \cdot 10^{-160}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 4.7 \cdot 10^{-79}:\\
\;\;\;\;-\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(d, b, -\frac{\left(d \cdot d\right) \cdot a}{c}\right)}{c}, -1, -a\right)}{c}\\
\mathbf{elif}\;d \leq 8.6 \cdot 10^{+102}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\end{array}
\end{array}
if d < -8.59999999999999957e76Initial program 42.6%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6442.6
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6442.6
Applied rewrites42.6%
Taylor expanded in d around inf
+-commutativeN/A
*-commutativeN/A
*-commutativeN/A
pow2N/A
pow2N/A
+-commutativeN/A
pow2N/A
pow2N/A
lower-/.f64N/A
Applied rewrites80.8%
if -8.59999999999999957e76 < d < -9.59999999999999964e-160 or 4.7000000000000002e-79 < d < 8.6000000000000002e102Initial program 76.6%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6476.6
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6476.6
Applied rewrites76.6%
if -9.59999999999999964e-160 < d < 4.7000000000000002e-79Initial program 72.1%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
Applied rewrites88.8%
if 8.6000000000000002e102 < d Initial program 40.0%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6483.5
Applied rewrites83.5%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma d b (* c a)) (fma d d (* c c))))
(t_1 (/ (fma a (/ c d) b) d)))
(if (<= d -8.6e+76)
t_1
(if (<= d -9.6e-160)
t_0
(if (<= d 4.7e-79)
(- (/ (fma (/ (fma d b (- (/ (* (* d d) a) c))) c) -1.0 (- a)) c))
(if (<= d 8.6e+102) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, b, (c * a)) / fma(d, d, (c * c));
double t_1 = fma(a, (c / d), b) / d;
double tmp;
if (d <= -8.6e+76) {
tmp = t_1;
} else if (d <= -9.6e-160) {
tmp = t_0;
} else if (d <= 4.7e-79) {
tmp = -(fma((fma(d, b, -(((d * d) * a) / c)) / c), -1.0, -a) / c);
} else if (d <= 8.6e+102) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, b, Float64(c * a)) / fma(d, d, Float64(c * c))) t_1 = Float64(fma(a, Float64(c / d), b) / d) tmp = 0.0 if (d <= -8.6e+76) tmp = t_1; elseif (d <= -9.6e-160) tmp = t_0; elseif (d <= 4.7e-79) tmp = Float64(-Float64(fma(Float64(fma(d, b, Float64(-Float64(Float64(Float64(d * d) * a) / c))) / c), -1.0, Float64(-a)) / c)); elseif (d <= 8.6e+102) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -8.6e+76], t$95$1, If[LessEqual[d, -9.6e-160], t$95$0, If[LessEqual[d, 4.7e-79], (-N[(N[(N[(N[(d * b + (-N[(N[(N[(d * d), $MachinePrecision] * a), $MachinePrecision] / c), $MachinePrecision])), $MachinePrecision] / c), $MachinePrecision] * -1.0 + (-a)), $MachinePrecision] / c), $MachinePrecision]), If[LessEqual[d, 8.6e+102], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
t_1 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -8.6 \cdot 10^{+76}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -9.6 \cdot 10^{-160}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 4.7 \cdot 10^{-79}:\\
\;\;\;\;-\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(d, b, -\frac{\left(d \cdot d\right) \cdot a}{c}\right)}{c}, -1, -a\right)}{c}\\
\mathbf{elif}\;d \leq 8.6 \cdot 10^{+102}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -8.59999999999999957e76 or 8.6000000000000002e102 < d Initial program 41.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6482.3
Applied rewrites82.3%
if -8.59999999999999957e76 < d < -9.59999999999999964e-160 or 4.7000000000000002e-79 < d < 8.6000000000000002e102Initial program 76.6%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6476.6
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6476.6
Applied rewrites76.6%
if -9.59999999999999964e-160 < d < 4.7000000000000002e-79Initial program 72.1%
Taylor expanded in c around -inf
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
Applied rewrites88.8%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma d b (* c a)) (fma d d (* c c))))
(t_1 (/ (fma a (/ c d) b) d)))
(if (<= d -8.6e+76)
t_1
(if (<= d -6.6e-157)
t_0
(if (<= d 4.7e-79)
(/ (fma b (/ d c) a) c)
(if (<= d 8.6e+102) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, b, (c * a)) / fma(d, d, (c * c));
double t_1 = fma(a, (c / d), b) / d;
double tmp;
if (d <= -8.6e+76) {
tmp = t_1;
} else if (d <= -6.6e-157) {
tmp = t_0;
} else if (d <= 4.7e-79) {
tmp = fma(b, (d / c), a) / c;
} else if (d <= 8.6e+102) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, b, Float64(c * a)) / fma(d, d, Float64(c * c))) t_1 = Float64(fma(a, Float64(c / d), b) / d) tmp = 0.0 if (d <= -8.6e+76) tmp = t_1; elseif (d <= -6.6e-157) tmp = t_0; elseif (d <= 4.7e-79) tmp = Float64(fma(b, Float64(d / c), a) / c); elseif (d <= 8.6e+102) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -8.6e+76], t$95$1, If[LessEqual[d, -6.6e-157], t$95$0, If[LessEqual[d, 4.7e-79], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 8.6e+102], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
t_1 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -8.6 \cdot 10^{+76}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -6.6 \cdot 10^{-157}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 4.7 \cdot 10^{-79}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{elif}\;d \leq 8.6 \cdot 10^{+102}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -8.59999999999999957e76 or 8.6000000000000002e102 < d Initial program 41.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6482.3
Applied rewrites82.3%
if -8.59999999999999957e76 < d < -6.59999999999999998e-157 or 4.7000000000000002e-79 < d < 8.6000000000000002e102Initial program 76.6%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6476.6
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
pow2N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6476.6
Applied rewrites76.6%
if -6.59999999999999998e-157 < d < 4.7000000000000002e-79Initial program 72.1%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6489.4
Applied rewrites89.4%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (* b (/ d (fma d d (* c c))))))
(if (<= d -5.4e+101)
(/ b d)
(if (<= d -8.5e-136)
t_0
(if (<= d 3e-101) (/ a c) (if (<= d 2.2e+139) t_0 (/ b d)))))))
double code(double a, double b, double c, double d) {
double t_0 = b * (d / fma(d, d, (c * c)));
double tmp;
if (d <= -5.4e+101) {
tmp = b / d;
} else if (d <= -8.5e-136) {
tmp = t_0;
} else if (d <= 3e-101) {
tmp = a / c;
} else if (d <= 2.2e+139) {
tmp = t_0;
} else {
tmp = b / d;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(b * Float64(d / fma(d, d, Float64(c * c)))) tmp = 0.0 if (d <= -5.4e+101) tmp = Float64(b / d); elseif (d <= -8.5e-136) tmp = t_0; elseif (d <= 3e-101) tmp = Float64(a / c); elseif (d <= 2.2e+139) tmp = t_0; else tmp = Float64(b / d); end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(b * N[(d / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -5.4e+101], N[(b / d), $MachinePrecision], If[LessEqual[d, -8.5e-136], t$95$0, If[LessEqual[d, 3e-101], N[(a / c), $MachinePrecision], If[LessEqual[d, 2.2e+139], t$95$0, N[(b / d), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := b \cdot \frac{d}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{if}\;d \leq -5.4 \cdot 10^{+101}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq -8.5 \cdot 10^{-136}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 3 \cdot 10^{-101}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;d \leq 2.2 \cdot 10^{+139}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -5.40000000000000012e101 or 2.1999999999999999e139 < d Initial program 36.6%
Taylor expanded in c around 0
lower-/.f6473.5
Applied rewrites73.5%
if -5.40000000000000012e101 < d < -8.49999999999999973e-136 or 3.0000000000000003e-101 < d < 2.1999999999999999e139Initial program 75.8%
Taylor expanded in a around 0
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6455.1
Applied rewrites55.1%
if -8.49999999999999973e-136 < d < 3.0000000000000003e-101Initial program 71.9%
Taylor expanded in c around inf
lower-/.f6471.4
Applied rewrites71.4%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma a (/ c d) b) d)))
(if (<= d -2e-34)
t_0
(if (<= d -2.8e-133)
(/ (fma a c (* b d)) (* c c))
(if (<= d 2.75e-67) (/ a c) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = fma(a, (c / d), b) / d;
double tmp;
if (d <= -2e-34) {
tmp = t_0;
} else if (d <= -2.8e-133) {
tmp = fma(a, c, (b * d)) / (c * c);
} else if (d <= 2.75e-67) {
tmp = a / c;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(a, Float64(c / d), b) / d) tmp = 0.0 if (d <= -2e-34) tmp = t_0; elseif (d <= -2.8e-133) tmp = Float64(fma(a, c, Float64(b * d)) / Float64(c * c)); elseif (d <= 2.75e-67) tmp = Float64(a / c); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -2e-34], t$95$0, If[LessEqual[d, -2.8e-133], N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 2.75e-67], N[(a / c), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -2 \cdot 10^{-34}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq -2.8 \cdot 10^{-133}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}\\
\mathbf{elif}\;d \leq 2.75 \cdot 10^{-67}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -1.99999999999999986e-34 or 2.7500000000000001e-67 < d Initial program 54.7%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6471.0
Applied rewrites71.0%
if -1.99999999999999986e-34 < d < -2.7999999999999999e-133Initial program 80.0%
Taylor expanded in c around inf
pow2N/A
lift-*.f6452.5
Applied rewrites52.5%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f6452.5
Applied rewrites52.5%
if -2.7999999999999999e-133 < d < 2.7500000000000001e-67Initial program 72.6%
Taylor expanded in c around inf
lower-/.f6469.4
Applied rewrites69.4%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma a (/ c d) b) d))) (if (<= d -1.65e-32) t_0 (if (<= d 9.5e-67) (/ (fma b (/ d c) a) c) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(a, (c / d), b) / d;
double tmp;
if (d <= -1.65e-32) {
tmp = t_0;
} else if (d <= 9.5e-67) {
tmp = fma(b, (d / c), a) / c;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(a, Float64(c / d), b) / d) tmp = 0.0 if (d <= -1.65e-32) tmp = t_0; elseif (d <= 9.5e-67) tmp = Float64(fma(b, Float64(d / c), a) / c); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -1.65e-32], t$95$0, If[LessEqual[d, 9.5e-67], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -1.65 \cdot 10^{-32}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 9.5 \cdot 10^{-67}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -1.65000000000000013e-32 or 9.4999999999999994e-67 < d Initial program 54.7%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6471.2
Applied rewrites71.2%
if -1.65000000000000013e-32 < d < 9.4999999999999994e-67Initial program 74.0%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6485.0
Applied rewrites85.0%
(FPCore (a b c d) :precision binary64 (if (<= d -6.8e+40) (/ b d) (if (<= d 9.5e-67) (/ a c) (/ b d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -6.8e+40) {
tmp = b / d;
} else if (d <= 9.5e-67) {
tmp = a / c;
} else {
tmp = b / d;
}
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, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if (d <= (-6.8d+40)) then
tmp = b / d
else if (d <= 9.5d-67) then
tmp = a / c
else
tmp = b / d
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (d <= -6.8e+40) {
tmp = b / d;
} else if (d <= 9.5e-67) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -6.8e+40: tmp = b / d elif d <= 9.5e-67: tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if (d <= -6.8e+40) tmp = Float64(b / d); elseif (d <= 9.5e-67) tmp = Float64(a / c); else tmp = Float64(b / d); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (d <= -6.8e+40) tmp = b / d; elseif (d <= 9.5e-67) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -6.8e+40], N[(b / d), $MachinePrecision], If[LessEqual[d, 9.5e-67], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -6.8 \cdot 10^{+40}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq 9.5 \cdot 10^{-67}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -6.79999999999999977e40 or 9.4999999999999994e-67 < d Initial program 51.8%
Taylor expanded in c around 0
lower-/.f6461.5
Applied rewrites61.5%
if -6.79999999999999977e40 < d < 9.4999999999999994e-67Initial program 74.7%
Taylor expanded in c around inf
lower-/.f6462.3
Applied rewrites62.3%
(FPCore (a b c d) :precision binary64 (/ a c))
double code(double a, double b, double c, double d) {
return a / c;
}
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, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a / c
end function
public static double code(double a, double b, double c, double d) {
return a / c;
}
def code(a, b, c, d): return a / c
function code(a, b, c, d) return Float64(a / c) end
function tmp = code(a, b, c, d) tmp = a / c; end
code[a_, b_, c_, d_] := N[(a / c), $MachinePrecision]
\begin{array}{l}
\\
\frac{a}{c}
\end{array}
Initial program 62.9%
Taylor expanded in c around inf
lower-/.f6442.5
Applied rewrites42.5%
(FPCore (a b c d) :precision binary64 (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (fabs(d) < fabs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
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, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if (abs(d) < abs(c)) then
tmp = (a + (b * (d / c))) / (c + (d * (d / c)))
else
tmp = (b + (a * (c / d))) / (d + (c * (c / d)))
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (Math.abs(d) < Math.abs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if math.fabs(d) < math.fabs(c): tmp = (a + (b * (d / c))) / (c + (d * (d / c))) else: tmp = (b + (a * (c / d))) / (d + (c * (c / d))) return tmp
function code(a, b, c, d) tmp = 0.0 if (abs(d) < abs(c)) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / Float64(c + Float64(d * Float64(d / c)))); else tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / Float64(d + Float64(c * Float64(c / d)))); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (abs(d) < abs(c)) tmp = (a + (b * (d / c))) / (c + (d * (d / c))); else tmp = (b + (a * (c / d))) / (d + (c * (c / d))); end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Less[N[Abs[d], $MachinePrecision], N[Abs[c], $MachinePrecision]], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c + N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(d + N[(c * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|d\right| < \left|c\right|:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\
\end{array}
\end{array}
herbie shell --seed 2025105
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:alt
(! :herbie-platform default (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))