
(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]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
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]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (fma d d (* c c))) (t_1 (fma (/ c t_0) a (* (/ b t_0) d))))
(if (<= c -1.15e+135)
(/ (fma d (/ b c) a) c)
(if (<= c -2.7e-17)
t_1
(if (<= c 3.25e-24)
(/ (fma (/ c d) a b) d)
(if (<= c 2.9e+175) t_1 (fma (/ d c) (/ b c) (/ a c))))))))double code(double a, double b, double c, double d) {
double t_0 = fma(d, d, (c * c));
double t_1 = fma((c / t_0), a, ((b / t_0) * d));
double tmp;
if (c <= -1.15e+135) {
tmp = fma(d, (b / c), a) / c;
} else if (c <= -2.7e-17) {
tmp = t_1;
} else if (c <= 3.25e-24) {
tmp = fma((c / d), a, b) / d;
} else if (c <= 2.9e+175) {
tmp = t_1;
} else {
tmp = fma((d / c), (b / c), (a / c));
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(d, d, Float64(c * c)) t_1 = fma(Float64(c / t_0), a, Float64(Float64(b / t_0) * d)) tmp = 0.0 if (c <= -1.15e+135) tmp = Float64(fma(d, Float64(b / c), a) / c); elseif (c <= -2.7e-17) tmp = t_1; elseif (c <= 3.25e-24) tmp = Float64(fma(Float64(c / d), a, b) / d); elseif (c <= 2.9e+175) tmp = t_1; else tmp = fma(Float64(d / c), Float64(b / c), Float64(a / c)); 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[(c / t$95$0), $MachinePrecision] * a + N[(N[(b / t$95$0), $MachinePrecision] * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -1.15e+135], N[(N[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -2.7e-17], t$95$1, If[LessEqual[c, 3.25e-24], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 2.9e+175], t$95$1, N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision] + N[(a / c), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_1 := \mathsf{fma}\left(\frac{c}{t\_0}, a, \frac{b}{t\_0} \cdot d\right)\\
\mathbf{if}\;c \leq -1.15 \cdot 10^{+135}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{elif}\;c \leq -2.7 \cdot 10^{-17}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq 3.25 \cdot 10^{-24}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{elif}\;c \leq 2.9 \cdot 10^{+175}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\
\end{array}
if c < -1.1500000000000001e135Initial program 62.0%
Applied rewrites62.0%
Taylor expanded in c around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6453.7%
Applied rewrites53.7%
if -1.1500000000000001e135 < c < -2.7000000000000001e-17 or 3.25e-24 < c < 2.9e175Initial program 62.0%
Applied rewrites61.7%
if -2.7000000000000001e-17 < c < 3.25e-24Initial program 62.0%
Taylor expanded in d around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6454.2%
Applied rewrites54.2%
if 2.9e175 < c Initial program 62.0%
Applied rewrites62.0%
Taylor expanded in c around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
div-addN/A
lift-/.f64N/A
associate-/l/N/A
lift-*.f64N/A
*-commutativeN/A
times-fracN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lower-/.f6453.0%
Applied rewrites53.0%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (fma (/ d c) b a))
(t_1 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(t_2 (fma d d (* c c)))
(t_3 (fma d b (* c a))))
(if (<= t_1 -5e+301)
(/ t_0 c)
(if (<= t_1 -5e-130)
(/ 1.0 (/ t_2 t_3))
(if (<= t_1 0.0)
(/ 1.0 (fma (/ c (fma a c (* b d))) c (/ d b)))
(if (<= t_1 2e+302)
(/ t_3 t_2)
(/ 1.0 (fma (/ 1.0 t_0) c (/ d b)))))))))double code(double a, double b, double c, double d) {
double t_0 = fma((d / c), b, a);
double t_1 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_2 = fma(d, d, (c * c));
double t_3 = fma(d, b, (c * a));
double tmp;
if (t_1 <= -5e+301) {
tmp = t_0 / c;
} else if (t_1 <= -5e-130) {
tmp = 1.0 / (t_2 / t_3);
} else if (t_1 <= 0.0) {
tmp = 1.0 / fma((c / fma(a, c, (b * d))), c, (d / b));
} else if (t_1 <= 2e+302) {
tmp = t_3 / t_2;
} else {
tmp = 1.0 / fma((1.0 / t_0), c, (d / b));
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(Float64(d / c), b, a) t_1 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) t_2 = fma(d, d, Float64(c * c)) t_3 = fma(d, b, Float64(c * a)) tmp = 0.0 if (t_1 <= -5e+301) tmp = Float64(t_0 / c); elseif (t_1 <= -5e-130) tmp = Float64(1.0 / Float64(t_2 / t_3)); elseif (t_1 <= 0.0) tmp = Float64(1.0 / fma(Float64(c / fma(a, c, Float64(b * d))), c, Float64(d / b))); elseif (t_1 <= 2e+302) tmp = Float64(t_3 / t_2); else tmp = Float64(1.0 / fma(Float64(1.0 / t_0), c, Float64(d / b))); end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -5e+301], N[(t$95$0 / c), $MachinePrecision], If[LessEqual[t$95$1, -5e-130], N[(1.0 / N[(t$95$2 / t$95$3), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.0], N[(1.0 / N[(N[(c / N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * c + N[(d / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 2e+302], N[(t$95$3 / t$95$2), $MachinePrecision], N[(1.0 / N[(N[(1.0 / t$95$0), $MachinePrecision] * c + N[(d / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]]]
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\
t_1 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
t_2 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_3 := \mathsf{fma}\left(d, b, c \cdot a\right)\\
\mathbf{if}\;t\_1 \leq -5 \cdot 10^{+301}:\\
\;\;\;\;\frac{t\_0}{c}\\
\mathbf{elif}\;t\_1 \leq -5 \cdot 10^{-130}:\\
\;\;\;\;\frac{1}{\frac{t\_2}{t\_3}}\\
\mathbf{elif}\;t\_1 \leq 0:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{c}{\mathsf{fma}\left(a, c, b \cdot d\right)}, c, \frac{d}{b}\right)}\\
\mathbf{elif}\;t\_1 \leq 2 \cdot 10^{+302}:\\
\;\;\;\;\frac{t\_3}{t\_2}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(\frac{1}{t\_0}, c, \frac{d}{b}\right)}\\
\end{array}
if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < -5.0000000000000004e301Initial program 62.0%
Applied rewrites62.0%
Taylor expanded in c around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6454.5%
Applied rewrites54.5%
if -5.0000000000000004e301 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < -4.9999999999999996e-130Initial program 62.0%
Applied rewrites61.8%
if -4.9999999999999996e-130 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 0.0Initial program 62.0%
Applied rewrites61.8%
lift-/.f64N/A
lift-fma.f64N/A
div-addN/A
+-commutativeN/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lift-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower-*.f6467.9%
lift-fma.f64N/A
lift-*.f64N/A
Applied rewrites67.9%
Taylor expanded in a around 0
lower-/.f6468.3%
Applied rewrites68.3%
if 0.0 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 2.0000000000000002e302Initial program 62.0%
Applied rewrites62.0%
if 2.0000000000000002e302 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) Initial program 62.0%
Applied rewrites61.8%
lift-/.f64N/A
lift-fma.f64N/A
div-addN/A
+-commutativeN/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lift-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower-*.f6467.9%
lift-fma.f64N/A
lift-*.f64N/A
Applied rewrites67.9%
Taylor expanded in a around 0
lower-/.f6468.3%
Applied rewrites68.3%
lift-/.f64N/A
div-flipN/A
lower-unsound-/.f32N/A
lower-/.f32N/A
lift-fma.f64N/A
add-to-fractionN/A
lift-/.f64N/A
lift-+.f64N/A
lower-unsound-/.f6474.1%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6476.3%
Applied rewrites76.3%
(FPCore (a b c d)
:precision binary64
(if (<= c -2.7e-17)
(/ (fma d (/ b c) a) c)
(if (<= c 1.9e-139)
(/ (fma (/ c d) a b) d)
(if (<= c 2.85e+175)
(/ (fma d b (* c a)) (fma d d (* c c)))
(fma (/ d c) (/ b c) (/ a c))))))double code(double a, double b, double c, double d) {
double tmp;
if (c <= -2.7e-17) {
tmp = fma(d, (b / c), a) / c;
} else if (c <= 1.9e-139) {
tmp = fma((c / d), a, b) / d;
} else if (c <= 2.85e+175) {
tmp = fma(d, b, (c * a)) / fma(d, d, (c * c));
} else {
tmp = fma((d / c), (b / c), (a / c));
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -2.7e-17) tmp = Float64(fma(d, Float64(b / c), a) / c); elseif (c <= 1.9e-139) tmp = Float64(fma(Float64(c / d), a, b) / d); elseif (c <= 2.85e+175) tmp = Float64(fma(d, b, Float64(c * a)) / fma(d, d, Float64(c * c))); else tmp = fma(Float64(d / c), Float64(b / c), Float64(a / c)); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -2.7e-17], N[(N[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, 1.9e-139], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 2.85e+175], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision] + N[(a / c), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\mathbf{if}\;c \leq -2.7 \cdot 10^{-17}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{elif}\;c \leq 1.9 \cdot 10^{-139}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{elif}\;c \leq 2.85 \cdot 10^{+175}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\
\end{array}
if c < -2.7000000000000001e-17Initial program 62.0%
Applied rewrites62.0%
Taylor expanded in c around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6453.7%
Applied rewrites53.7%
if -2.7000000000000001e-17 < c < 1.90000000000000004e-139Initial program 62.0%
Taylor expanded in d around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6454.2%
Applied rewrites54.2%
if 1.90000000000000004e-139 < c < 2.85000000000000012e175Initial program 62.0%
Applied rewrites62.0%
if 2.85000000000000012e175 < c Initial program 62.0%
Applied rewrites62.0%
Taylor expanded in c around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
div-addN/A
lift-/.f64N/A
associate-/l/N/A
lift-*.f64N/A
*-commutativeN/A
times-fracN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f64N/A
lower-/.f6453.0%
Applied rewrites53.0%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma d (/ b c) a) c))) (if (<= c -2.7e-17) t_0 (if (<= c 5.2e+62) (/ (fma (/ c d) a b) d) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, (b / c), a) / c;
double tmp;
if (c <= -2.7e-17) {
tmp = t_0;
} else if (c <= 5.2e+62) {
tmp = fma((c / d), a, b) / d;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, Float64(b / c), a) / c) tmp = 0.0 if (c <= -2.7e-17) tmp = t_0; elseif (c <= 5.2e+62) tmp = Float64(fma(Float64(c / d), a, b) / d); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -2.7e-17], t$95$0, If[LessEqual[c, 5.2e+62], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], t$95$0]]]
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -2.7 \cdot 10^{-17}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 5.2 \cdot 10^{+62}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
if c < -2.7000000000000001e-17 or 5.19999999999999968e62 < c Initial program 62.0%
Applied rewrites62.0%
Taylor expanded in c around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6453.7%
Applied rewrites53.7%
if -2.7000000000000001e-17 < c < 5.19999999999999968e62Initial program 62.0%
Taylor expanded in d around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6454.2%
Applied rewrites54.2%
(FPCore (a b c d) :precision binary64 (if (<= c -2.7e-17) (/ a c) (if (<= c 2.6e+45) (/ (fma (/ c d) a b) d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -2.7e-17) {
tmp = a / c;
} else if (c <= 2.6e+45) {
tmp = fma((c / d), a, b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -2.7e-17) tmp = Float64(a / c); elseif (c <= 2.6e+45) tmp = Float64(fma(Float64(c / d), a, b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -2.7e-17], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.6e+45], N[(N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;c \leq -2.7 \cdot 10^{-17}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.6 \cdot 10^{+45}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
if c < -2.7000000000000001e-17 or 2.60000000000000007e45 < c Initial program 62.0%
Taylor expanded in c around inf
lower-/.f6442.9%
Applied rewrites42.9%
if -2.7000000000000001e-17 < c < 2.60000000000000007e45Initial program 62.0%
Taylor expanded in d around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6454.2%
Applied rewrites54.2%
(FPCore (a b c d) :precision binary64 (if (<= c -2.7e-17) (/ a c) (if (<= c 2.6e+45) (/ (fma (/ a d) c b) d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -2.7e-17) {
tmp = a / c;
} else if (c <= 2.6e+45) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -2.7e-17) tmp = Float64(a / c); elseif (c <= 2.6e+45) tmp = Float64(fma(Float64(a / d), c, b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -2.7e-17], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.6e+45], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;c \leq -2.7 \cdot 10^{-17}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.6 \cdot 10^{+45}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
if c < -2.7000000000000001e-17 or 2.60000000000000007e45 < c Initial program 62.0%
Taylor expanded in c around inf
lower-/.f6442.9%
Applied rewrites42.9%
if -2.7000000000000001e-17 < c < 2.60000000000000007e45Initial program 62.0%
Taylor expanded in d around inf
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
lower-*.f6452.3%
Applied rewrites52.3%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6453.5%
Applied rewrites53.5%
(FPCore (a b c d) :precision binary64 (if (<= d -5.5e+78) (/ b d) (if (<= d 9.8e+17) (/ a c) (/ b d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -5.5e+78) {
tmp = b / d;
} else if (d <= 9.8e+17) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if (d <= (-5.5d+78)) then
tmp = b / d
else if (d <= 9.8d+17) then
tmp = a / c
else
tmp = b / d
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (d <= -5.5e+78) {
tmp = b / d;
} else if (d <= 9.8e+17) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -5.5e+78: tmp = b / d elif d <= 9.8e+17: tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if (d <= -5.5e+78) tmp = Float64(b / d); elseif (d <= 9.8e+17) tmp = Float64(a / c); else tmp = Float64(b / d); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (d <= -5.5e+78) tmp = b / d; elseif (d <= 9.8e+17) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -5.5e+78], N[(b / d), $MachinePrecision], If[LessEqual[d, 9.8e+17], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}
\mathbf{if}\;d \leq -5.5 \cdot 10^{+78}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq 9.8 \cdot 10^{+17}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
if d < -5.4999999999999997e78 or 9.8e17 < d Initial program 62.0%
Taylor expanded in c around 0
lower-/.f6442.5%
Applied rewrites42.5%
if -5.4999999999999997e78 < d < 9.8e17Initial program 62.0%
Taylor expanded in c around inf
lower-/.f6442.9%
Applied rewrites42.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]
\frac{a}{c}
Initial program 62.0%
Taylor expanded in c around inf
lower-/.f6442.9%
Applied rewrites42.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}
\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}
herbie shell --seed 2025183
(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))))