
(FPCore (a b c d) :precision binary64 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((b * c) - (a * 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 = ((b * c) - (a * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((b * c) - (a * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((b * c) - (a * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c d) :precision binary64 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((b * c) - (a * 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 = ((b * c) - (a * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((b * c) - (a * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((b * c) - (a * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\end{array}
(FPCore (a b c d) :precision binary64 (if (<= d -1.45e-31) (/ (fma c (/ b d) (- a)) d) (if (<= d 2.2e+69) (/ (- b (* d (/ a c))) c) (/ (fma b (/ c d) (- a)) d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.45e-31) {
tmp = fma(c, (b / d), -a) / d;
} else if (d <= 2.2e+69) {
tmp = (b - (d * (a / c))) / c;
} else {
tmp = fma(b, (c / d), -a) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (d <= -1.45e-31) tmp = Float64(fma(c, Float64(b / d), Float64(-a)) / d); elseif (d <= 2.2e+69) tmp = Float64(Float64(b - Float64(d * Float64(a / c))) / c); else tmp = Float64(fma(b, Float64(c / d), Float64(-a)) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.45e-31], N[(N[(c * N[(b / d), $MachinePrecision] + (-a)), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 2.2e+69], N[(N[(b - N[(d * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], N[(N[(b * N[(c / d), $MachinePrecision] + (-a)), $MachinePrecision] / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.45 \cdot 10^{-31}:\\
\;\;\;\;\frac{\mathsf{fma}\left(c, \frac{b}{d}, -a\right)}{d}\\
\mathbf{elif}\;d \leq 2.2 \cdot 10^{+69}:\\
\;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{c}{d}, -a\right)}{d}\\
\end{array}
\end{array}
if d < -1.45e-31Initial program 47.1%
Taylor expanded in b around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites45.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lift-neg.f6477.1
Applied rewrites77.1%
if -1.45e-31 < d < 2.2000000000000002e69Initial program 74.0%
Taylor expanded in c around inf
lower-/.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
metadata-evalN/A
times-fracN/A
mul-1-negN/A
mul-1-negN/A
frac-2negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6486.8
Applied rewrites86.8%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6487.6
Applied rewrites87.6%
if 2.2000000000000002e69 < d Initial program 45.3%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6487.2
Applied rewrites87.2%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (- a) d)))
(if (<= d -8.5e+53)
t_0
(if (<= d -1.45e-31)
(/ (- (* b c) (* a d)) (* d d))
(if (<= d 7.2e+99) (/ (- b (* d (/ a c))) c) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = -a / d;
double tmp;
if (d <= -8.5e+53) {
tmp = t_0;
} else if (d <= -1.45e-31) {
tmp = ((b * c) - (a * d)) / (d * d);
} else if (d <= 7.2e+99) {
tmp = (b - (d * (a / c))) / c;
} else {
tmp = t_0;
}
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) :: t_0
real(8) :: tmp
t_0 = -a / d
if (d <= (-8.5d+53)) then
tmp = t_0
else if (d <= (-1.45d-31)) then
tmp = ((b * c) - (a * d)) / (d * d)
else if (d <= 7.2d+99) then
tmp = (b - (d * (a / c))) / c
else
tmp = t_0
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = -a / d;
double tmp;
if (d <= -8.5e+53) {
tmp = t_0;
} else if (d <= -1.45e-31) {
tmp = ((b * c) - (a * d)) / (d * d);
} else if (d <= 7.2e+99) {
tmp = (b - (d * (a / c))) / c;
} else {
tmp = t_0;
}
return tmp;
}
def code(a, b, c, d): t_0 = -a / d tmp = 0 if d <= -8.5e+53: tmp = t_0 elif d <= -1.45e-31: tmp = ((b * c) - (a * d)) / (d * d) elif d <= 7.2e+99: tmp = (b - (d * (a / c))) / c else: tmp = t_0 return tmp
function code(a, b, c, d) t_0 = Float64(Float64(-a) / d) tmp = 0.0 if (d <= -8.5e+53) tmp = t_0; elseif (d <= -1.45e-31) tmp = Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(d * d)); elseif (d <= 7.2e+99) tmp = Float64(Float64(b - Float64(d * Float64(a / c))) / c); else tmp = t_0; end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = -a / d; tmp = 0.0; if (d <= -8.5e+53) tmp = t_0; elseif (d <= -1.45e-31) tmp = ((b * c) - (a * d)) / (d * d); elseif (d <= 7.2e+99) tmp = (b - (d * (a / c))) / c; else tmp = t_0; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[((-a) / d), $MachinePrecision]}, If[LessEqual[d, -8.5e+53], t$95$0, If[LessEqual[d, -1.45e-31], N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(d * d), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 7.2e+99], N[(N[(b - N[(d * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-a}{d}\\
\mathbf{if}\;d \leq -8.5 \cdot 10^{+53}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq -1.45 \cdot 10^{-31}:\\
\;\;\;\;\frac{b \cdot c - a \cdot d}{d \cdot d}\\
\mathbf{elif}\;d \leq 7.2 \cdot 10^{+99}:\\
\;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -8.5000000000000002e53 or 7.2000000000000003e99 < d Initial program 41.0%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6478.4
Applied rewrites78.4%
if -8.5000000000000002e53 < d < -1.45e-31Initial program 76.7%
Taylor expanded in c around 0
pow2N/A
lift-*.f6477.1
Applied rewrites77.1%
if -1.45e-31 < d < 7.2000000000000003e99Initial program 74.0%
Taylor expanded in c around inf
lower-/.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
metadata-evalN/A
times-fracN/A
mul-1-negN/A
mul-1-negN/A
frac-2negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6484.3
Applied rewrites84.3%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6485.1
Applied rewrites85.1%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (- a) d)) (t_1 (- (* b c) (* a d))))
(if (<= d -8.5e+53)
t_0
(if (<= d -5e-32)
(/ t_1 (* d d))
(if (<= d -9e-208) (/ t_1 (* c c)) (if (<= d 7.2e+99) (/ b c) t_0))))))
double code(double a, double b, double c, double d) {
double t_0 = -a / d;
double t_1 = (b * c) - (a * d);
double tmp;
if (d <= -8.5e+53) {
tmp = t_0;
} else if (d <= -5e-32) {
tmp = t_1 / (d * d);
} else if (d <= -9e-208) {
tmp = t_1 / (c * c);
} else if (d <= 7.2e+99) {
tmp = b / c;
} else {
tmp = t_0;
}
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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = -a / d
t_1 = (b * c) - (a * d)
if (d <= (-8.5d+53)) then
tmp = t_0
else if (d <= (-5d-32)) then
tmp = t_1 / (d * d)
else if (d <= (-9d-208)) then
tmp = t_1 / (c * c)
else if (d <= 7.2d+99) then
tmp = b / c
else
tmp = t_0
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = -a / d;
double t_1 = (b * c) - (a * d);
double tmp;
if (d <= -8.5e+53) {
tmp = t_0;
} else if (d <= -5e-32) {
tmp = t_1 / (d * d);
} else if (d <= -9e-208) {
tmp = t_1 / (c * c);
} else if (d <= 7.2e+99) {
tmp = b / c;
} else {
tmp = t_0;
}
return tmp;
}
def code(a, b, c, d): t_0 = -a / d t_1 = (b * c) - (a * d) tmp = 0 if d <= -8.5e+53: tmp = t_0 elif d <= -5e-32: tmp = t_1 / (d * d) elif d <= -9e-208: tmp = t_1 / (c * c) elif d <= 7.2e+99: tmp = b / c else: tmp = t_0 return tmp
function code(a, b, c, d) t_0 = Float64(Float64(-a) / d) t_1 = Float64(Float64(b * c) - Float64(a * d)) tmp = 0.0 if (d <= -8.5e+53) tmp = t_0; elseif (d <= -5e-32) tmp = Float64(t_1 / Float64(d * d)); elseif (d <= -9e-208) tmp = Float64(t_1 / Float64(c * c)); elseif (d <= 7.2e+99) tmp = Float64(b / c); else tmp = t_0; end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = -a / d; t_1 = (b * c) - (a * d); tmp = 0.0; if (d <= -8.5e+53) tmp = t_0; elseif (d <= -5e-32) tmp = t_1 / (d * d); elseif (d <= -9e-208) tmp = t_1 / (c * c); elseif (d <= 7.2e+99) tmp = b / c; else tmp = t_0; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[((-a) / d), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -8.5e+53], t$95$0, If[LessEqual[d, -5e-32], N[(t$95$1 / N[(d * d), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -9e-208], N[(t$95$1 / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 7.2e+99], N[(b / c), $MachinePrecision], t$95$0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-a}{d}\\
t_1 := b \cdot c - a \cdot d\\
\mathbf{if}\;d \leq -8.5 \cdot 10^{+53}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq -5 \cdot 10^{-32}:\\
\;\;\;\;\frac{t\_1}{d \cdot d}\\
\mathbf{elif}\;d \leq -9 \cdot 10^{-208}:\\
\;\;\;\;\frac{t\_1}{c \cdot c}\\
\mathbf{elif}\;d \leq 7.2 \cdot 10^{+99}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -8.5000000000000002e53 or 7.2000000000000003e99 < d Initial program 41.0%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6478.4
Applied rewrites78.4%
if -8.5000000000000002e53 < d < -5e-32Initial program 76.7%
Taylor expanded in c around 0
pow2N/A
lift-*.f6477.1
Applied rewrites77.1%
if -5e-32 < d < -8.9999999999999992e-208Initial program 78.8%
Taylor expanded in c around inf
pow2N/A
lift-*.f6468.7
Applied rewrites68.7%
if -8.9999999999999992e-208 < d < 7.2000000000000003e99Initial program 71.7%
Taylor expanded in c around inf
lower-/.f6468.4
Applied rewrites68.4%
(FPCore (a b c d) :precision binary64 (if (or (<= d -1.45e-31) (not (<= d 2.2e+69))) (/ (fma b (/ c d) (- a)) d) (/ (- b (* d (/ a c))) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.45e-31) || !(d <= 2.2e+69)) {
tmp = fma(b, (c / d), -a) / d;
} else {
tmp = (b - (d * (a / c))) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if ((d <= -1.45e-31) || !(d <= 2.2e+69)) tmp = Float64(fma(b, Float64(c / d), Float64(-a)) / d); else tmp = Float64(Float64(b - Float64(d * Float64(a / c))) / c); end return tmp end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -1.45e-31], N[Not[LessEqual[d, 2.2e+69]], $MachinePrecision]], N[(N[(b * N[(c / d), $MachinePrecision] + (-a)), $MachinePrecision] / d), $MachinePrecision], N[(N[(b - N[(d * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.45 \cdot 10^{-31} \lor \neg \left(d \leq 2.2 \cdot 10^{+69}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{c}{d}, -a\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\
\end{array}
\end{array}
if d < -1.45e-31 or 2.2000000000000002e69 < d Initial program 46.3%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6481.6
Applied rewrites81.6%
if -1.45e-31 < d < 2.2000000000000002e69Initial program 74.0%
Taylor expanded in c around inf
lower-/.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
metadata-evalN/A
times-fracN/A
mul-1-negN/A
mul-1-negN/A
frac-2negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6486.8
Applied rewrites86.8%
lift-*.f64N/A
lift-/.f64N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6487.6
Applied rewrites87.6%
Final simplification84.6%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (- a) d)))
(if (<= d -1.45e-31)
t_0
(if (<= d -9e-208)
(/ (- (* b c) (* a d)) (* c c))
(if (<= d 7.2e+99) (/ b c) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = -a / d;
double tmp;
if (d <= -1.45e-31) {
tmp = t_0;
} else if (d <= -9e-208) {
tmp = ((b * c) - (a * d)) / (c * c);
} else if (d <= 7.2e+99) {
tmp = b / c;
} else {
tmp = t_0;
}
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) :: t_0
real(8) :: tmp
t_0 = -a / d
if (d <= (-1.45d-31)) then
tmp = t_0
else if (d <= (-9d-208)) then
tmp = ((b * c) - (a * d)) / (c * c)
else if (d <= 7.2d+99) then
tmp = b / c
else
tmp = t_0
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = -a / d;
double tmp;
if (d <= -1.45e-31) {
tmp = t_0;
} else if (d <= -9e-208) {
tmp = ((b * c) - (a * d)) / (c * c);
} else if (d <= 7.2e+99) {
tmp = b / c;
} else {
tmp = t_0;
}
return tmp;
}
def code(a, b, c, d): t_0 = -a / d tmp = 0 if d <= -1.45e-31: tmp = t_0 elif d <= -9e-208: tmp = ((b * c) - (a * d)) / (c * c) elif d <= 7.2e+99: tmp = b / c else: tmp = t_0 return tmp
function code(a, b, c, d) t_0 = Float64(Float64(-a) / d) tmp = 0.0 if (d <= -1.45e-31) tmp = t_0; elseif (d <= -9e-208) tmp = Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(c * c)); elseif (d <= 7.2e+99) tmp = Float64(b / c); else tmp = t_0; end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = -a / d; tmp = 0.0; if (d <= -1.45e-31) tmp = t_0; elseif (d <= -9e-208) tmp = ((b * c) - (a * d)) / (c * c); elseif (d <= 7.2e+99) tmp = b / c; else tmp = t_0; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[((-a) / d), $MachinePrecision]}, If[LessEqual[d, -1.45e-31], t$95$0, If[LessEqual[d, -9e-208], N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 7.2e+99], N[(b / c), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-a}{d}\\
\mathbf{if}\;d \leq -1.45 \cdot 10^{-31}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq -9 \cdot 10^{-208}:\\
\;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c}\\
\mathbf{elif}\;d \leq 7.2 \cdot 10^{+99}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -1.45e-31 or 7.2000000000000003e99 < d Initial program 44.8%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6473.8
Applied rewrites73.8%
if -1.45e-31 < d < -8.9999999999999992e-208Initial program 78.8%
Taylor expanded in c around inf
pow2N/A
lift-*.f6468.7
Applied rewrites68.7%
if -8.9999999999999992e-208 < d < 7.2000000000000003e99Initial program 71.7%
Taylor expanded in c around inf
lower-/.f6468.4
Applied rewrites68.4%
(FPCore (a b c d) :precision binary64 (if (or (<= d -2.05e-53) (not (<= d 7.2e+99))) (/ (- a) d) (/ b c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.05e-53) || !(d <= 7.2e+99)) {
tmp = -a / d;
} else {
tmp = b / c;
}
return tmp;
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(a, b, c, d)
use fmin_fmax_functions
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if ((d <= (-2.05d-53)) .or. (.not. (d <= 7.2d+99))) then
tmp = -a / d
else
tmp = b / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.05e-53) || !(d <= 7.2e+99)) {
tmp = -a / d;
} else {
tmp = b / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -2.05e-53) or not (d <= 7.2e+99): tmp = -a / d else: tmp = b / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -2.05e-53) || !(d <= 7.2e+99)) tmp = Float64(Float64(-a) / d); else tmp = Float64(b / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -2.05e-53) || ~((d <= 7.2e+99))) tmp = -a / d; else tmp = b / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.05e-53], N[Not[LessEqual[d, 7.2e+99]], $MachinePrecision]], N[((-a) / d), $MachinePrecision], N[(b / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.05 \cdot 10^{-53} \lor \neg \left(d \leq 7.2 \cdot 10^{+99}\right):\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{c}\\
\end{array}
\end{array}
if d < -2.05e-53 or 7.2000000000000003e99 < d Initial program 47.4%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6472.2
Applied rewrites72.2%
if -2.05e-53 < d < 7.2000000000000003e99Initial program 72.7%
Taylor expanded in c around inf
lower-/.f6465.2
Applied rewrites65.2%
Final simplification68.7%
(FPCore (a b c d) :precision binary64 (/ b c))
double code(double a, double b, double c, double d) {
return b / 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 = b / c
end function
public static double code(double a, double b, double c, double d) {
return b / c;
}
def code(a, b, c, d): return b / c
function code(a, b, c, d) return Float64(b / c) end
function tmp = code(a, b, c, d) tmp = b / c; end
code[a_, b_, c_, d_] := N[(b / c), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{c}
\end{array}
Initial program 60.1%
Taylor expanded in c around inf
lower-/.f6441.7
Applied rewrites41.7%
(FPCore (a b c d) :precision binary64 (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (fabs(d) < fabs(c)) {
tmp = (b - (a * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (-a + (b * (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 = (b - (a * (d / c))) / (c + (d * (d / c)))
else
tmp = (-a + (b * (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 = (b - (a * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (-a + (b * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if math.fabs(d) < math.fabs(c): tmp = (b - (a * (d / c))) / (c + (d * (d / c))) else: tmp = (-a + (b * (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(b - Float64(a * Float64(d / c))) / Float64(c + Float64(d * Float64(d / c)))); else tmp = Float64(Float64(Float64(-a) + Float64(b * 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 = (b - (a * (d / c))) / (c + (d * (d / c))); else tmp = (-a + (b * (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[(b - N[(a * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c + N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[((-a) + N[(b * 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{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\
\end{array}
\end{array}
herbie shell --seed 2025051
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:alt
(! :herbie-platform default (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d))))))
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))