?

Average Error: 25.9 → 16.1
Time: 7.5s
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 -9 \cdot 10^{+87}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.3 \cdot 10^{-82}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 3.3 \cdot 10^{-133}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 1.2 \cdot 10^{+125}:\\ \;\;\;\;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 -9e+87)
     t_0
     (if (<= d -1.3e-82)
       t_1
       (if (<= d 3.3e-133) (/ b c) (if (<= d 1.2e+125) 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 <= -9e+87) {
		tmp = t_0;
	} else if (d <= -1.3e-82) {
		tmp = t_1;
	} else if (d <= 3.3e-133) {
		tmp = b / c;
	} else if (d <= 1.2e+125) {
		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 <= (-9d+87)) then
        tmp = t_0
    else if (d <= (-1.3d-82)) then
        tmp = t_1
    else if (d <= 3.3d-133) then
        tmp = b / c
    else if (d <= 1.2d+125) 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 <= -9e+87) {
		tmp = t_0;
	} else if (d <= -1.3e-82) {
		tmp = t_1;
	} else if (d <= 3.3e-133) {
		tmp = b / c;
	} else if (d <= 1.2e+125) {
		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 <= -9e+87:
		tmp = t_0
	elif d <= -1.3e-82:
		tmp = t_1
	elif d <= 3.3e-133:
		tmp = b / c
	elif d <= 1.2e+125:
		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 <= -9e+87)
		tmp = t_0;
	elseif (d <= -1.3e-82)
		tmp = t_1;
	elseif (d <= 3.3e-133)
		tmp = Float64(b / c);
	elseif (d <= 1.2e+125)
		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 <= -9e+87)
		tmp = t_0;
	elseif (d <= -1.3e-82)
		tmp = t_1;
	elseif (d <= 3.3e-133)
		tmp = b / c;
	elseif (d <= 1.2e+125)
		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, -9e+87], t$95$0, If[LessEqual[d, -1.3e-82], t$95$1, If[LessEqual[d, 3.3e-133], N[(b / c), $MachinePrecision], If[LessEqual[d, 1.2e+125], 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 -9 \cdot 10^{+87}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d \leq -1.3 \cdot 10^{-82}:\\
\;\;\;\;t_1\\

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

\mathbf{elif}\;d \leq 1.2 \cdot 10^{+125}:\\
\;\;\;\;t_1\\

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


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original25.9
Target0.5
Herbie16.1
\[\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 < -9.0000000000000005e87 or 1.2e125 < d

    1. Initial program 39.8

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

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

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

      [Start]15.6

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

      rational.json-simplify-2 [=>]15.6

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

      rational.json-simplify-9 [=>]15.6

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

    if -9.0000000000000005e87 < d < -1.3e-82 or 3.30000000000000009e-133 < d < 1.2e125

    1. Initial program 15.9

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

    if -1.3e-82 < d < 3.30000000000000009e-133

    1. Initial program 21.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -9 \cdot 10^{+87}:\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{elif}\;d \leq -1.3 \cdot 10^{-82}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 3.3 \cdot 10^{-133}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 1.2 \cdot 10^{+125}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;-\frac{a}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error23.2
Cost1364
\[\begin{array}{l} t_0 := \frac{c \cdot b}{c \cdot c + d \cdot d}\\ t_1 := -\frac{a}{d}\\ \mathbf{if}\;c \leq -1.45 \cdot 10^{+113}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -2.5 \cdot 10^{+44}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -150:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 8 \cdot 10^{-45}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 1.15 \cdot 10^{+46}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 2
Error22.6
Cost520
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -9.5 \cdot 10^{-33}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3 \cdot 10^{-5}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error37.8
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

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