
(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 10 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 (/ a d) c b) d)))
(if (<= d -1.25e+104)
t_0
(if (<= d -8.5e-160)
(/ (fma d b (* c a)) (fma d d (* c c)))
(if (<= d 1e+24) (/ (fma (/ d c) b 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.25e+104) {
tmp = t_0;
} else if (d <= -8.5e-160) {
tmp = fma(d, b, (c * a)) / fma(d, d, (c * c));
} else if (d <= 1e+24) {
tmp = fma((d / c), b, 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.25e+104) tmp = t_0; elseif (d <= -8.5e-160) tmp = Float64(fma(d, b, Float64(c * a)) / fma(d, d, Float64(c * c))); elseif (d <= 1e+24) tmp = Float64(fma(Float64(d / c), b, 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.25e+104], t$95$0, If[LessEqual[d, -8.5e-160], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1e+24], N[(N[(N[(d / c), $MachinePrecision] * b + 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.25 \cdot 10^{+104}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq -8.5 \cdot 10^{-160}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{elif}\;d \leq 10^{+24}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -1.2499999999999999e104 or 9.9999999999999998e23 < d Initial program 38.5%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6485.8
Applied rewrites85.8%
if -1.2499999999999999e104 < d < -8.49999999999999959e-160Initial program 79.6%
Applied rewrites79.6%
if -8.49999999999999959e-160 < d < 9.9999999999999998e23Initial program 67.5%
Applied rewrites67.5%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6488.5
Applied rewrites88.5%
Final simplification85.5%
(FPCore (a b c d)
:precision binary64
(if (<= c -4e+76)
(/ a c)
(if (<= c -2.3e-127)
(* (/ d (fma c c (* d d))) b)
(if (<= c 4.1e+26)
(/ b d)
(if (<= c 9e+157) (* a (/ c (fma d d (* c c)))) (/ a c))))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -4e+76) {
tmp = a / c;
} else if (c <= -2.3e-127) {
tmp = (d / fma(c, c, (d * d))) * b;
} else if (c <= 4.1e+26) {
tmp = b / d;
} else if (c <= 9e+157) {
tmp = a * (c / fma(d, d, (c * c)));
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -4e+76) tmp = Float64(a / c); elseif (c <= -2.3e-127) tmp = Float64(Float64(d / fma(c, c, Float64(d * d))) * b); elseif (c <= 4.1e+26) tmp = Float64(b / d); elseif (c <= 9e+157) tmp = Float64(a * Float64(c / fma(d, d, Float64(c * c)))); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -4e+76], N[(a / c), $MachinePrecision], If[LessEqual[c, -2.3e-127], N[(N[(d / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision], If[LessEqual[c, 4.1e+26], N[(b / d), $MachinePrecision], If[LessEqual[c, 9e+157], N[(a * N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -4 \cdot 10^{+76}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -2.3 \cdot 10^{-127}:\\
\;\;\;\;\frac{d}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot b\\
\mathbf{elif}\;c \leq 4.1 \cdot 10^{+26}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;c \leq 9 \cdot 10^{+157}:\\
\;\;\;\;a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -4.0000000000000002e76 or 8.9999999999999997e157 < c Initial program 33.7%
Taylor expanded in c around inf
lower-/.f6474.2
Applied rewrites74.2%
if -4.0000000000000002e76 < c < -2.30000000000000019e-127Initial program 80.2%
Applied rewrites80.2%
Taylor expanded in a around 0
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6455.6
Applied rewrites55.6%
if -2.30000000000000019e-127 < c < 4.09999999999999983e26Initial program 66.7%
Taylor expanded in c around 0
lower-/.f6469.4
Applied rewrites69.4%
if 4.09999999999999983e26 < c < 8.9999999999999997e157Initial program 66.8%
Taylor expanded in a around inf
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6462.9
Applied rewrites62.9%
Applied rewrites64.7%
Final simplification68.2%
(FPCore (a b c d)
:precision binary64
(if (<= c -1.45e+36)
(/ a c)
(if (<= c 4.1e+26)
(/ b d)
(if (<= c 9e+157) (* a (/ c (fma d d (* c c)))) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1.45e+36) {
tmp = a / c;
} else if (c <= 4.1e+26) {
tmp = b / d;
} else if (c <= 9e+157) {
tmp = a * (c / fma(d, d, (c * c)));
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -1.45e+36) tmp = Float64(a / c); elseif (c <= 4.1e+26) tmp = Float64(b / d); elseif (c <= 9e+157) tmp = Float64(a * Float64(c / fma(d, d, Float64(c * c)))); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -1.45e+36], N[(a / c), $MachinePrecision], If[LessEqual[c, 4.1e+26], N[(b / d), $MachinePrecision], If[LessEqual[c, 9e+157], N[(a * N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.45 \cdot 10^{+36}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 4.1 \cdot 10^{+26}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;c \leq 9 \cdot 10^{+157}:\\
\;\;\;\;a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -1.45e36 or 8.9999999999999997e157 < c Initial program 39.7%
Taylor expanded in c around inf
lower-/.f6469.8
Applied rewrites69.8%
if -1.45e36 < c < 4.09999999999999983e26Initial program 69.3%
Taylor expanded in c around 0
lower-/.f6463.1
Applied rewrites63.1%
if 4.09999999999999983e26 < c < 8.9999999999999997e157Initial program 66.8%
Taylor expanded in a around inf
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6462.9
Applied rewrites62.9%
Applied rewrites64.7%
Final simplification65.6%
(FPCore (a b c d) :precision binary64 (if (or (<= d -4.8e-8) (not (<= d 1e+24))) (/ (fma (/ a d) c b) d) (/ (fma (/ d c) b a) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -4.8e-8) || !(d <= 1e+24)) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = fma((d / c), b, a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -4.8e-8) || !(d <= 1e+24)) tmp = Float64(fma(Float64(a / d), c, 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, -4.8e-8], N[Not[LessEqual[d, 1e+24]], $MachinePrecision]], N[(N[(N[(a / d), $MachinePrecision] * c + 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 -4.8 \cdot 10^{-8} \lor \neg \left(d \leq 10^{+24}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\end{array}
\end{array}
if d < -4.79999999999999997e-8 or 9.9999999999999998e23 < d Initial program 46.5%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.1
Applied rewrites82.1%
if -4.79999999999999997e-8 < d < 9.9999999999999998e23Initial program 70.8%
Applied rewrites70.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.5
Applied rewrites82.5%
Final simplification82.3%
(FPCore (a b c d) :precision binary64 (if (or (<= d -4.8e-8) (not (<= d 1e+24))) (/ (fma (/ a d) c b) d) (/ (fma (/ b c) d a) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -4.8e-8) || !(d <= 1e+24)) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = fma((b / c), d, a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -4.8e-8) || !(d <= 1e+24)) tmp = Float64(fma(Float64(a / d), c, b) / d); else tmp = Float64(fma(Float64(b / c), d, a) / c); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -4.8e-8], N[Not[LessEqual[d, 1e+24]], $MachinePrecision]], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -4.8 \cdot 10^{-8} \lor \neg \left(d \leq 10^{+24}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\end{array}
\end{array}
if d < -4.79999999999999997e-8 or 9.9999999999999998e23 < d Initial program 46.5%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.1
Applied rewrites82.1%
if -4.79999999999999997e-8 < d < 9.9999999999999998e23Initial program 70.8%
Taylor expanded in c around inf
lower-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
associate-*r*N/A
distribute-lft-outN/A
+-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
mul-1-negN/A
mul-1-negN/A
remove-double-negN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
*-commutativeN/A
mul-1-negN/A
mul-1-negN/A
remove-double-negN/A
lower-fma.f64N/A
lower-/.f6481.0
Applied rewrites81.0%
Final simplification81.5%
(FPCore (a b c d) :precision binary64 (if (or (<= c -3.5e+74) (not (<= c 2.2e+31))) (/ a c) (/ (fma (/ a d) c b) d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -3.5e+74) || !(c <= 2.2e+31)) {
tmp = a / c;
} else {
tmp = fma((a / d), c, b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((c <= -3.5e+74) || !(c <= 2.2e+31)) tmp = Float64(a / c); else tmp = Float64(fma(Float64(a / d), c, b) / d); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -3.5e+74], N[Not[LessEqual[c, 2.2e+31]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3.5 \cdot 10^{+74} \lor \neg \left(c \leq 2.2 \cdot 10^{+31}\right):\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\end{array}
\end{array}
if c < -3.50000000000000014e74 or 2.2000000000000001e31 < c Initial program 42.4%
Taylor expanded in c around inf
lower-/.f6467.7
Applied rewrites67.7%
if -3.50000000000000014e74 < c < 2.2000000000000001e31Initial program 70.4%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6479.7
Applied rewrites79.7%
Final simplification74.7%
(FPCore (a b c d) :precision binary64 (if (<= d -4.8e-8) (/ (+ (* (/ a d) c) b) d) (if (<= d 1e+24) (/ (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 <= -4.8e-8) {
tmp = (((a / d) * c) + b) / d;
} else if (d <= 1e+24) {
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 <= -4.8e-8) tmp = Float64(Float64(Float64(Float64(a / d) * c) + b) / d); elseif (d <= 1e+24) 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, -4.8e-8], N[(N[(N[(N[(a / d), $MachinePrecision] * c), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 1e+24], 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 -4.8 \cdot 10^{-8}:\\
\;\;\;\;\frac{\frac{a}{d} \cdot c + b}{d}\\
\mathbf{elif}\;d \leq 10^{+24}:\\
\;\;\;\;\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 < -4.79999999999999997e-8Initial program 44.6%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6483.2
Applied rewrites83.2%
Applied rewrites83.2%
if -4.79999999999999997e-8 < d < 9.9999999999999998e23Initial program 70.8%
Applied rewrites70.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.5
Applied rewrites82.5%
if 9.9999999999999998e23 < d Initial program 48.6%
Taylor expanded in c around 0
+-commutativeN/A
associate-*l/N/A
associate-*l/N/A
unpow2N/A
associate-/r*N/A
div-addN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6480.8
Applied rewrites80.8%
Final simplification82.3%
(FPCore (a b c d)
:precision binary64
(if (<= d -1.55e+104)
(/ b d)
(if (<= d -7e-106)
(* (/ b (fma d d (* c c))) d)
(if (<= d 1e+24) (/ a c) (/ b d)))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.55e+104) {
tmp = b / d;
} else if (d <= -7e-106) {
tmp = (b / fma(d, d, (c * c))) * d;
} else if (d <= 1e+24) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.55e+104) tmp = Float64(b / d); elseif (d <= -7e-106) tmp = Float64(Float64(b / fma(d, d, Float64(c * c))) * d); elseif (d <= 1e+24) tmp = Float64(a / c); else tmp = Float64(b / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.55e+104], N[(b / d), $MachinePrecision], If[LessEqual[d, -7e-106], N[(N[(b / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * d), $MachinePrecision], If[LessEqual[d, 1e+24], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.55 \cdot 10^{+104}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq -7 \cdot 10^{-106}:\\
\;\;\;\;\frac{b}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot d\\
\mathbf{elif}\;d \leq 10^{+24}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -1.55000000000000008e104 or 9.9999999999999998e23 < d Initial program 38.5%
Taylor expanded in c around 0
lower-/.f6470.7
Applied rewrites70.7%
if -1.55000000000000008e104 < d < -7e-106Initial program 81.8%
Taylor expanded in a around 0
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6465.2
Applied rewrites65.2%
if -7e-106 < d < 9.9999999999999998e23Initial program 67.9%
Taylor expanded in c around inf
lower-/.f6461.8
Applied rewrites61.8%
Final simplification65.9%
(FPCore (a b c d) :precision binary64 (if (or (<= c -1.45e+36) (not (<= c 5.5e+26))) (/ a c) (/ b d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -1.45e+36) || !(c <= 5.5e+26)) {
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 <= (-1.45d+36)) .or. (.not. (c <= 5.5d+26))) 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 <= -1.45e+36) || !(c <= 5.5e+26)) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (c <= -1.45e+36) or not (c <= 5.5e+26): tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if ((c <= -1.45e+36) || !(c <= 5.5e+26)) 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 <= -1.45e+36) || ~((c <= 5.5e+26))) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -1.45e+36], N[Not[LessEqual[c, 5.5e+26]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.45 \cdot 10^{+36} \lor \neg \left(c \leq 5.5 \cdot 10^{+26}\right):\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if c < -1.45e36 or 5.4999999999999997e26 < c Initial program 46.6%
Taylor expanded in c around inf
lower-/.f6464.6
Applied rewrites64.6%
if -1.45e36 < c < 5.4999999999999997e26Initial program 69.3%
Taylor expanded in c around 0
lower-/.f6463.1
Applied rewrites63.1%
Final simplification63.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 58.8%
Taylor expanded in c around inf
lower-/.f6439.5
Applied rewrites39.5%
Final simplification39.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 2024350
(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))))