?

Average Error: 26.2 → 15.6
Time: 5.1s
Precision: binary64
Cost: 1488

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ t_1 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.02 \cdot 10^{+145}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.25 \cdot 10^{-158}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{-119}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+143}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (- (/ a d))) (t_1 (/ (- (* b c) (* a d)) (+ (* c c) (* d d)))))
   (if (<= d -1.02e+145)
     t_0
     (if (<= d -1.25e-158)
       t_1
       (if (<= d 2.5e-119) (/ b c) (if (<= d 4.2e+143) t_1 t_0))))))
double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double t_0 = -(a / d);
	double t_1 = ((b * c) - (a * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -1.02e+145) {
		tmp = t_0;
	} else if (d <= -1.25e-158) {
		tmp = t_1;
	} else if (d <= 2.5e-119) {
		tmp = b / c;
	} else if (d <= 4.2e+143) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	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
    code = ((b * c) - (a * d)) / ((c * c) + (d * d))
end function
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 = -(a / d)
    t_1 = ((b * c) - (a * d)) / ((c * c) + (d * d))
    if (d <= (-1.02d+145)) then
        tmp = t_0
    else if (d <= (-1.25d-158)) then
        tmp = t_1
    else if (d <= 2.5d-119) then
        tmp = b / c
    else if (d <= 4.2d+143) then
        tmp = t_1
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	double t_0 = -(a / d);
	double t_1 = ((b * c) - (a * d)) / ((c * c) + (d * d));
	double tmp;
	if (d <= -1.02e+145) {
		tmp = t_0;
	} else if (d <= -1.25e-158) {
		tmp = t_1;
	} else if (d <= 2.5e-119) {
		tmp = b / c;
	} else if (d <= 4.2e+143) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(a, b, c, d):
	return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	t_0 = -(a / d)
	t_1 = ((b * c) - (a * d)) / ((c * c) + (d * d))
	tmp = 0
	if d <= -1.02e+145:
		tmp = t_0
	elif d <= -1.25e-158:
		tmp = t_1
	elif d <= 2.5e-119:
		tmp = b / c
	elif d <= 4.2e+143:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
function code(a, b, c, d)
	return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function code(a, b, c, d)
	t_0 = Float64(-Float64(a / d))
	t_1 = Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (d <= -1.02e+145)
		tmp = t_0;
	elseif (d <= -1.25e-158)
		tmp = t_1;
	elseif (d <= 2.5e-119)
		tmp = Float64(b / c);
	elseif (d <= 4.2e+143)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp = code(a, b, c, d)
	tmp = ((b * c) - (a * d)) / ((c * c) + (d * d));
end
function tmp_2 = code(a, b, c, d)
	t_0 = -(a / d);
	t_1 = ((b * c) - (a * d)) / ((c * c) + (d * d));
	tmp = 0.0;
	if (d <= -1.02e+145)
		tmp = t_0;
	elseif (d <= -1.25e-158)
		tmp = t_1;
	elseif (d <= 2.5e-119)
		tmp = b / c;
	elseif (d <= 4.2e+143)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
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]
code[a_, b_, c_, d_] := Block[{t$95$0 = (-N[(a / d), $MachinePrecision])}, Block[{t$95$1 = N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -1.02e+145], t$95$0, If[LessEqual[d, -1.25e-158], t$95$1, If[LessEqual[d, 2.5e-119], N[(b / c), $MachinePrecision], If[LessEqual[d, 4.2e+143], t$95$1, t$95$0]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := -\frac{a}{d}\\
t_1 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{if}\;d \leq -1.02 \cdot 10^{+145}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq -1.25 \cdot 10^{-158}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq 2.5 \cdot 10^{-119}:\\
\;\;\;\;\frac{b}{c}\\

\mathbf{elif}\;d \leq 4.2 \cdot 10^{+143}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.2
Target0.4
Herbie15.6
\[\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} \]

Derivation?

  1. Split input into 3 regimes
  2. if d < -1.02e145 or 4.19999999999999975e143 < d

    1. Initial program 43.2

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around 0 13.2

      \[\leadsto \color{blue}{-1 \cdot \frac{a}{d}} \]
    3. Simplified13.2

      \[\leadsto \color{blue}{-\frac{a}{d}} \]
      Proof

      [Start]13.2

      \[ -1 \cdot \frac{a}{d} \]

      rational_best-simplify-3 [=>]13.2

      \[ \color{blue}{\frac{a}{d} \cdot -1} \]

      rational_best-simplify-17 [=>]13.2

      \[ \color{blue}{-\frac{a}{d}} \]

    if -1.02e145 < d < -1.24999999999999993e-158 or 2.49999999999999996e-119 < d < 4.19999999999999975e143

    1. Initial program 17.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]

    if -1.24999999999999993e-158 < d < 2.49999999999999996e-119

    1. Initial program 22.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around inf 15.2

      \[\leadsto \color{blue}{\frac{b}{c}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification15.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -1.02 \cdot 10^{+145}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;d \leq -1.25 \cdot 10^{-158}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{-119}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+143}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;-\frac{a}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error22.9
Cost1296
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -6.4 \cdot 10^{-53}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -3.4 \cdot 10^{-111}:\\ \;\;\;\;\frac{c \cdot b}{t_0}\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{-132}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 6.6 \cdot 10^{+78}:\\ \;\;\;\;\frac{a \cdot \left(-d\right)}{t_0}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error24.1
Cost968
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -2.25 \cdot 10^{-40}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-109}:\\ \;\;\;\;\frac{c \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-110}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error23.8
Cost520
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{-110}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Error37.5
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

herbie shell --seed 2023104 
(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))))