
(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}
Sampling outcomes in binary64 precision:
Herbie found 7 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))))
(t_1 (/ (fma c (/ a d) b) d)))
(if (<= d -5.8e+106)
t_1
(if (<= d -6.8e-109)
t_0
(if (<= d 8.5e-149)
(/ (fma (/ d c) b a) c)
(if (<= d 6e+84) 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(c, (a / d), b) / d;
double tmp;
if (d <= -5.8e+106) {
tmp = t_1;
} else if (d <= -6.8e-109) {
tmp = t_0;
} else if (d <= 8.5e-149) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 6e+84) {
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(c, Float64(a / d), b) / d) tmp = 0.0 if (d <= -5.8e+106) tmp = t_1; elseif (d <= -6.8e-109) tmp = t_0; elseif (d <= 8.5e-149) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 6e+84) 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[(c * N[(a / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -5.8e+106], t$95$1, If[LessEqual[d, -6.8e-109], t$95$0, If[LessEqual[d, 8.5e-149], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 6e+84], 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(c, \frac{a}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -5.8 \cdot 10^{+106}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -6.8 \cdot 10^{-109}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 8.5 \cdot 10^{-149}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 6 \cdot 10^{+84}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -5.8000000000000004e106 or 5.99999999999999992e84 < d Initial program 39.5%
Taylor expanded in c around 0
Applied rewrites80.7%
Applied rewrites82.8%
if -5.8000000000000004e106 < d < -6.80000000000000023e-109 or 8.5000000000000006e-149 < d < 5.99999999999999992e84Initial program 83.5%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6483.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6483.6
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6483.6
Applied rewrites83.6%
if -6.80000000000000023e-109 < d < 8.5000000000000006e-149Initial program 65.9%
Taylor expanded in c around inf
Applied rewrites95.1%
(FPCore (a b c d)
:precision binary64
(if (<= c -6e+154)
(/ a c)
(if (<= c -7.2e+62)
(/ (fma d b (* c a)) (* c c))
(if (<= c 2.7e+88) (/ (fma (/ c d) a b) d) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -6e+154) {
tmp = a / c;
} else if (c <= -7.2e+62) {
tmp = fma(d, b, (c * a)) / (c * c);
} else if (c <= 2.7e+88) {
tmp = fma((c / d), a, b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -6e+154) tmp = Float64(a / c); elseif (c <= -7.2e+62) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(c * c)); elseif (c <= 2.7e+88) tmp = Float64(fma(Float64(c / d), a, b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -6e+154], N[(a / c), $MachinePrecision], If[LessEqual[c, -7.2e+62], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.7e+88], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -6 \cdot 10^{+154}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -7.2 \cdot 10^{+62}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c}\\
\mathbf{elif}\;c \leq 2.7 \cdot 10^{+88}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -6.00000000000000052e154 or 2.70000000000000016e88 < c Initial program 39.9%
Taylor expanded in c around inf
Applied rewrites81.6%
if -6.00000000000000052e154 < c < -7.2e62Initial program 81.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6481.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6481.7
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6481.7
Applied rewrites81.7%
Taylor expanded in c around inf
Applied rewrites69.3%
if -7.2e62 < c < 2.70000000000000016e88Initial program 75.0%
Taylor expanded in c around 0
Applied rewrites75.6%
(FPCore (a b c d)
:precision binary64
(if (<= c -6e+154)
(/ a c)
(if (<= c -7.2e+62)
(/ (fma d b (* c a)) (* c c))
(if (<= c 2.7e+88) (/ (fma c (/ a d) b) d) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -6e+154) {
tmp = a / c;
} else if (c <= -7.2e+62) {
tmp = fma(d, b, (c * a)) / (c * c);
} else if (c <= 2.7e+88) {
tmp = fma(c, (a / d), b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -6e+154) tmp = Float64(a / c); elseif (c <= -7.2e+62) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(c * c)); elseif (c <= 2.7e+88) tmp = Float64(fma(c, Float64(a / d), b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -6e+154], N[(a / c), $MachinePrecision], If[LessEqual[c, -7.2e+62], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.7e+88], N[(N[(c * N[(a / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -6 \cdot 10^{+154}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -7.2 \cdot 10^{+62}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c}\\
\mathbf{elif}\;c \leq 2.7 \cdot 10^{+88}:\\
\;\;\;\;\frac{\mathsf{fma}\left(c, \frac{a}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -6.00000000000000052e154 or 2.70000000000000016e88 < c Initial program 39.9%
Taylor expanded in c around inf
Applied rewrites81.6%
if -6.00000000000000052e154 < c < -7.2e62Initial program 81.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6481.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6481.7
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6481.7
Applied rewrites81.7%
Taylor expanded in c around inf
Applied rewrites69.3%
if -7.2e62 < c < 2.70000000000000016e88Initial program 75.0%
Taylor expanded in c around 0
Applied rewrites75.6%
Applied rewrites74.5%
(FPCore (a b c d) :precision binary64 (if (or (<= d -2.55e-22) (not (<= d 2.65e+44))) (/ (fma c (/ a d) b) d) (/ (fma (/ d c) b a) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.55e-22) || !(d <= 2.65e+44)) {
tmp = fma(c, (a / d), b) / d;
} else {
tmp = fma((d / c), b, a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -2.55e-22) || !(d <= 2.65e+44)) tmp = Float64(fma(c, Float64(a / d), b) / d); else tmp = Float64(fma(Float64(d / c), b, a) / c); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.55e-22], N[Not[LessEqual[d, 2.65e+44]], $MachinePrecision]], N[(N[(c * N[(a / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.55 \cdot 10^{-22} \lor \neg \left(d \leq 2.65 \cdot 10^{+44}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(c, \frac{a}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\end{array}
\end{array}
if d < -2.55000000000000011e-22 or 2.65e44 < d Initial program 49.8%
Taylor expanded in c around 0
Applied rewrites75.8%
Applied rewrites77.4%
if -2.55000000000000011e-22 < d < 2.65e44Initial program 75.3%
Taylor expanded in c around inf
Applied rewrites83.7%
Final simplification80.9%
(FPCore (a b c d)
:precision binary64
(if (<= c -6e+154)
(/ a c)
(if (<= c -1.72e+41)
(/ (fma d b (* c a)) (* c c))
(if (<= c 2.7e+88) (/ b d) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -6e+154) {
tmp = a / c;
} else if (c <= -1.72e+41) {
tmp = fma(d, b, (c * a)) / (c * c);
} else if (c <= 2.7e+88) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -6e+154) tmp = Float64(a / c); elseif (c <= -1.72e+41) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(c * c)); elseif (c <= 2.7e+88) tmp = Float64(b / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -6e+154], N[(a / c), $MachinePrecision], If[LessEqual[c, -1.72e+41], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.7e+88], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -6 \cdot 10^{+154}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -1.72 \cdot 10^{+41}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c}\\
\mathbf{elif}\;c \leq 2.7 \cdot 10^{+88}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -6.00000000000000052e154 or 2.70000000000000016e88 < c Initial program 39.9%
Taylor expanded in c around inf
Applied rewrites81.6%
if -6.00000000000000052e154 < c < -1.7200000000000001e41Initial program 81.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6481.4
lift-*.f64N/A
*-commutativeN/A
lower-*.f6481.4
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6481.4
Applied rewrites81.4%
Taylor expanded in c around inf
Applied rewrites63.6%
if -1.7200000000000001e41 < c < 2.70000000000000016e88Initial program 74.8%
Taylor expanded in c around 0
Applied rewrites63.4%
(FPCore (a b c d) :precision binary64 (if (or (<= c -2.2e+41) (not (<= c 2.7e+88))) (/ a c) (/ b d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -2.2e+41) || !(c <= 2.7e+88)) {
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 ((c <= (-2.2d+41)) .or. (.not. (c <= 2.7d+88))) 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 ((c <= -2.2e+41) || !(c <= 2.7e+88)) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (c <= -2.2e+41) or not (c <= 2.7e+88): tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if ((c <= -2.2e+41) || !(c <= 2.7e+88)) 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 ((c <= -2.2e+41) || ~((c <= 2.7e+88))) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -2.2e+41], N[Not[LessEqual[c, 2.7e+88]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -2.2 \cdot 10^{+41} \lor \neg \left(c \leq 2.7 \cdot 10^{+88}\right):\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if c < -2.1999999999999999e41 or 2.70000000000000016e88 < c Initial program 49.0%
Taylor expanded in c around inf
Applied rewrites73.2%
if -2.1999999999999999e41 < c < 2.70000000000000016e88Initial program 74.8%
Taylor expanded in c around 0
Applied rewrites63.4%
Final simplification67.6%
(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 63.8%
Taylor expanded in c around inf
Applied rewrites44.1%
(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 2025019
(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))))