
(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));
}
real(8) function code(a, b, c, d)
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 5 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));
}
real(8) function code(a, b, c, d)
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 (if (<= d -4.2e+30) (/ 1.0 (/ d (fma c (/ a d) b))) (if (<= d 4.1e+94) (/ (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 (d <= -4.2e+30) {
tmp = 1.0 / (d / fma(c, (a / d), b));
} else if (d <= 4.1e+94) {
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 (d <= -4.2e+30) tmp = Float64(1.0 / Float64(d / fma(c, Float64(a / d), b))); elseif (d <= 4.1e+94) 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[LessEqual[d, -4.2e+30], N[(1.0 / N[(d / N[(c * N[(a / d), $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 4.1e+94], 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}\;d \leq -4.2 \cdot 10^{+30}:\\
\;\;\;\;\frac{1}{\frac{d}{\mathsf{fma}\left(c, \frac{a}{d}, b\right)}}\\
\mathbf{elif}\;d \leq 4.1 \cdot 10^{+94}:\\
\;\;\;\;\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 d < -4.2e30Initial program 49.3%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6484.9
Applied rewrites84.9%
Applied rewrites88.7%
if -4.2e30 < d < 4.10000000000000031e94Initial program 67.5%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6485.1
Applied rewrites85.1%
if 4.10000000000000031e94 < d Initial program 40.2%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6488.2
Applied rewrites88.2%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma a (/ c d) b) d))) (if (<= d -4.2e+30) t_0 (if (<= d 4.1e+94) (/ (fma b (/ d c) a) c) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(a, (c / d), b) / d;
double tmp;
if (d <= -4.2e+30) {
tmp = t_0;
} else if (d <= 4.1e+94) {
tmp = fma(b, (d / c), a) / c;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(a, Float64(c / d), b) / d) tmp = 0.0 if (d <= -4.2e+30) tmp = t_0; elseif (d <= 4.1e+94) tmp = Float64(fma(b, Float64(d / c), a) / c); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -4.2e+30], t$95$0, If[LessEqual[d, 4.1e+94], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -4.2 \cdot 10^{+30}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 4.1 \cdot 10^{+94}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -4.2e30 or 4.10000000000000031e94 < d Initial program 45.8%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6486.2
Applied rewrites86.2%
if -4.2e30 < d < 4.10000000000000031e94Initial program 67.5%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6485.1
Applied rewrites85.1%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma a (/ c d) b) d))) (if (<= d -1.25e-92) t_0 (if (<= d 1150000.0) (/ a c) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(a, (c / d), b) / d;
double tmp;
if (d <= -1.25e-92) {
tmp = t_0;
} else if (d <= 1150000.0) {
tmp = a / c;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(a, Float64(c / d), b) / d) tmp = 0.0 if (d <= -1.25e-92) tmp = t_0; elseif (d <= 1150000.0) tmp = Float64(a / c); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -1.25e-92], t$95$0, If[LessEqual[d, 1150000.0], N[(a / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{if}\;d \leq -1.25 \cdot 10^{-92}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 1150000:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -1.25000000000000003e-92 or 1.15e6 < d Initial program 49.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6475.6
Applied rewrites75.6%
if -1.25000000000000003e-92 < d < 1.15e6Initial program 69.5%
Taylor expanded in c around inf
lower-/.f6474.8
Applied rewrites74.8%
(FPCore (a b c d) :precision binary64 (if (<= d -4.2e+30) (/ b d) (if (<= d 6.2e+57) (/ a c) (/ b d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -4.2e+30) {
tmp = b / d;
} else if (d <= 6.2e+57) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
real(8) function code(a, b, c, d)
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 <= (-4.2d+30)) then
tmp = b / d
else if (d <= 6.2d+57) 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 <= -4.2e+30) {
tmp = b / d;
} else if (d <= 6.2e+57) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -4.2e+30: tmp = b / d elif d <= 6.2e+57: tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if (d <= -4.2e+30) tmp = Float64(b / d); elseif (d <= 6.2e+57) 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 <= -4.2e+30) tmp = b / d; elseif (d <= 6.2e+57) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -4.2e+30], N[(b / d), $MachinePrecision], If[LessEqual[d, 6.2e+57], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -4.2 \cdot 10^{+30}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq 6.2 \cdot 10^{+57}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -4.2e30 or 6.20000000000000026e57 < d Initial program 46.1%
Taylor expanded in c around 0
lower-/.f6477.0
Applied rewrites77.0%
if -4.2e30 < d < 6.20000000000000026e57Initial program 68.2%
Taylor expanded in c around inf
lower-/.f6468.1
Applied rewrites68.1%
(FPCore (a b c d) :precision binary64 (/ a c))
double code(double a, double b, double c, double d) {
return a / c;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a / c
end function
public static double code(double a, double b, double c, double d) {
return a / c;
}
def code(a, b, c, d): return a / c
function code(a, b, c, d) return Float64(a / c) end
function tmp = code(a, b, c, d) tmp = a / c; end
code[a_, b_, c_, d_] := N[(a / c), $MachinePrecision]
\begin{array}{l}
\\
\frac{a}{c}
\end{array}
Initial program 58.9%
Taylor expanded in c around inf
lower-/.f6446.5
Applied rewrites46.5%
(FPCore (a b c d) :precision binary64 (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (fabs(d) < fabs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
real(8) function code(a, b, c, d)
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 2024232
(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))))