
(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 6 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
(let* ((t_0 (/ (+ (* b d) (* c a)) (+ (* d d) (* c c))))
(t_1 (/ (fma (/ a d) c b) d)))
(if (<= d -4.3e+52)
t_1
(if (<= d -2e-83)
t_0
(if (<= d 4.15e-131)
(/ (fma b (/ d c) a) c)
(if (<= d 7.5e+77) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = ((b * d) + (c * a)) / ((d * d) + (c * c));
double t_1 = fma((a / d), c, b) / d;
double tmp;
if (d <= -4.3e+52) {
tmp = t_1;
} else if (d <= -2e-83) {
tmp = t_0;
} else if (d <= 4.15e-131) {
tmp = fma(b, (d / c), a) / c;
} else if (d <= 7.5e+77) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(Float64(Float64(b * d) + Float64(c * a)) / Float64(Float64(d * d) + Float64(c * c))) t_1 = Float64(fma(Float64(a / d), c, b) / d) tmp = 0.0 if (d <= -4.3e+52) tmp = t_1; elseif (d <= -2e-83) tmp = t_0; elseif (d <= 4.15e-131) tmp = Float64(fma(b, Float64(d / c), a) / c); elseif (d <= 7.5e+77) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(b * d), $MachinePrecision] + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(N[(d * d), $MachinePrecision] + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -4.3e+52], t$95$1, If[LessEqual[d, -2e-83], t$95$0, If[LessEqual[d, 4.15e-131], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 7.5e+77], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{b \cdot d + c \cdot a}{d \cdot d + c \cdot c}\\
t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -4.3 \cdot 10^{+52}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -2 \cdot 10^{-83}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 4.15 \cdot 10^{-131}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{+77}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -4.3e52 or 7.49999999999999955e77 < d Initial program 38.2%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6488.1
Applied rewrites88.1%
if -4.3e52 < d < -2.0000000000000001e-83 or 4.14999999999999982e-131 < d < 7.49999999999999955e77Initial program 80.8%
if -2.0000000000000001e-83 < d < 4.14999999999999982e-131Initial program 69.6%
Taylor expanded in c around inf
lower-/.f6471.8
Applied rewrites71.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6490.6
Applied rewrites90.6%
Final simplification86.8%
(FPCore (a b c d) :precision binary64 (if (<= d -0.0035) (/ (fma (/ a d) c b) d) (if (<= d 7.5e+120) (/ (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 <= -0.0035) {
tmp = fma((a / d), c, b) / d;
} else if (d <= 7.5e+120) {
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 <= -0.0035) tmp = Float64(fma(Float64(a / d), c, b) / d); elseif (d <= 7.5e+120) 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, -0.0035], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 7.5e+120], 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 -0.0035:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{+120}:\\
\;\;\;\;\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 < -0.00350000000000000007Initial program 44.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.6
Applied rewrites82.6%
if -0.00350000000000000007 < d < 7.5000000000000006e120Initial program 73.8%
Taylor expanded in c around inf
lower-/.f6463.2
Applied rewrites63.2%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6479.3
Applied rewrites79.3%
if 7.5000000000000006e120 < d Initial program 40.5%
Taylor expanded in c around inf
lower-/.f649.3
Applied rewrites9.3%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6494.2
Applied rewrites94.2%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma a (/ c d) b) d))) (if (<= d -0.0035) t_0 (if (<= d 7.5e+120) (/ (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 <= -0.0035) {
tmp = t_0;
} else if (d <= 7.5e+120) {
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 <= -0.0035) tmp = t_0; elseif (d <= 7.5e+120) 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, -0.0035], t$95$0, If[LessEqual[d, 7.5e+120], 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 -0.0035:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{+120}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -0.00350000000000000007 or 7.5000000000000006e120 < d Initial program 43.2%
Taylor expanded in c around inf
lower-/.f6414.4
Applied rewrites14.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6485.5
Applied rewrites85.5%
if -0.00350000000000000007 < d < 7.5000000000000006e120Initial program 73.8%
Taylor expanded in c around inf
lower-/.f6463.2
Applied rewrites63.2%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6479.3
Applied rewrites79.3%
(FPCore (a b c d) :precision binary64 (if (<= c -1.02e+40) (/ a c) (if (<= c 2.55e-12) (/ (fma a (/ c d) b) d) (/ 1.0 (/ c a)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1.02e+40) {
tmp = a / c;
} else if (c <= 2.55e-12) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = 1.0 / (c / a);
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -1.02e+40) tmp = Float64(a / c); elseif (c <= 2.55e-12) tmp = Float64(fma(a, Float64(c / d), b) / d); else tmp = Float64(1.0 / Float64(c / a)); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -1.02e+40], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.55e-12], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], N[(1.0 / N[(c / a), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.02 \cdot 10^{+40}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.55 \cdot 10^{-12}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{c}{a}}\\
\end{array}
\end{array}
if c < -1.02e40Initial program 43.6%
Taylor expanded in c around inf
lower-/.f6469.1
Applied rewrites69.1%
if -1.02e40 < c < 2.54999999999999984e-12Initial program 73.4%
Taylor expanded in c around inf
lower-/.f6418.8
Applied rewrites18.8%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6480.2
Applied rewrites80.2%
if 2.54999999999999984e-12 < c Initial program 55.1%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6455.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6455.0
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6455.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f6455.0
Applied rewrites55.0%
Taylor expanded in c around inf
lower-/.f6467.3
Applied rewrites67.3%
(FPCore (a b c d) :precision binary64 (if (<= d -1.42e+57) (/ b d) (if (<= d 9.8e+100) (/ a c) (/ b d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -1.42e+57) {
tmp = b / d;
} else if (d <= 9.8e+100) {
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 <= (-1.42d+57)) then
tmp = b / d
else if (d <= 9.8d+100) 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 <= -1.42e+57) {
tmp = b / d;
} else if (d <= 9.8e+100) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -1.42e+57: tmp = b / d elif d <= 9.8e+100: tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if (d <= -1.42e+57) tmp = Float64(b / d); elseif (d <= 9.8e+100) 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 <= -1.42e+57) tmp = b / d; elseif (d <= 9.8e+100) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -1.42e+57], N[(b / d), $MachinePrecision], If[LessEqual[d, 9.8e+100], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.42 \cdot 10^{+57}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq 9.8 \cdot 10^{+100}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -1.42e57 or 9.79999999999999934e100 < d Initial program 39.9%
Taylor expanded in c around 0
lower-/.f6478.3
Applied rewrites78.3%
if -1.42e57 < d < 9.79999999999999934e100Initial program 73.0%
Taylor expanded in c around inf
lower-/.f6460.6
Applied rewrites60.6%
(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 61.8%
Taylor expanded in c around inf
lower-/.f6443.9
Applied rewrites43.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 2024240
(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))))