
(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 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 (/ (+ (* a c) (* b d)) (fma c c (* d d))))
(t_1 (/ (fma b (/ d c) a) c)))
(if (<= c -1.05e+61)
t_1
(if (<= c -1.8e-156)
t_0
(if (<= c 2.6e-153)
(/ (fma (/ a d) c b) d)
(if (<= c 2.2e+83) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / fma(c, c, (d * d));
double t_1 = fma(b, (d / c), a) / c;
double tmp;
if (c <= -1.05e+61) {
tmp = t_1;
} else if (c <= -1.8e-156) {
tmp = t_0;
} else if (c <= 2.6e-153) {
tmp = fma((a / d), c, b) / d;
} else if (c <= 2.2e+83) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / fma(c, c, Float64(d * d))) t_1 = Float64(fma(b, Float64(d / c), a) / c) tmp = 0.0 if (c <= -1.05e+61) tmp = t_1; elseif (c <= -1.8e-156) tmp = t_0; elseif (c <= 2.6e-153) tmp = Float64(fma(Float64(a / d), c, b) / d); elseif (c <= 2.2e+83) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -1.05e+61], t$95$1, If[LessEqual[c, -1.8e-156], t$95$0, If[LessEqual[c, 2.6e-153], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 2.2e+83], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\
t_1 := \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -1.05 \cdot 10^{+61}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq -1.8 \cdot 10^{-156}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 2.6 \cdot 10^{-153}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{elif}\;c \leq 2.2 \cdot 10^{+83}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if c < -1.0500000000000001e61 or 2.19999999999999999e83 < c Initial program 42.7%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6486.5
Applied rewrites86.5%
if -1.0500000000000001e61 < c < -1.79999999999999999e-156 or 2.6000000000000001e-153 < c < 2.19999999999999999e83Initial program 83.9%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6484.0
Applied rewrites84.0%
if -1.79999999999999999e-156 < c < 2.6000000000000001e-153Initial program 70.4%
Taylor expanded in c around 0
+-commutativeN/A
pow2N/A
times-fracN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lower-/.f6494.2
Applied rewrites94.2%
lift-/.f64N/A
lift-/.f64N/A
lift-/.f64N/A
lift-fma.f64N/A
associate-*r/N/A
div-add-revN/A
lower-/.f64N/A
lower-fma.f64N/A
lift-/.f6494.6
Applied rewrites94.6%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (fma d b (* c a))))
(if (<= c -2.5e+144)
(/ a c)
(if (<= c -4.1e-102)
(* a (/ c (fma d d (* c c))))
(if (<= c 1.7e-78)
(/ t_0 (* d d))
(if (<= c 9.5e+81) (/ t_0 (* c c)) (/ a c)))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, b, (c * a));
double tmp;
if (c <= -2.5e+144) {
tmp = a / c;
} else if (c <= -4.1e-102) {
tmp = a * (c / fma(d, d, (c * c)));
} else if (c <= 1.7e-78) {
tmp = t_0 / (d * d);
} else if (c <= 9.5e+81) {
tmp = t_0 / (c * c);
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(d, b, Float64(c * a)) tmp = 0.0 if (c <= -2.5e+144) tmp = Float64(a / c); elseif (c <= -4.1e-102) tmp = Float64(a * Float64(c / fma(d, d, Float64(c * c)))); elseif (c <= 1.7e-78) tmp = Float64(t_0 / Float64(d * d)); elseif (c <= 9.5e+81) tmp = Float64(t_0 / Float64(c * c)); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -2.5e+144], N[(a / c), $MachinePrecision], If[LessEqual[c, -4.1e-102], N[(a * N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.7e-78], N[(t$95$0 / N[(d * d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 9.5e+81], N[(t$95$0 / N[(c * c), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, b, c \cdot a\right)\\
\mathbf{if}\;c \leq -2.5 \cdot 10^{+144}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -4.1 \cdot 10^{-102}:\\
\;\;\;\;a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{elif}\;c \leq 1.7 \cdot 10^{-78}:\\
\;\;\;\;\frac{t\_0}{d \cdot d}\\
\mathbf{elif}\;c \leq 9.5 \cdot 10^{+81}:\\
\;\;\;\;\frac{t\_0}{c \cdot c}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -2.5e144 or 9.50000000000000083e81 < c Initial program 38.6%
Taylor expanded in c around inf
lower-/.f6481.0
Applied rewrites81.0%
if -2.5e144 < c < -4.1000000000000003e-102Initial program 77.0%
Taylor expanded in a around inf
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6470.2
Applied rewrites70.2%
if -4.1000000000000003e-102 < c < 1.70000000000000006e-78Initial program 77.2%
Taylor expanded in c around 0
+-commutativeN/A
pow2N/A
times-fracN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lower-/.f6485.5
Applied rewrites85.5%
Taylor expanded in d around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
pow2N/A
lift-*.f6468.2
Applied rewrites68.2%
if 1.70000000000000006e-78 < c < 9.50000000000000083e81Initial program 79.0%
Taylor expanded in c around inf
pow2N/A
lift-*.f6464.8
Applied rewrites64.8%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6464.8
pow264.8
pow264.8
+-commutative64.8
pow264.8
pow264.8
Applied rewrites64.8%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (* a (/ c (fma d d (* c c))))))
(if (<= c -2.5e+144)
(/ a c)
(if (<= c -5.8e-102)
t_0
(if (<= c 5.2e-151) (/ b d) (if (<= c 8.6e+164) t_0 (/ a c)))))))
double code(double a, double b, double c, double d) {
double t_0 = a * (c / fma(d, d, (c * c)));
double tmp;
if (c <= -2.5e+144) {
tmp = a / c;
} else if (c <= -5.8e-102) {
tmp = t_0;
} else if (c <= 5.2e-151) {
tmp = b / d;
} else if (c <= 8.6e+164) {
tmp = t_0;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(a * Float64(c / fma(d, d, Float64(c * c)))) tmp = 0.0 if (c <= -2.5e+144) tmp = Float64(a / c); elseif (c <= -5.8e-102) tmp = t_0; elseif (c <= 5.2e-151) tmp = Float64(b / d); elseif (c <= 8.6e+164) tmp = t_0; else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(a * N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -2.5e+144], N[(a / c), $MachinePrecision], If[LessEqual[c, -5.8e-102], t$95$0, If[LessEqual[c, 5.2e-151], N[(b / d), $MachinePrecision], If[LessEqual[c, 8.6e+164], t$95$0, N[(a / c), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := a \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{if}\;c \leq -2.5 \cdot 10^{+144}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -5.8 \cdot 10^{-102}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 5.2 \cdot 10^{-151}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;c \leq 8.6 \cdot 10^{+164}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -2.5e144 or 8.6e164 < c Initial program 33.1%
Taylor expanded in c around inf
lower-/.f6484.9
Applied rewrites84.9%
if -2.5e144 < c < -5.79999999999999973e-102 or 5.2000000000000001e-151 < c < 8.6e164Initial program 77.0%
Taylor expanded in a around inf
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
pow2N/A
lower-fma.f64N/A
pow2N/A
lift-*.f6463.5
Applied rewrites63.5%
if -5.79999999999999973e-102 < c < 5.2000000000000001e-151Initial program 74.9%
Taylor expanded in c around 0
lower-/.f6471.0
Applied rewrites71.0%
(FPCore (a b c d)
:precision binary64
(if (<= c -6.8e-12)
(/ a c)
(if (<= c 2.4e-78)
(/ (fma a (/ c d) b) d)
(if (<= c 9.5e+81) (/ (fma d b (* c a)) (* c c)) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -6.8e-12) {
tmp = a / c;
} else if (c <= 2.4e-78) {
tmp = fma(a, (c / d), b) / d;
} else if (c <= 9.5e+81) {
tmp = fma(d, b, (c * a)) / (c * c);
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -6.8e-12) tmp = Float64(a / c); elseif (c <= 2.4e-78) tmp = Float64(fma(a, Float64(c / d), b) / d); elseif (c <= 9.5e+81) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(c * c)); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -6.8e-12], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.4e-78], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 9.5e+81], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -6.8 \cdot 10^{-12}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.4 \cdot 10^{-78}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{elif}\;c \leq 9.5 \cdot 10^{+81}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -6.8000000000000001e-12 or 9.50000000000000083e81 < c Initial program 48.2%
Taylor expanded in c around inf
lower-/.f6475.8
Applied rewrites75.8%
if -6.8000000000000001e-12 < c < 2.4e-78Initial program 78.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6482.8
Applied rewrites82.8%
if 2.4e-78 < c < 9.50000000000000083e81Initial program 79.0%
Taylor expanded in c around inf
pow2N/A
lift-*.f6464.8
Applied rewrites64.8%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f6464.8
pow264.8
pow264.8
+-commutative64.8
pow264.8
pow264.8
Applied rewrites64.8%
(FPCore (a b c d) :precision binary64 (if (or (<= c -6e-12) (not (<= c 2.4e-78))) (/ (fma b (/ d c) a) c) (/ (fma a (/ c d) b) d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -6e-12) || !(c <= 2.4e-78)) {
tmp = fma(b, (d / c), a) / c;
} else {
tmp = fma(a, (c / d), b) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((c <= -6e-12) || !(c <= 2.4e-78)) tmp = Float64(fma(b, Float64(d / c), a) / c); else tmp = Float64(fma(a, Float64(c / d), b) / d); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -6e-12], N[Not[LessEqual[c, 2.4e-78]], $MachinePrecision]], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -6 \cdot 10^{-12} \lor \neg \left(c \leq 2.4 \cdot 10^{-78}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\end{array}
\end{array}
if c < -6.0000000000000003e-12 or 2.4e-78 < c Initial program 54.9%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6480.6
Applied rewrites80.6%
if -6.0000000000000003e-12 < c < 2.4e-78Initial program 78.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6482.8
Applied rewrites82.8%
Final simplification81.5%
(FPCore (a b c d) :precision binary64 (if (or (<= c -3.2e-12) (not (<= c 2.4e-78))) (/ a c) (/ (fma d b (* c a)) (* d d))))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -3.2e-12) || !(c <= 2.4e-78)) {
tmp = a / c;
} else {
tmp = fma(d, b, (c * a)) / (d * d);
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((c <= -3.2e-12) || !(c <= 2.4e-78)) tmp = Float64(a / c); else tmp = Float64(fma(d, b, Float64(c * a)) / Float64(d * d)); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -3.2e-12], N[Not[LessEqual[c, 2.4e-78]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3.2 \cdot 10^{-12} \lor \neg \left(c \leq 2.4 \cdot 10^{-78}\right):\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{d \cdot d}\\
\end{array}
\end{array}
if c < -3.2000000000000001e-12 or 2.4e-78 < c Initial program 54.9%
Taylor expanded in c around inf
lower-/.f6466.6
Applied rewrites66.6%
if -3.2000000000000001e-12 < c < 2.4e-78Initial program 78.4%
Taylor expanded in c around 0
+-commutativeN/A
pow2N/A
times-fracN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lower-/.f6481.9
Applied rewrites81.9%
Taylor expanded in d around 0
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
pow2N/A
lift-*.f6465.6
Applied rewrites65.6%
Final simplification66.2%
(FPCore (a b c d) :precision binary64 (if (or (<= c -6.5e-12) (not (<= c 2.4e-78))) (/ a c) (/ b d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -6.5e-12) || !(c <= 2.4e-78)) {
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 <= (-6.5d-12)) .or. (.not. (c <= 2.4d-78))) 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 <= -6.5e-12) || !(c <= 2.4e-78)) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (c <= -6.5e-12) or not (c <= 2.4e-78): tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if ((c <= -6.5e-12) || !(c <= 2.4e-78)) 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 <= -6.5e-12) || ~((c <= 2.4e-78))) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -6.5e-12], N[Not[LessEqual[c, 2.4e-78]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -6.5 \cdot 10^{-12} \lor \neg \left(c \leq 2.4 \cdot 10^{-78}\right):\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if c < -6.5000000000000002e-12 or 2.4e-78 < c Initial program 54.9%
Taylor expanded in c around inf
lower-/.f6466.6
Applied rewrites66.6%
if -6.5000000000000002e-12 < c < 2.4e-78Initial program 78.4%
Taylor expanded in c around 0
lower-/.f6460.0
Applied rewrites60.0%
Final simplification63.9%
(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 64.4%
Taylor expanded in c around inf
lower-/.f6446.9
Applied rewrites46.9%
(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 2025037
(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))))