
(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 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));
}
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 (/ (- (* c (/ b (hypot c d))) (* (/ d (hypot c d)) a)) (hypot c d)))
double code(double a, double b, double c, double d) {
return ((c * (b / hypot(c, d))) - ((d / hypot(c, d)) * a)) / hypot(c, d);
}
public static double code(double a, double b, double c, double d) {
return ((c * (b / Math.hypot(c, d))) - ((d / Math.hypot(c, d)) * a)) / Math.hypot(c, d);
}
def code(a, b, c, d): return ((c * (b / math.hypot(c, d))) - ((d / math.hypot(c, d)) * a)) / math.hypot(c, d)
function code(a, b, c, d) return Float64(Float64(Float64(c * Float64(b / hypot(c, d))) - Float64(Float64(d / hypot(c, d)) * a)) / hypot(c, d)) end
function tmp = code(a, b, c, d) tmp = ((c * (b / hypot(c, d))) - ((d / hypot(c, d)) * a)) / hypot(c, d); end
code[a_, b_, c_, d_] := N[(N[(N[(c * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)} - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a}{\mathsf{hypot}\left(c, d\right)}
\end{array}
Initial program 58.2%
div-sub55.8%
sub-neg55.8%
associate-/l*58.1%
fma-def58.1%
add-sqr-sqrt58.1%
pow258.1%
fma-def58.1%
hypot-def58.1%
fma-def58.1%
add-sqr-sqrt58.1%
pow258.1%
fma-def58.1%
hypot-def58.1%
Applied egg-rr58.1%
sub-neg58.1%
associate-/r/54.7%
associate-*l/55.8%
*-commutative55.8%
*-commutative55.8%
*-lft-identity55.8%
times-frac56.0%
/-rgt-identity56.0%
Simplified56.0%
unpow256.0%
times-frac75.3%
Applied egg-rr75.3%
*-commutative75.3%
associate-*l/77.2%
Simplified77.2%
associate-*r/76.0%
*-commutative76.0%
associate-*r/74.2%
associate-*l/74.5%
unpow274.5%
frac-times95.4%
associate-*r/96.9%
sub-div97.2%
Applied egg-rr97.2%
Final simplification97.2%
(FPCore (a b c d) :precision binary64 (- (/ (* b (/ c (hypot c d))) (hypot c d)) (/ a d)))
double code(double a, double b, double c, double d) {
return ((b * (c / hypot(c, d))) / hypot(c, d)) - (a / d);
}
public static double code(double a, double b, double c, double d) {
return ((b * (c / Math.hypot(c, d))) / Math.hypot(c, d)) - (a / d);
}
def code(a, b, c, d): return ((b * (c / math.hypot(c, d))) / math.hypot(c, d)) - (a / d)
function code(a, b, c, d) return Float64(Float64(Float64(b * Float64(c / hypot(c, d))) / hypot(c, d)) - Float64(a / d)) end
function tmp = code(a, b, c, d) tmp = ((b * (c / hypot(c, d))) / hypot(c, d)) - (a / d); end
code[a_, b_, c_, d_] := N[(N[(N[(b * N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] - N[(a / d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)} - \frac{a}{d}
\end{array}
Initial program 58.2%
div-sub55.8%
sub-neg55.8%
associate-/l*58.1%
fma-def58.1%
add-sqr-sqrt58.1%
pow258.1%
fma-def58.1%
hypot-def58.1%
fma-def58.1%
add-sqr-sqrt58.1%
pow258.1%
fma-def58.1%
hypot-def58.1%
Applied egg-rr58.1%
sub-neg58.1%
associate-/r/54.7%
associate-*l/55.8%
*-commutative55.8%
*-commutative55.8%
*-lft-identity55.8%
times-frac56.0%
/-rgt-identity56.0%
Simplified56.0%
unpow256.0%
times-frac75.3%
Applied egg-rr75.3%
*-commutative75.3%
associate-*l/77.2%
Simplified77.2%
Taylor expanded in d around inf 62.4%
Final simplification62.4%
(FPCore (a b c d) :precision binary64 (/ (- b (* (/ d (hypot c d)) a)) (hypot c d)))
double code(double a, double b, double c, double d) {
return (b - ((d / hypot(c, d)) * a)) / hypot(c, d);
}
public static double code(double a, double b, double c, double d) {
return (b - ((d / Math.hypot(c, d)) * a)) / Math.hypot(c, d);
}
def code(a, b, c, d): return (b - ((d / math.hypot(c, d)) * a)) / math.hypot(c, d)
function code(a, b, c, d) return Float64(Float64(b - Float64(Float64(d / hypot(c, d)) * a)) / hypot(c, d)) end
function tmp = code(a, b, c, d) tmp = (b - ((d / hypot(c, d)) * a)) / hypot(c, d); end
code[a_, b_, c_, d_] := N[(N[(b - N[(N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a}{\mathsf{hypot}\left(c, d\right)}
\end{array}
Initial program 58.2%
div-sub55.8%
sub-neg55.8%
associate-/l*58.1%
fma-def58.1%
add-sqr-sqrt58.1%
pow258.1%
fma-def58.1%
hypot-def58.1%
fma-def58.1%
add-sqr-sqrt58.1%
pow258.1%
fma-def58.1%
hypot-def58.1%
Applied egg-rr58.1%
sub-neg58.1%
associate-/r/54.7%
associate-*l/55.8%
*-commutative55.8%
*-commutative55.8%
*-lft-identity55.8%
times-frac56.0%
/-rgt-identity56.0%
Simplified56.0%
unpow256.0%
times-frac75.3%
Applied egg-rr75.3%
*-commutative75.3%
associate-*l/77.2%
Simplified77.2%
associate-*r/76.0%
*-commutative76.0%
associate-*r/74.2%
associate-*l/74.5%
unpow274.5%
frac-times95.4%
associate-*r/96.9%
sub-div97.2%
Applied egg-rr97.2%
Taylor expanded in c around inf 61.2%
Final simplification61.2%
(FPCore (a b c d) :precision binary64 (- (/ b c) (/ a (* c (* c (/ 1.0 d))))))
double code(double a, double b, double c, double d) {
return (b / c) - (a / (c * (c * (1.0 / 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 / (c * (c * (1.0d0 / d))))
end function
public static double code(double a, double b, double c, double d) {
return (b / c) - (a / (c * (c * (1.0 / d))));
}
def code(a, b, c, d): return (b / c) - (a / (c * (c * (1.0 / d))))
function code(a, b, c, d) return Float64(Float64(b / c) - Float64(a / Float64(c * Float64(c * Float64(1.0 / d))))) end
function tmp = code(a, b, c, d) tmp = (b / c) - (a / (c * (c * (1.0 / d)))); end
code[a_, b_, c_, d_] := N[(N[(b / c), $MachinePrecision] - N[(a / N[(c * N[(c * N[(1.0 / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{c} - \frac{a}{c \cdot \left(c \cdot \frac{1}{d}\right)}
\end{array}
Initial program 58.2%
Taylor expanded in c around inf 50.7%
+-commutative50.7%
mul-1-neg50.7%
unsub-neg50.7%
associate-/l*52.5%
Simplified52.5%
div-inv52.5%
unpow252.5%
associate-*l*55.5%
Applied egg-rr55.5%
Final simplification55.5%
(FPCore (a b c d) :precision binary64 (- (/ b c) (/ a (* c (/ c d)))))
double code(double a, double b, double c, double d) {
return (b / c) - (a / (c * (c / 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 / (c * (c / d)))
end function
public static double code(double a, double b, double c, double d) {
return (b / c) - (a / (c * (c / d)));
}
def code(a, b, c, d): return (b / c) - (a / (c * (c / d)))
function code(a, b, c, d) return Float64(Float64(b / c) - Float64(a / Float64(c * Float64(c / d)))) end
function tmp = code(a, b, c, d) tmp = (b / c) - (a / (c * (c / d))); end
code[a_, b_, c_, d_] := N[(N[(b / c), $MachinePrecision] - N[(a / N[(c * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}
\end{array}
Initial program 58.2%
Taylor expanded in c around inf 50.7%
+-commutative50.7%
mul-1-neg50.7%
unsub-neg50.7%
associate-/l*52.5%
Simplified52.5%
unpow252.5%
*-un-lft-identity52.5%
times-frac55.5%
Applied egg-rr55.5%
Final simplification55.5%
(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}
\\
\frac{a}{d}
\end{array}
Initial program 58.2%
Taylor expanded in b around 0 36.0%
mul-1-neg36.0%
distribute-rgt-neg-in36.0%
Simplified36.0%
*-commutative36.0%
fma-def36.0%
add-sqr-sqrt36.0%
fma-def36.0%
hypot-udef36.0%
fma-def36.0%
hypot-udef36.0%
times-frac52.6%
add-sqr-sqrt26.4%
sqrt-unprod17.6%
sqr-neg17.6%
sqrt-prod7.1%
add-sqr-sqrt14.3%
Applied egg-rr14.3%
Taylor expanded in d around inf 8.2%
Final simplification8.2%
(FPCore (a b c d) :precision binary64 (/ b c))
double code(double a, double b, double c, double d) {
return b / 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 = 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 58.2%
Taylor expanded in c around inf 45.8%
Final simplification45.8%
(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 2024033
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:herbie-target
(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))))