
(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 d (* c c))) (t_1 (/ (fma b (/ d c) a) c)))
(if (<= c -5.2e+151)
t_1
(if (<= c -5.2e-15)
(* (- a) (fma (/ (- b) t_0) (/ d a) (/ (- c) t_0)))
(if (<= c 8.2e-45)
(/ (fma a (/ c d) b) d)
(if (<= c 1.3e+147) (* (fma b (/ d (* t_0 a)) (/ c t_0)) a) t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, d, (c * c));
double t_1 = fma(b, (d / c), a) / c;
double tmp;
if (c <= -5.2e+151) {
tmp = t_1;
} else if (c <= -5.2e-15) {
tmp = -a * fma((-b / t_0), (d / a), (-c / t_0));
} else if (c <= 8.2e-45) {
tmp = fma(a, (c / d), b) / d;
} else if (c <= 1.3e+147) {
tmp = fma(b, (d / (t_0 * a)), (c / t_0)) * a;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(d, d, Float64(c * c)) t_1 = Float64(fma(b, Float64(d / c), a) / c) tmp = 0.0 if (c <= -5.2e+151) tmp = t_1; elseif (c <= -5.2e-15) tmp = Float64(Float64(-a) * fma(Float64(Float64(-b) / t_0), Float64(d / a), Float64(Float64(-c) / t_0))); elseif (c <= 8.2e-45) tmp = Float64(fma(a, Float64(c / d), b) / d); elseif (c <= 1.3e+147) tmp = Float64(fma(b, Float64(d / Float64(t_0 * a)), Float64(c / t_0)) * a); else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -5.2e+151], t$95$1, If[LessEqual[c, -5.2e-15], N[((-a) * N[(N[((-b) / t$95$0), $MachinePrecision] * N[(d / a), $MachinePrecision] + N[((-c) / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 8.2e-45], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 1.3e+147], N[(N[(b * N[(d / N[(t$95$0 * a), $MachinePrecision]), $MachinePrecision] + N[(c / t$95$0), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision], t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_1 := \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -5.2 \cdot 10^{+151}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq -5.2 \cdot 10^{-15}:\\
\;\;\;\;\left(-a\right) \cdot \mathsf{fma}\left(\frac{-b}{t\_0}, \frac{d}{a}, \frac{-c}{t\_0}\right)\\
\mathbf{elif}\;c \leq 8.2 \cdot 10^{-45}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{elif}\;c \leq 1.3 \cdot 10^{+147}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{d}{t\_0 \cdot a}, \frac{c}{t\_0}\right) \cdot a\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if c < -5.20000000000000026e151 or 1.2999999999999999e147 < c Initial program 61.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.3
Applied rewrites54.3%
if -5.20000000000000026e151 < c < -5.20000000000000009e-15Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
Taylor expanded in a around -inf
associate-*r*N/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f64N/A
+-commutativeN/A
lower-+.f64N/A
Applied rewrites55.8%
lift-+.f64N/A
lift-/.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
times-fracN/A
lift-/.f64N/A
lift-neg.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
lower-fma.f64N/A
Applied rewrites51.7%
if -5.20000000000000009e-15 < c < 8.1999999999999998e-45Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
if 8.1999999999999998e-45 < c < 1.2999999999999999e147Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites57.4%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (fma d d (* c c))) (t_1 (/ (fma b (/ d c) a) c)))
(if (<= c -1.4e+110)
t_1
(if (<= c -5.2e-15)
(* (/ (fma b (/ d a) c) t_0) a)
(if (<= c 8.2e-45)
(/ (fma a (/ c d) b) d)
(if (<= c 1.3e+147) (* (fma b (/ d (* t_0 a)) (/ c t_0)) a) t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, d, (c * c));
double t_1 = fma(b, (d / c), a) / c;
double tmp;
if (c <= -1.4e+110) {
tmp = t_1;
} else if (c <= -5.2e-15) {
tmp = (fma(b, (d / a), c) / t_0) * a;
} else if (c <= 8.2e-45) {
tmp = fma(a, (c / d), b) / d;
} else if (c <= 1.3e+147) {
tmp = fma(b, (d / (t_0 * a)), (c / t_0)) * a;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(d, d, Float64(c * c)) t_1 = Float64(fma(b, Float64(d / c), a) / c) tmp = 0.0 if (c <= -1.4e+110) tmp = t_1; elseif (c <= -5.2e-15) tmp = Float64(Float64(fma(b, Float64(d / a), c) / t_0) * a); elseif (c <= 8.2e-45) tmp = Float64(fma(a, Float64(c / d), b) / d); elseif (c <= 1.3e+147) tmp = Float64(fma(b, Float64(d / Float64(t_0 * a)), Float64(c / t_0)) * a); else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -1.4e+110], t$95$1, If[LessEqual[c, -5.2e-15], N[(N[(N[(b * N[(d / a), $MachinePrecision] + c), $MachinePrecision] / t$95$0), $MachinePrecision] * a), $MachinePrecision], If[LessEqual[c, 8.2e-45], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 1.3e+147], N[(N[(b * N[(d / N[(t$95$0 * a), $MachinePrecision]), $MachinePrecision] + N[(c / t$95$0), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision], t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_1 := \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -1.4 \cdot 10^{+110}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq -5.2 \cdot 10^{-15}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{a}, c\right)}{t\_0} \cdot a\\
\mathbf{elif}\;c \leq 8.2 \cdot 10^{-45}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{elif}\;c \leq 1.3 \cdot 10^{+147}:\\
\;\;\;\;\mathsf{fma}\left(b, \frac{d}{t\_0 \cdot a}, \frac{c}{t\_0}\right) \cdot a\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if c < -1.39999999999999993e110 or 1.2999999999999999e147 < c Initial program 61.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.3
Applied rewrites54.3%
if -1.39999999999999993e110 < c < -5.20000000000000009e-15Initial program 61.4%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
associate-/r*N/A
div-add-revN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6454.1
Applied rewrites54.1%
if -5.20000000000000009e-15 < c < 8.1999999999999998e-45Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
if 8.1999999999999998e-45 < c < 1.2999999999999999e147Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites57.4%
(FPCore (a b c d)
:precision binary64
(if (<= c -1.4e+110)
(/ (fma b (/ d c) a) c)
(if (<= c -5.2e-15)
(* (/ (fma b (/ d a) c) (fma d d (* c c))) a)
(if (<= c 7e-45)
(/ (fma a (/ c d) b) d)
(if (<= c 1.55e+131)
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
(/ a c))))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1.4e+110) {
tmp = fma(b, (d / c), a) / c;
} else if (c <= -5.2e-15) {
tmp = (fma(b, (d / a), c) / fma(d, d, (c * c))) * a;
} else if (c <= 7e-45) {
tmp = fma(a, (c / d), b) / d;
} else if (c <= 1.55e+131) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -1.4e+110) tmp = Float64(fma(b, Float64(d / c), a) / c); elseif (c <= -5.2e-15) tmp = Float64(Float64(fma(b, Float64(d / a), c) / fma(d, d, Float64(c * c))) * a); elseif (c <= 7e-45) tmp = Float64(fma(a, Float64(c / d), b) / d); elseif (c <= 1.55e+131) tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -1.4e+110], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -5.2e-15], N[(N[(N[(b * N[(d / a), $MachinePrecision] + c), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision], If[LessEqual[c, 7e-45], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 1.55e+131], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.4 \cdot 10^{+110}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{elif}\;c \leq -5.2 \cdot 10^{-15}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{a}, c\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot a\\
\mathbf{elif}\;c \leq 7 \cdot 10^{-45}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{elif}\;c \leq 1.55 \cdot 10^{+131}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -1.39999999999999993e110Initial program 61.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.3
Applied rewrites54.3%
if -1.39999999999999993e110 < c < -5.20000000000000009e-15Initial program 61.4%
Taylor expanded in a around inf
*-commutativeN/A
lower-*.f64N/A
associate-/r*N/A
div-add-revN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6454.1
Applied rewrites54.1%
if -5.20000000000000009e-15 < c < 7e-45Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
if 7e-45 < c < 1.55000000000000008e131Initial program 61.4%
if 1.55000000000000008e131 < c Initial program 61.4%
Taylor expanded in c around inf
lower-/.f6442.5
Applied rewrites42.5%
(FPCore (a b c d)
:precision binary64
(if (<= d -1.45e+53)
(/ (fma a (/ c d) b) d)
(if (<= d 1.42e-158)
(/ (fma b (/ d c) a) c)
(if (<= d 6.5e+98)
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
(/ (fma c (/ a d) b) d)))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.45e+53) {
tmp = fma(a, (c / d), b) / d;
} else if (d <= 1.42e-158) {
tmp = fma(b, (d / c), a) / c;
} else if (d <= 6.5e+98) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = fma(c, (a / d), b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.45e+53) tmp = Float64(fma(a, Float64(c / d), b) / d); elseif (d <= 1.42e-158) tmp = Float64(fma(b, Float64(d / c), a) / c); elseif (d <= 6.5e+98) tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); else tmp = Float64(fma(c, Float64(a / d), b) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.45e+53], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 1.42e-158], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 6.5e+98], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(c * N[(a / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.45 \cdot 10^{+53}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{elif}\;d \leq 1.42 \cdot 10^{-158}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{elif}\;d \leq 6.5 \cdot 10^{+98}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(c, \frac{a}{d}, b\right)}{d}\\
\end{array}
\end{array}
if d < -1.4500000000000001e53Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
if -1.4500000000000001e53 < d < 1.42000000000000005e-158Initial program 61.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.3
Applied rewrites54.3%
if 1.42000000000000005e-158 < d < 6.4999999999999999e98Initial program 61.4%
if 6.4999999999999999e98 < d Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
lift-/.f64N/A
lift-fma.f64N/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6453.4
Applied rewrites53.4%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma b (/ d c) a) c))) (if (<= c -5.2e-15) t_0 (if (<= c 7.5e+39) (/ (fma a (/ c d) b) d) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(b, (d / c), a) / c;
double tmp;
if (c <= -5.2e-15) {
tmp = t_0;
} else if (c <= 7.5e+39) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(b, Float64(d / c), a) / c) tmp = 0.0 if (c <= -5.2e-15) tmp = t_0; elseif (c <= 7.5e+39) tmp = Float64(fma(a, Float64(c / d), b) / d); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -5.2e-15], t$95$0, If[LessEqual[c, 7.5e+39], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -5.2 \cdot 10^{-15}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 7.5 \cdot 10^{+39}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -5.20000000000000009e-15 or 7.5000000000000005e39 < c Initial program 61.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.3
Applied rewrites54.3%
if -5.20000000000000009e-15 < c < 7.5000000000000005e39Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
(FPCore (a b c d) :precision binary64 (if (<= c -5.5e-15) (/ a c) (if (<= c 2.9e+113) (/ (fma a (/ c d) b) d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -5.5e-15) {
tmp = a / c;
} else if (c <= 2.9e+113) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -5.5e-15) tmp = Float64(a / c); elseif (c <= 2.9e+113) tmp = Float64(fma(a, Float64(c / d), b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -5.5e-15], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.9e+113], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -5.5 \cdot 10^{-15}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.9 \cdot 10^{+113}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -5.5000000000000002e-15 or 2.89999999999999984e113 < c Initial program 61.4%
Taylor expanded in c around inf
lower-/.f6442.5
Applied rewrites42.5%
if -5.5000000000000002e-15 < c < 2.89999999999999984e113Initial program 61.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6454.1
Applied rewrites54.1%
(FPCore (a b c d) :precision binary64 (if (<= c -5.2e-15) (/ a c) (if (<= c 2.9e+113) (/ b d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -5.2e-15) {
tmp = a / c;
} else if (c <= 2.9e+113) {
tmp = b / d;
} else {
tmp = a / c;
}
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 <= (-5.2d-15)) then
tmp = a / c
else if (c <= 2.9d+113) then
tmp = b / d
else
tmp = a / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (c <= -5.2e-15) {
tmp = a / c;
} else if (c <= 2.9e+113) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if c <= -5.2e-15: tmp = a / c elif c <= 2.9e+113: tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if (c <= -5.2e-15) tmp = Float64(a / c); elseif (c <= 2.9e+113) tmp = Float64(b / d); else tmp = Float64(a / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (c <= -5.2e-15) tmp = a / c; elseif (c <= 2.9e+113) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[c, -5.2e-15], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.9e+113], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -5.2 \cdot 10^{-15}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.9 \cdot 10^{+113}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -5.20000000000000009e-15 or 2.89999999999999984e113 < c Initial program 61.4%
Taylor expanded in c around inf
lower-/.f6442.5
Applied rewrites42.5%
if -5.20000000000000009e-15 < c < 2.89999999999999984e113Initial program 61.4%
Taylor expanded in c around 0
lower-/.f6442.6
Applied rewrites42.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 61.4%
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 2025135
(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))))