
(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 (/ (+ (* 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}
Initial program 54.2%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (+ (* c c) (* d d))) (t_1 (/ (* b d) t_0))) (if (<= b -1.9e-56) t_1 (if (<= b 0.00055) (/ (* a c) t_0) t_1))))
double code(double a, double b, double c, double d) {
double t_0 = (c * c) + (d * d);
double t_1 = (b * d) / t_0;
double tmp;
if (b <= -1.9e-56) {
tmp = t_1;
} else if (b <= 0.00055) {
tmp = (a * c) / t_0;
} else {
tmp = t_1;
}
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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = (c * c) + (d * d)
t_1 = (b * d) / t_0
if (b <= (-1.9d-56)) then
tmp = t_1
else if (b <= 0.00055d0) then
tmp = (a * c) / t_0
else
tmp = t_1
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = (c * c) + (d * d);
double t_1 = (b * d) / t_0;
double tmp;
if (b <= -1.9e-56) {
tmp = t_1;
} else if (b <= 0.00055) {
tmp = (a * c) / t_0;
} else {
tmp = t_1;
}
return tmp;
}
def code(a, b, c, d): t_0 = (c * c) + (d * d) t_1 = (b * d) / t_0 tmp = 0 if b <= -1.9e-56: tmp = t_1 elif b <= 0.00055: tmp = (a * c) / t_0 else: tmp = t_1 return tmp
function code(a, b, c, d) t_0 = Float64(Float64(c * c) + Float64(d * d)) t_1 = Float64(Float64(b * d) / t_0) tmp = 0.0 if (b <= -1.9e-56) tmp = t_1; elseif (b <= 0.00055) tmp = Float64(Float64(a * c) / t_0); else tmp = t_1; end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = (c * c) + (d * d); t_1 = (b * d) / t_0; tmp = 0.0; if (b <= -1.9e-56) tmp = t_1; elseif (b <= 0.00055) tmp = (a * c) / t_0; else tmp = t_1; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * d), $MachinePrecision] / t$95$0), $MachinePrecision]}, If[LessEqual[b, -1.9e-56], t$95$1, If[LessEqual[b, 0.00055], N[(N[(a * c), $MachinePrecision] / t$95$0), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot c + d \cdot d\\
t_1 := \frac{b \cdot d}{t\_0}\\
\mathbf{if}\;b \leq -1.9 \cdot 10^{-56}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;b \leq 0.00055:\\
\;\;\;\;\frac{a \cdot c}{t\_0}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if b < -1.9000000000000001e-56 or 5.50000000000000033e-4 < b Initial program 46.4%
Taylor expanded in a around 0
Applied rewrites39.1%
if -1.9000000000000001e-56 < b < 5.50000000000000033e-4Initial program 63.5%
Taylor expanded in a around 0
Applied rewrites57.2%
(FPCore (a b c d) :precision binary64 (/ (* a c) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return (a * c) / ((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) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return (a * c) / ((c * c) + (d * d));
}
def code(a, b, c, d): return (a * c) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(a * c) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = (a * c) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(a * c), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c}{c \cdot c + d \cdot d}
\end{array}
Initial program 54.2%
Taylor expanded in a around 0
Applied rewrites35.7%
(FPCore (a b c d) :precision binary64 (* c c))
double code(double a, double b, double c, double d) {
return c * 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 = c * c
end function
public static double code(double a, double b, double c, double d) {
return c * c;
}
def code(a, b, c, d): return c * c
function code(a, b, c, d) return Float64(c * c) end
function tmp = code(a, b, c, d) tmp = c * c; end
code[a_, b_, c_, d_] := N[(c * c), $MachinePrecision]
\begin{array}{l}
\\
c \cdot c
\end{array}
Initial program 54.2%
Taylor expanded in a around 0
Applied rewrites3.8%
Taylor expanded in a around 0
Applied rewrites4.9%
Taylor expanded in a around -inf
Applied rewrites5.0%
(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}
\\
a \cdot c
\end{array}
Initial program 54.2%
Taylor expanded in a around 0
Applied rewrites3.8%
Taylor expanded in a around 0
Applied rewrites4.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;
}
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 2024321
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:pre (TRUE)
: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))))