
(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));
}
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 = ((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 6 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));
}
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 = ((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 (/ (- (* 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));
}
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 = ((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}
Initial program 59.3%
(FPCore (a b c d) :precision binary64 (/ (* b c) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return (b * 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 = (b * c) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return (b * c) / ((c * c) + (d * d));
}
def code(a, b, c, d): return (b * c) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(b * c) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = (b * c) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(b * c), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b \cdot c}{c \cdot c + d \cdot d}
\end{array}
Initial program 59.3%
Taylor expanded in a around 0
Applied rewrites39.1%
(FPCore (a b c d) :precision binary64 (/ (* a d) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return (a * 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 * d) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return (a * d) / ((c * c) + (d * d));
}
def code(a, b, c, d): return (a * d) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(a * d) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = (a * d) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(a * d), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot d}{c \cdot c + d \cdot d}
\end{array}
Initial program 59.3%
Taylor expanded in a around 0
Applied rewrites19.9%
(FPCore (a b c d) :precision binary64 (if (<= c -4.2e+103) (* a d) (* c c)))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -4.2e+103) {
tmp = a * d;
} else {
tmp = c * c;
}
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 (c <= (-4.2d+103)) then
tmp = a * d
else
tmp = c * c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (c <= -4.2e+103) {
tmp = a * d;
} else {
tmp = c * c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if c <= -4.2e+103: tmp = a * d else: tmp = c * c return tmp
function code(a, b, c, d) tmp = 0.0 if (c <= -4.2e+103) tmp = Float64(a * d); else tmp = Float64(c * c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (c <= -4.2e+103) tmp = a * d; else tmp = c * c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[c, -4.2e+103], N[(a * d), $MachinePrecision], N[(c * c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -4.2 \cdot 10^{+103}:\\
\;\;\;\;a \cdot d\\
\mathbf{else}:\\
\;\;\;\;c \cdot c\\
\end{array}
\end{array}
if c < -4.2000000000000003e103Initial program 43.8%
Taylor expanded in a around 0
Applied rewrites2.8%
Taylor expanded in a around 0
Applied rewrites11.2%
if -4.2000000000000003e103 < c Initial program 63.0%
Taylor expanded in a around 0
Applied rewrites4.0%
Taylor expanded in a around 0
Applied rewrites3.0%
Taylor expanded in b around 0
Applied rewrites5.3%
(FPCore (a b c d) :precision binary64 (* d d))
double code(double a, double b, double c, double d) {
return 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 = d * d
end function
public static double code(double a, double b, double c, double d) {
return d * d;
}
def code(a, b, c, d): return d * d
function code(a, b, c, d) return Float64(d * d) end
function tmp = code(a, b, c, d) tmp = d * d; end
code[a_, b_, c_, d_] := N[(d * d), $MachinePrecision]
\begin{array}{l}
\\
d \cdot d
\end{array}
Initial program 59.3%
Taylor expanded in a around 0
Applied rewrites3.8%
Taylor expanded in a around 0
Applied rewrites4.6%
Taylor expanded in b around inf
Applied rewrites6.0%
(FPCore (a b c d) :precision binary64 (* a d))
double code(double a, double b, double c, double d) {
return a * 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 * d
end function
public static double code(double a, double b, double c, double d) {
return a * d;
}
def code(a, b, c, d): return a * d
function code(a, b, c, d) return Float64(a * d) end
function tmp = code(a, b, c, d) tmp = a * d; end
code[a_, b_, c_, d_] := N[(a * d), $MachinePrecision]
\begin{array}{l}
\\
a \cdot d
\end{array}
Initial program 59.3%
Taylor expanded in a around 0
Applied rewrites3.8%
Taylor expanded in a around 0
Applied rewrites4.6%
(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;
}
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 = (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 2024321
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:pre (TRUE)
: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))))