
(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
(if (<= d -1.02e-84)
(/ (fma (/ c d) a b) d)
(if (<= d 1.16e-108)
(/ (fma (/ d c) b a) c)
(if (<= d 1.26e+149)
(* (/ (fma (/ c b) a d) (fma d d (* c c))) b)
(/ (fma (/ a d) c b) d)))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.02e-84) {
tmp = fma((c / d), a, b) / d;
} else if (d <= 1.16e-108) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 1.26e+149) {
tmp = (fma((c / b), a, d) / fma(d, d, (c * c))) * b;
} else {
tmp = fma((a / d), c, b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.02e-84) tmp = Float64(fma(Float64(c / d), a, b) / d); elseif (d <= 1.16e-108) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 1.26e+149) tmp = Float64(Float64(fma(Float64(c / b), a, d) / fma(d, d, Float64(c * c))) * b); else tmp = Float64(fma(Float64(a / d), c, b) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.02e-84], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 1.16e-108], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.26e+149], N[(N[(N[(N[(c / b), $MachinePrecision] * a + d), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.02 \cdot 10^{-84}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{elif}\;d \leq 1.16 \cdot 10^{-108}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 1.26 \cdot 10^{+149}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{b}, a, d\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\end{array}
\end{array}
if d < -1.02000000000000004e-84Initial program 55.6%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6468.5
Applied rewrites68.5%
if -1.02000000000000004e-84 < d < 1.16e-108Initial program 70.9%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6488.3
Applied rewrites88.3%
if 1.16e-108 < d < 1.26000000000000005e149Initial program 72.6%
Taylor expanded in b around inf
Applied rewrites69.9%
if 1.26000000000000005e149 < d Initial program 30.0%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6488.6
Applied rewrites88.6%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6489.2
Applied rewrites89.2%
(FPCore (a b c d)
:precision binary64
(if (<= d -1.02e-84)
(/ (fma (/ c d) a b) d)
(if (<= d 4.3e-111)
(/ (fma (/ d c) b a) c)
(if (<= d 1.8e+126)
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
(/ (fma (/ a d) c b) d)))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.02e-84) {
tmp = fma((c / d), a, b) / d;
} else if (d <= 4.3e-111) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 1.8e+126) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = fma((a / d), c, b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.02e-84) tmp = Float64(fma(Float64(c / d), a, b) / d); elseif (d <= 4.3e-111) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 1.8e+126) tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); else tmp = Float64(fma(Float64(a / d), c, b) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.02e-84], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 4.3e-111], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.8e+126], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.02 \cdot 10^{-84}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{elif}\;d \leq 4.3 \cdot 10^{-111}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 1.8 \cdot 10^{+126}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\end{array}
\end{array}
if d < -1.02000000000000004e-84Initial program 55.6%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6468.5
Applied rewrites68.5%
if -1.02000000000000004e-84 < d < 4.29999999999999981e-111Initial program 70.9%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6488.3
Applied rewrites88.3%
if 4.29999999999999981e-111 < d < 1.8e126Initial program 73.8%
if 1.8e126 < d Initial program 32.8%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6486.4
Applied rewrites86.4%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6487.1
Applied rewrites87.1%
(FPCore (a b c d) :precision binary64 (if (<= d -1.02e-84) (/ (fma (/ c d) a b) d) (if (<= d 2.65e-12) (/ (fma (/ d c) b a) c) (/ (fma (/ a d) c b) d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.02e-84) {
tmp = fma((c / d), a, b) / d;
} else if (d <= 2.65e-12) {
tmp = fma((d / c), b, a) / c;
} else {
tmp = fma((a / d), c, b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.02e-84) tmp = Float64(fma(Float64(c / d), a, b) / d); elseif (d <= 2.65e-12) tmp = Float64(fma(Float64(d / c), b, a) / c); else tmp = Float64(fma(Float64(a / d), c, b) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.02e-84], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 2.65e-12], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.02 \cdot 10^{-84}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{elif}\;d \leq 2.65 \cdot 10^{-12}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\end{array}
\end{array}
if d < -1.02000000000000004e-84Initial program 55.6%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6468.5
Applied rewrites68.5%
if -1.02000000000000004e-84 < d < 2.64999999999999982e-12Initial program 72.1%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6484.0
Applied rewrites84.0%
if 2.64999999999999982e-12 < d Initial program 49.6%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6476.5
Applied rewrites76.5%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6477.3
Applied rewrites77.3%
(FPCore (a b c d) :precision binary64 (if (<= d -1.02e-84) (/ (fma (/ c d) a b) d) (if (<= d 2.65e-12) (/ (fma (/ b c) d a) c) (/ (fma (/ a d) c b) d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.02e-84) {
tmp = fma((c / d), a, b) / d;
} else if (d <= 2.65e-12) {
tmp = fma((b / c), d, a) / c;
} else {
tmp = fma((a / d), c, b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.02e-84) tmp = Float64(fma(Float64(c / d), a, b) / d); elseif (d <= 2.65e-12) tmp = Float64(fma(Float64(b / c), d, a) / c); else tmp = Float64(fma(Float64(a / d), c, b) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.02e-84], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 2.65e-12], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.02 \cdot 10^{-84}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{elif}\;d \leq 2.65 \cdot 10^{-12}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\end{array}
\end{array}
if d < -1.02000000000000004e-84Initial program 55.6%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6468.5
Applied rewrites68.5%
if -1.02000000000000004e-84 < d < 2.64999999999999982e-12Initial program 72.1%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6484.0
Applied rewrites84.0%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6482.1
Applied rewrites82.1%
if 2.64999999999999982e-12 < d Initial program 49.6%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6476.5
Applied rewrites76.5%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6477.3
Applied rewrites77.3%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma (/ a d) c b) d))) (if (<= d -1.02e-84) t_0 (if (<= d 2.65e-12) (/ (fma (/ b c) d a) c) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma((a / d), c, b) / d;
double tmp;
if (d <= -1.02e-84) {
tmp = t_0;
} else if (d <= 2.65e-12) {
tmp = fma((b / c), d, a) / c;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(Float64(a / d), c, b) / d) tmp = 0.0 if (d <= -1.02e-84) tmp = t_0; elseif (d <= 2.65e-12) tmp = Float64(fma(Float64(b / c), d, a) / c); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -1.02e-84], t$95$0, If[LessEqual[d, 2.65e-12], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -1.02 \cdot 10^{-84}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 2.65 \cdot 10^{-12}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -1.02000000000000004e-84 or 2.64999999999999982e-12 < d Initial program 52.9%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6472.2
Applied rewrites72.2%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6472.5
Applied rewrites72.5%
if -1.02000000000000004e-84 < d < 2.64999999999999982e-12Initial program 72.1%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6484.0
Applied rewrites84.0%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6482.1
Applied rewrites82.1%
(FPCore (a b c d) :precision binary64 (if (<= c -0.0008) (/ a c) (if (<= c 7.5e+49) (/ (fma (/ a d) c b) d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -0.0008) {
tmp = a / c;
} else if (c <= 7.5e+49) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -0.0008) tmp = Float64(a / c); elseif (c <= 7.5e+49) tmp = Float64(fma(Float64(a / d), c, b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -0.0008], N[(a / c), $MachinePrecision], If[LessEqual[c, 7.5e+49], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -0.0008:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 7.5 \cdot 10^{+49}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -8.00000000000000038e-4 or 7.4999999999999995e49 < c Initial program 47.5%
Taylor expanded in c around inf
lower-/.f6466.8
Applied rewrites66.8%
if -8.00000000000000038e-4 < c < 7.4999999999999995e49Initial program 72.1%
Taylor expanded in c around 0
pow2N/A
associate-/l/N/A
div-addN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6479.3
Applied rewrites79.3%
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/l*N/A
associate-*l/N/A
lower-fma.f64N/A
lower-/.f6477.9
Applied rewrites77.9%
(FPCore (a b c d) :precision binary64 (if (<= d -2.9e-86) (/ b d) (if (<= d 2.15e-12) (/ a c) (/ b d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -2.9e-86) {
tmp = b / d;
} else if (d <= 2.15e-12) {
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 <= (-2.9d-86)) then
tmp = b / d
else if (d <= 2.15d-12) 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 <= -2.9e-86) {
tmp = b / d;
} else if (d <= 2.15e-12) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -2.9e-86: tmp = b / d elif d <= 2.15e-12: tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if (d <= -2.9e-86) tmp = Float64(b / d); elseif (d <= 2.15e-12) 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 <= -2.9e-86) tmp = b / d; elseif (d <= 2.15e-12) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -2.9e-86], N[(b / d), $MachinePrecision], If[LessEqual[d, 2.15e-12], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.9 \cdot 10^{-86}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq 2.15 \cdot 10^{-12}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -2.8999999999999999e-86 or 2.14999999999999993e-12 < d Initial program 53.0%
Taylor expanded in c around 0
lower-/.f6460.2
Applied rewrites60.2%
if -2.8999999999999999e-86 < d < 2.14999999999999993e-12Initial program 72.1%
Taylor expanded in c around inf
lower-/.f6466.8
Applied rewrites66.8%
(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 60.9%
Taylor expanded in c around inf
lower-/.f6442.1
Applied rewrites42.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 2025130
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:alt
(! :herbie-platform c (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))))