
(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 9 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
(let* ((t_0 (fma d d (* c c))) (t_1 (/ (- b (* a (/ d c))) c)))
(if (<= c -3.2e+156)
t_1
(if (<= c -5.2e-76)
(fma (/ c t_0) b (/ (- (* a d)) t_0))
(if (<= c 1.8e-26) (/ (- (/ (* c b) d) a) d) t_1)))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, d, (c * c));
double t_1 = (b - (a * (d / c))) / c;
double tmp;
if (c <= -3.2e+156) {
tmp = t_1;
} else if (c <= -5.2e-76) {
tmp = fma((c / t_0), b, (-(a * d) / t_0));
} else if (c <= 1.8e-26) {
tmp = (((c * b) / d) - a) / d;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(d, d, Float64(c * c)) t_1 = Float64(Float64(b - Float64(a * Float64(d / c))) / c) tmp = 0.0 if (c <= -3.2e+156) tmp = t_1; elseif (c <= -5.2e-76) tmp = fma(Float64(c / t_0), b, Float64(Float64(-Float64(a * d)) / t_0)); elseif (c <= 1.8e-26) tmp = Float64(Float64(Float64(Float64(c * b) / d) - a) / d); else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b - N[(a * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -3.2e+156], t$95$1, If[LessEqual[c, -5.2e-76], N[(N[(c / t$95$0), $MachinePrecision] * b + N[((-N[(a * d), $MachinePrecision]) / t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.8e-26], N[(N[(N[(N[(c * b), $MachinePrecision] / d), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_1 := \frac{b - a \cdot \frac{d}{c}}{c}\\
\mathbf{if}\;c \leq -3.2 \cdot 10^{+156}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq -5.2 \cdot 10^{-76}:\\
\;\;\;\;\mathsf{fma}\left(\frac{c}{t\_0}, b, \frac{-a \cdot d}{t\_0}\right)\\
\mathbf{elif}\;c \leq 1.8 \cdot 10^{-26}:\\
\;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if c < -3.20000000000000002e156 or 1.8000000000000001e-26 < c Initial program 38.8%
Taylor expanded in c around inf
lower-/.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6484.7
Applied rewrites84.7%
Applied rewrites88.8%
if -3.20000000000000002e156 < c < -5.1999999999999999e-76Initial program 82.4%
lift-/.f64N/A
lift--.f64N/A
div-subN/A
sub-negN/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
distribute-neg-frac2N/A
lower-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
Applied rewrites84.3%
if -5.1999999999999999e-76 < c < 1.8000000000000001e-26Initial program 64.4%
Taylor expanded in c around inf
lower-/.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6431.1
Applied rewrites31.1%
Applied rewrites31.2%
Applied rewrites30.3%
Taylor expanded in c around 0
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
unpow2N/A
associate-/r*N/A
div-subN/A
unsub-negN/A
mul-1-negN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6482.8
Applied rewrites82.8%
Final simplification85.3%
(FPCore (a b c d)
:precision binary64
(if (<= c -3.2e+156)
(/ b c)
(if (<= c -2.5e-149)
(* b (/ c (fma d d (* c c))))
(if (<= c 1.25e-26)
(/ a (- d))
(if (<= c 7.5e+92) (/ (- (* c b) (* a d)) (* c c)) (/ b c))))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -3.2e+156) {
tmp = b / c;
} else if (c <= -2.5e-149) {
tmp = b * (c / fma(d, d, (c * c)));
} else if (c <= 1.25e-26) {
tmp = a / -d;
} else if (c <= 7.5e+92) {
tmp = ((c * b) - (a * d)) / (c * c);
} else {
tmp = b / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -3.2e+156) tmp = Float64(b / c); elseif (c <= -2.5e-149) tmp = Float64(b * Float64(c / fma(d, d, Float64(c * c)))); elseif (c <= 1.25e-26) tmp = Float64(a / Float64(-d)); elseif (c <= 7.5e+92) tmp = Float64(Float64(Float64(c * b) - Float64(a * d)) / Float64(c * c)); else tmp = Float64(b / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -3.2e+156], N[(b / c), $MachinePrecision], If[LessEqual[c, -2.5e-149], N[(b * N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.25e-26], N[(a / (-d)), $MachinePrecision], If[LessEqual[c, 7.5e+92], N[(N[(N[(c * b), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], N[(b / c), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3.2 \cdot 10^{+156}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{elif}\;c \leq -2.5 \cdot 10^{-149}:\\
\;\;\;\;b \cdot \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{elif}\;c \leq 1.25 \cdot 10^{-26}:\\
\;\;\;\;\frac{a}{-d}\\
\mathbf{elif}\;c \leq 7.5 \cdot 10^{+92}:\\
\;\;\;\;\frac{c \cdot b - a \cdot d}{c \cdot c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{c}\\
\end{array}
\end{array}
if c < -3.20000000000000002e156 or 7.49999999999999946e92 < c Initial program 35.4%
Taylor expanded in c around inf
lower-/.f6475.3
Applied rewrites75.3%
if -3.20000000000000002e156 < c < -2.49999999999999984e-149Initial program 73.4%
Taylor expanded in c around inf
lower-/.f6439.5
Applied rewrites39.5%
Taylor expanded in b around inf
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6454.5
Applied rewrites54.5%
if -2.49999999999999984e-149 < c < 1.25000000000000005e-26Initial program 73.1%
Taylor expanded in c around 0
mul-1-negN/A
distribute-neg-frac2N/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6469.3
Applied rewrites69.3%
if 1.25000000000000005e-26 < c < 7.49999999999999946e92Initial program 75.7%
Taylor expanded in c around inf
unpow2N/A
lower-*.f6452.7
Applied rewrites52.7%
Final simplification66.0%
(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 2024230
(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))))