FastMath dist4

Percentage Accurate: 87.6% → 100.0%
Time: 5.5s
Alternatives: 12
Speedup: 1.7×

Specification

?
\[\begin{array}{l} \\ \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))
double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
end function
public static double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
def code(d1, d2, d3, d4):
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
function code(d1, d2, d3, d4)
	return Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1))
end
function tmp = code(d1, d2, d3, d4)
	tmp = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
end
code[d1_, d2_, d3_, d4_] := N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 12 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 87.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))
double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
end function
public static double code(double d1, double d2, double d3, double d4) {
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
}
def code(d1, d2, d3, d4):
	return (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1)
function code(d1, d2, d3, d4)
	return Float64(Float64(Float64(Float64(d1 * d2) - Float64(d1 * d3)) + Float64(d4 * d1)) - Float64(d1 * d1))
end
function tmp = code(d1, d2, d3, d4)
	tmp = (((d1 * d2) - (d1 * d3)) + (d4 * d1)) - (d1 * d1);
end
code[d1_, d2_, d3_, d4_] := N[(N[(N[(N[(d1 * d2), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d4 * d1), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1
\end{array}

Alternative 1: 100.0% accurate, 1.7× speedup?

\[\begin{array}{l} \\ d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right) \end{array} \]
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 (+ (- d2 d3) (- d4 d1))))
double code(double d1, double d2, double d3, double d4) {
	return d1 * ((d2 - d3) + (d4 - d1));
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = d1 * ((d2 - d3) + (d4 - d1))
end function
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * ((d2 - d3) + (d4 - d1));
}
def code(d1, d2, d3, d4):
	return d1 * ((d2 - d3) + (d4 - d1))
function code(d1, d2, d3, d4)
	return Float64(d1 * Float64(Float64(d2 - d3) + Float64(d4 - d1)))
end
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * ((d2 - d3) + (d4 - d1));
end
code[d1_, d2_, d3_, d4_] := N[(d1 * N[(N[(d2 - d3), $MachinePrecision] + N[(d4 - d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)
\end{array}
Derivation
  1. Initial program 89.4%

    \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
  2. Step-by-step derivation
    1. associate--l+89.4%

      \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
    2. distribute-lft-out--90.6%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
    3. distribute-rgt-out--93.7%

      \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
    4. distribute-lft-out100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
  4. Final simplification100.0%

    \[\leadsto d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right) \]

Alternative 2: 67.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := d1 \cdot \left(d4 - d1\right)\\ t_1 := d1 \cdot \left(-d3\right)\\ t_2 := d1 \cdot \left(d2 + d4\right)\\ \mathbf{if}\;d3 \leq -1.9 \cdot 10^{+91}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d3 \leq -1.5 \cdot 10^{-12}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d3 \leq -3.35 \cdot 10^{-164}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq 10^{-107}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d3 \leq 4 \cdot 10^{+43}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq 1.6 \cdot 10^{+179}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d4 d1))) (t_1 (* d1 (- d3))) (t_2 (* d1 (+ d2 d4))))
   (if (<= d3 -1.9e+91)
     t_1
     (if (<= d3 -1.5e-12)
       t_2
       (if (<= d3 -3.35e-164)
         t_0
         (if (<= d3 1e-107)
           t_2
           (if (<= d3 4e+43) t_0 (if (<= d3 1.6e+179) t_2 t_1))))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d4 - d1);
	double t_1 = d1 * -d3;
	double t_2 = d1 * (d2 + d4);
	double tmp;
	if (d3 <= -1.9e+91) {
		tmp = t_1;
	} else if (d3 <= -1.5e-12) {
		tmp = t_2;
	} else if (d3 <= -3.35e-164) {
		tmp = t_0;
	} else if (d3 <= 1e-107) {
		tmp = t_2;
	} else if (d3 <= 4e+43) {
		tmp = t_0;
	} else if (d3 <= 1.6e+179) {
		tmp = t_2;
	} else {
		tmp = t_1;
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = d1 * (d4 - d1)
    t_1 = d1 * -d3
    t_2 = d1 * (d2 + d4)
    if (d3 <= (-1.9d+91)) then
        tmp = t_1
    else if (d3 <= (-1.5d-12)) then
        tmp = t_2
    else if (d3 <= (-3.35d-164)) then
        tmp = t_0
    else if (d3 <= 1d-107) then
        tmp = t_2
    else if (d3 <= 4d+43) then
        tmp = t_0
    else if (d3 <= 1.6d+179) then
        tmp = t_2
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d4 - d1);
	double t_1 = d1 * -d3;
	double t_2 = d1 * (d2 + d4);
	double tmp;
	if (d3 <= -1.9e+91) {
		tmp = t_1;
	} else if (d3 <= -1.5e-12) {
		tmp = t_2;
	} else if (d3 <= -3.35e-164) {
		tmp = t_0;
	} else if (d3 <= 1e-107) {
		tmp = t_2;
	} else if (d3 <= 4e+43) {
		tmp = t_0;
	} else if (d3 <= 1.6e+179) {
		tmp = t_2;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = d1 * (d4 - d1)
	t_1 = d1 * -d3
	t_2 = d1 * (d2 + d4)
	tmp = 0
	if d3 <= -1.9e+91:
		tmp = t_1
	elif d3 <= -1.5e-12:
		tmp = t_2
	elif d3 <= -3.35e-164:
		tmp = t_0
	elif d3 <= 1e-107:
		tmp = t_2
	elif d3 <= 4e+43:
		tmp = t_0
	elif d3 <= 1.6e+179:
		tmp = t_2
	else:
		tmp = t_1
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d4 - d1))
	t_1 = Float64(d1 * Float64(-d3))
	t_2 = Float64(d1 * Float64(d2 + d4))
	tmp = 0.0
	if (d3 <= -1.9e+91)
		tmp = t_1;
	elseif (d3 <= -1.5e-12)
		tmp = t_2;
	elseif (d3 <= -3.35e-164)
		tmp = t_0;
	elseif (d3 <= 1e-107)
		tmp = t_2;
	elseif (d3 <= 4e+43)
		tmp = t_0;
	elseif (d3 <= 1.6e+179)
		tmp = t_2;
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d4 - d1);
	t_1 = d1 * -d3;
	t_2 = d1 * (d2 + d4);
	tmp = 0.0;
	if (d3 <= -1.9e+91)
		tmp = t_1;
	elseif (d3 <= -1.5e-12)
		tmp = t_2;
	elseif (d3 <= -3.35e-164)
		tmp = t_0;
	elseif (d3 <= 1e-107)
		tmp = t_2;
	elseif (d3 <= 4e+43)
		tmp = t_0;
	elseif (d3 <= 1.6e+179)
		tmp = t_2;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * (-d3)), $MachinePrecision]}, Block[{t$95$2 = N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d3, -1.9e+91], t$95$1, If[LessEqual[d3, -1.5e-12], t$95$2, If[LessEqual[d3, -3.35e-164], t$95$0, If[LessEqual[d3, 1e-107], t$95$2, If[LessEqual[d3, 4e+43], t$95$0, If[LessEqual[d3, 1.6e+179], t$95$2, t$95$1]]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := d1 \cdot \left(d4 - d1\right)\\
t_1 := d1 \cdot \left(-d3\right)\\
t_2 := d1 \cdot \left(d2 + d4\right)\\
\mathbf{if}\;d3 \leq -1.9 \cdot 10^{+91}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d3 \leq -1.5 \cdot 10^{-12}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d3 \leq -3.35 \cdot 10^{-164}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq 10^{-107}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d3 \leq 4 \cdot 10^{+43}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq 1.6 \cdot 10^{+179}:\\
\;\;\;\;t_2\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -1.8999999999999999e91 or 1.6000000000000001e179 < d3

    1. Initial program 85.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+85.5%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--89.4%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--92.1%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d3 around inf 83.1%

      \[\leadsto \color{blue}{-1 \cdot \left(d1 \cdot d3\right)} \]
    5. Step-by-step derivation
      1. mul-1-neg83.1%

        \[\leadsto \color{blue}{-d1 \cdot d3} \]
      2. distribute-rgt-neg-out83.1%

        \[\leadsto \color{blue}{d1 \cdot \left(-d3\right)} \]
    6. Simplified83.1%

      \[\leadsto \color{blue}{d1 \cdot \left(-d3\right)} \]

    if -1.8999999999999999e91 < d3 < -1.5000000000000001e-12 or -3.35e-164 < d3 < 1e-107 or 4.00000000000000006e43 < d3 < 1.6000000000000001e179

    1. Initial program 91.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+91.8%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--91.8%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.2%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 84.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d3 around 0 77.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    6. Step-by-step derivation
      1. +-commutative77.5%

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    7. Simplified77.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]

    if -1.5000000000000001e-12 < d3 < -3.35e-164 or 1e-107 < d3 < 4.00000000000000006e43

    1. Initial program 89.6%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+89.6%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--89.6%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.8%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around 0 81.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - \left(d1 + d3\right)\right)} \]
    5. Taylor expanded in d3 around 0 71.7%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification77.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -1.9 \cdot 10^{+91}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d3 \leq -1.5 \cdot 10^{-12}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d3 \leq -3.35 \cdot 10^{-164}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 10^{-107}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d3 \leq 4 \cdot 10^{+43}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.6 \cdot 10^{+179}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \end{array} \]

Alternative 3: 70.6% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d3\right)\\ t_1 := d1 \cdot \left(d2 + d4\right)\\ t_2 := -d1 \cdot \left(d1 + d3\right)\\ \mathbf{if}\;d4 \leq -5 \cdot 10^{-22}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d4 \leq -2.8 \cdot 10^{-205}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d4 \leq 2.9 \cdot 10^{-230}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 1.95 \cdot 10^{-166}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d4 \leq 8.5 \cdot 10^{+58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 2.5 \cdot 10^{+146}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d3)))
        (t_1 (* d1 (+ d2 d4)))
        (t_2 (- (* d1 (+ d1 d3)))))
   (if (<= d4 -5e-22)
     t_1
     (if (<= d4 -2.8e-205)
       t_2
       (if (<= d4 2.9e-230)
         t_0
         (if (<= d4 1.95e-166)
           t_2
           (if (<= d4 8.5e+58)
             t_0
             (if (<= d4 2.5e+146) t_1 (* d1 (- d4 d3))))))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double t_1 = d1 * (d2 + d4);
	double t_2 = -(d1 * (d1 + d3));
	double tmp;
	if (d4 <= -5e-22) {
		tmp = t_1;
	} else if (d4 <= -2.8e-205) {
		tmp = t_2;
	} else if (d4 <= 2.9e-230) {
		tmp = t_0;
	} else if (d4 <= 1.95e-166) {
		tmp = t_2;
	} else if (d4 <= 8.5e+58) {
		tmp = t_0;
	} else if (d4 <= 2.5e+146) {
		tmp = t_1;
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = d1 * (d2 - d3)
    t_1 = d1 * (d2 + d4)
    t_2 = -(d1 * (d1 + d3))
    if (d4 <= (-5d-22)) then
        tmp = t_1
    else if (d4 <= (-2.8d-205)) then
        tmp = t_2
    else if (d4 <= 2.9d-230) then
        tmp = t_0
    else if (d4 <= 1.95d-166) then
        tmp = t_2
    else if (d4 <= 8.5d+58) then
        tmp = t_0
    else if (d4 <= 2.5d+146) then
        tmp = t_1
    else
        tmp = d1 * (d4 - d3)
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double t_1 = d1 * (d2 + d4);
	double t_2 = -(d1 * (d1 + d3));
	double tmp;
	if (d4 <= -5e-22) {
		tmp = t_1;
	} else if (d4 <= -2.8e-205) {
		tmp = t_2;
	} else if (d4 <= 2.9e-230) {
		tmp = t_0;
	} else if (d4 <= 1.95e-166) {
		tmp = t_2;
	} else if (d4 <= 8.5e+58) {
		tmp = t_0;
	} else if (d4 <= 2.5e+146) {
		tmp = t_1;
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d3)
	t_1 = d1 * (d2 + d4)
	t_2 = -(d1 * (d1 + d3))
	tmp = 0
	if d4 <= -5e-22:
		tmp = t_1
	elif d4 <= -2.8e-205:
		tmp = t_2
	elif d4 <= 2.9e-230:
		tmp = t_0
	elif d4 <= 1.95e-166:
		tmp = t_2
	elif d4 <= 8.5e+58:
		tmp = t_0
	elif d4 <= 2.5e+146:
		tmp = t_1
	else:
		tmp = d1 * (d4 - d3)
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d3))
	t_1 = Float64(d1 * Float64(d2 + d4))
	t_2 = Float64(-Float64(d1 * Float64(d1 + d3)))
	tmp = 0.0
	if (d4 <= -5e-22)
		tmp = t_1;
	elseif (d4 <= -2.8e-205)
		tmp = t_2;
	elseif (d4 <= 2.9e-230)
		tmp = t_0;
	elseif (d4 <= 1.95e-166)
		tmp = t_2;
	elseif (d4 <= 8.5e+58)
		tmp = t_0;
	elseif (d4 <= 2.5e+146)
		tmp = t_1;
	else
		tmp = Float64(d1 * Float64(d4 - d3));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d3);
	t_1 = d1 * (d2 + d4);
	t_2 = -(d1 * (d1 + d3));
	tmp = 0.0;
	if (d4 <= -5e-22)
		tmp = t_1;
	elseif (d4 <= -2.8e-205)
		tmp = t_2;
	elseif (d4 <= 2.9e-230)
		tmp = t_0;
	elseif (d4 <= 1.95e-166)
		tmp = t_2;
	elseif (d4 <= 8.5e+58)
		tmp = t_0;
	elseif (d4 <= 2.5e+146)
		tmp = t_1;
	else
		tmp = d1 * (d4 - d3);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = (-N[(d1 * N[(d1 + d3), $MachinePrecision]), $MachinePrecision])}, If[LessEqual[d4, -5e-22], t$95$1, If[LessEqual[d4, -2.8e-205], t$95$2, If[LessEqual[d4, 2.9e-230], t$95$0, If[LessEqual[d4, 1.95e-166], t$95$2, If[LessEqual[d4, 8.5e+58], t$95$0, If[LessEqual[d4, 2.5e+146], t$95$1, N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d3\right)\\
t_1 := d1 \cdot \left(d2 + d4\right)\\
t_2 := -d1 \cdot \left(d1 + d3\right)\\
\mathbf{if}\;d4 \leq -5 \cdot 10^{-22}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d4 \leq -2.8 \cdot 10^{-205}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d4 \leq 2.9 \cdot 10^{-230}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 1.95 \cdot 10^{-166}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d4 \leq 8.5 \cdot 10^{+58}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 2.5 \cdot 10^{+146}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(d4 - d3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d4 < -4.99999999999999954e-22 or 8.50000000000000015e58 < d4 < 2.4999999999999999e146

    1. Initial program 80.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+80.7%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--81.8%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--90.9%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 80.8%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d3 around 0 67.8%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    6. Step-by-step derivation
      1. +-commutative67.8%

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    7. Simplified67.8%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]

    if -4.99999999999999954e-22 < d4 < -2.79999999999999991e-205 or 2.90000000000000005e-230 < d4 < 1.95e-166

    1. Initial program 93.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+93.8%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--95.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--95.9%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around 0 68.9%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - \left(d1 + d3\right)\right)} \]
    5. Taylor expanded in d4 around 0 69.0%

      \[\leadsto \color{blue}{-1 \cdot \left(\left(d1 + d3\right) \cdot d1\right)} \]
    6. Step-by-step derivation
      1. *-commutative69.0%

        \[\leadsto -1 \cdot \color{blue}{\left(d1 \cdot \left(d1 + d3\right)\right)} \]
      2. associate-*r*69.0%

        \[\leadsto \color{blue}{\left(-1 \cdot d1\right) \cdot \left(d1 + d3\right)} \]
      3. neg-mul-169.0%

        \[\leadsto \color{blue}{\left(-d1\right)} \cdot \left(d1 + d3\right) \]
    7. Simplified69.0%

      \[\leadsto \color{blue}{\left(-d1\right) \cdot \left(d1 + d3\right)} \]

    if -2.79999999999999991e-205 < d4 < 2.90000000000000005e-230 or 1.95e-166 < d4 < 8.50000000000000015e58

    1. Initial program 96.4%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+96.4%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--97.6%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--97.6%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out99.9%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 87.4%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d4 around 0 83.1%

      \[\leadsto \color{blue}{\left(d2 - d3\right) \cdot d1} \]

    if 2.4999999999999999e146 < d4

    1. Initial program 88.2%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.2%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.2%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--88.2%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around 0 92.0%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - \left(d1 + d3\right)\right)} \]
    5. Taylor expanded in d1 around 0 92.0%

      \[\leadsto \color{blue}{\left(d4 - d3\right) \cdot d1} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification76.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -5 \cdot 10^{-22}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d4 \leq -2.8 \cdot 10^{-205}:\\ \;\;\;\;-d1 \cdot \left(d1 + d3\right)\\ \mathbf{elif}\;d4 \leq 2.9 \cdot 10^{-230}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 1.95 \cdot 10^{-166}:\\ \;\;\;\;-d1 \cdot \left(d1 + d3\right)\\ \mathbf{elif}\;d4 \leq 8.5 \cdot 10^{+58}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 2.5 \cdot 10^{+146}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \]

Alternative 4: 62.9% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d3\right)\\ \mathbf{if}\;d4 \leq 2.1 \cdot 10^{-203}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 6.9 \cdot 10^{-168}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 1.1 \cdot 10^{+146}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d3))))
   (if (<= d4 2.1e-203)
     t_0
     (if (<= d4 6.9e-168)
       (* d1 (- d1))
       (if (<= d4 8.6e+58)
         t_0
         (if (<= d4 1.1e+146) (* d1 (+ d2 d4)) (* d1 (- d4 d3))))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d4 <= 2.1e-203) {
		tmp = t_0;
	} else if (d4 <= 6.9e-168) {
		tmp = d1 * -d1;
	} else if (d4 <= 8.6e+58) {
		tmp = t_0;
	} else if (d4 <= 1.1e+146) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: tmp
    t_0 = d1 * (d2 - d3)
    if (d4 <= 2.1d-203) then
        tmp = t_0
    else if (d4 <= 6.9d-168) then
        tmp = d1 * -d1
    else if (d4 <= 8.6d+58) then
        tmp = t_0
    else if (d4 <= 1.1d+146) then
        tmp = d1 * (d2 + d4)
    else
        tmp = d1 * (d4 - d3)
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d4 <= 2.1e-203) {
		tmp = t_0;
	} else if (d4 <= 6.9e-168) {
		tmp = d1 * -d1;
	} else if (d4 <= 8.6e+58) {
		tmp = t_0;
	} else if (d4 <= 1.1e+146) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d3)
	tmp = 0
	if d4 <= 2.1e-203:
		tmp = t_0
	elif d4 <= 6.9e-168:
		tmp = d1 * -d1
	elif d4 <= 8.6e+58:
		tmp = t_0
	elif d4 <= 1.1e+146:
		tmp = d1 * (d2 + d4)
	else:
		tmp = d1 * (d4 - d3)
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d3))
	tmp = 0.0
	if (d4 <= 2.1e-203)
		tmp = t_0;
	elseif (d4 <= 6.9e-168)
		tmp = Float64(d1 * Float64(-d1));
	elseif (d4 <= 8.6e+58)
		tmp = t_0;
	elseif (d4 <= 1.1e+146)
		tmp = Float64(d1 * Float64(d2 + d4));
	else
		tmp = Float64(d1 * Float64(d4 - d3));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d3);
	tmp = 0.0;
	if (d4 <= 2.1e-203)
		tmp = t_0;
	elseif (d4 <= 6.9e-168)
		tmp = d1 * -d1;
	elseif (d4 <= 8.6e+58)
		tmp = t_0;
	elseif (d4 <= 1.1e+146)
		tmp = d1 * (d2 + d4);
	else
		tmp = d1 * (d4 - d3);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d4, 2.1e-203], t$95$0, If[LessEqual[d4, 6.9e-168], N[(d1 * (-d1)), $MachinePrecision], If[LessEqual[d4, 8.6e+58], t$95$0, If[LessEqual[d4, 1.1e+146], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d3\right)\\
\mathbf{if}\;d4 \leq 2.1 \cdot 10^{-203}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 6.9 \cdot 10^{-168}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

\mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+58}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 1.1 \cdot 10^{+146}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(d4 - d3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d4 < 2.10000000000000002e-203 or 6.9e-168 < d4 < 8.59999999999999982e58

    1. Initial program 89.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+89.8%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--91.3%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.9%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 82.8%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d4 around 0 66.4%

      \[\leadsto \color{blue}{\left(d2 - d3\right) \cdot d1} \]

    if 2.10000000000000002e-203 < d4 < 6.9e-168

    1. Initial program 87.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+87.5%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--87.5%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--87.5%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around inf 50.8%

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
    5. Step-by-step derivation
      1. unpow250.8%

        \[\leadsto -1 \cdot \color{blue}{\left(d1 \cdot d1\right)} \]
      2. neg-mul-150.8%

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
    6. Simplified50.8%

      \[\leadsto \color{blue}{-d1 \cdot d1} \]

    if 8.59999999999999982e58 < d4 < 1.0999999999999999e146

    1. Initial program 88.9%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.9%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.4%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 83.4%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d3 around 0 72.7%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    6. Step-by-step derivation
      1. +-commutative72.7%

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    7. Simplified72.7%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]

    if 1.0999999999999999e146 < d4

    1. Initial program 88.2%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.2%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.2%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--88.2%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around 0 92.0%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - \left(d1 + d3\right)\right)} \]
    5. Taylor expanded in d1 around 0 92.0%

      \[\leadsto \color{blue}{\left(d4 - d3\right) \cdot d1} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification69.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 2.1 \cdot 10^{-203}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 6.9 \cdot 10^{-168}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 8.6 \cdot 10^{+58}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 1.1 \cdot 10^{+146}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \]

Alternative 5: 81.4% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d2 \leq -6.4 \cdot 10^{+142}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d2 \leq -8.5 \cdot 10^{+80} \lor \neg \left(d2 \leq -3.8 \cdot 10^{+22}\right):\\ \;\;\;\;d1 \cdot \left(d4 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d2 -6.4e+142)
   (* d1 (+ d2 d4))
   (if (or (<= d2 -8.5e+80) (not (<= d2 -3.8e+22)))
     (* d1 (- d4 (+ d1 d3)))
     (* d1 (- d2 d3)))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -6.4e+142) {
		tmp = d1 * (d2 + d4);
	} else if ((d2 <= -8.5e+80) || !(d2 <= -3.8e+22)) {
		tmp = d1 * (d4 - (d1 + d3));
	} else {
		tmp = d1 * (d2 - d3);
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if (d2 <= (-6.4d+142)) then
        tmp = d1 * (d2 + d4)
    else if ((d2 <= (-8.5d+80)) .or. (.not. (d2 <= (-3.8d+22)))) then
        tmp = d1 * (d4 - (d1 + d3))
    else
        tmp = d1 * (d2 - d3)
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -6.4e+142) {
		tmp = d1 * (d2 + d4);
	} else if ((d2 <= -8.5e+80) || !(d2 <= -3.8e+22)) {
		tmp = d1 * (d4 - (d1 + d3));
	} else {
		tmp = d1 * (d2 - d3);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -6.4e+142:
		tmp = d1 * (d2 + d4)
	elif (d2 <= -8.5e+80) or not (d2 <= -3.8e+22):
		tmp = d1 * (d4 - (d1 + d3))
	else:
		tmp = d1 * (d2 - d3)
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -6.4e+142)
		tmp = Float64(d1 * Float64(d2 + d4));
	elseif ((d2 <= -8.5e+80) || !(d2 <= -3.8e+22))
		tmp = Float64(d1 * Float64(d4 - Float64(d1 + d3)));
	else
		tmp = Float64(d1 * Float64(d2 - d3));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d2 <= -6.4e+142)
		tmp = d1 * (d2 + d4);
	elseif ((d2 <= -8.5e+80) || ~((d2 <= -3.8e+22)))
		tmp = d1 * (d4 - (d1 + d3));
	else
		tmp = d1 * (d2 - d3);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -6.4e+142], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[d2, -8.5e+80], N[Not[LessEqual[d2, -3.8e+22]], $MachinePrecision]], N[(d1 * N[(d4 - N[(d1 + d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -6.4 \cdot 10^{+142}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\

\mathbf{elif}\;d2 \leq -8.5 \cdot 10^{+80} \lor \neg \left(d2 \leq -3.8 \cdot 10^{+22}\right):\\
\;\;\;\;d1 \cdot \left(d4 - \left(d1 + d3\right)\right)\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d2 < -6.40000000000000011e142

    1. Initial program 77.3%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+77.3%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--80.5%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--80.5%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out99.9%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 93.4%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d3 around 0 92.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    6. Step-by-step derivation
      1. +-commutative92.5%

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    7. Simplified92.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]

    if -6.40000000000000011e142 < d2 < -8.50000000000000007e80 or -3.8000000000000004e22 < d2

    1. Initial program 91.1%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+91.1%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--92.0%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--95.8%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around 0 83.2%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - \left(d1 + d3\right)\right)} \]

    if -8.50000000000000007e80 < d2 < -3.8000000000000004e22

    1. Initial program 91.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+91.7%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--91.5%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--91.5%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out99.9%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified99.9%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 91.5%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d4 around 0 59.6%

      \[\leadsto \color{blue}{\left(d2 - d3\right) \cdot d1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification83.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -6.4 \cdot 10^{+142}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d2 \leq -8.5 \cdot 10^{+80} \lor \neg \left(d2 \leq -3.8 \cdot 10^{+22}\right):\\ \;\;\;\;d1 \cdot \left(d4 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \end{array} \]

Alternative 6: 63.2% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d3\right)\\ \mathbf{if}\;d4 \leq 2.1 \cdot 10^{-203}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 8 \cdot 10^{-168}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 7.8 \cdot 10^{+58}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d3))))
   (if (<= d4 2.1e-203)
     t_0
     (if (<= d4 8e-168)
       (* d1 (- d1))
       (if (<= d4 7.8e+58) t_0 (* d1 (+ d2 d4)))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d4 <= 2.1e-203) {
		tmp = t_0;
	} else if (d4 <= 8e-168) {
		tmp = d1 * -d1;
	} else if (d4 <= 7.8e+58) {
		tmp = t_0;
	} else {
		tmp = d1 * (d2 + d4);
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: tmp
    t_0 = d1 * (d2 - d3)
    if (d4 <= 2.1d-203) then
        tmp = t_0
    else if (d4 <= 8d-168) then
        tmp = d1 * -d1
    else if (d4 <= 7.8d+58) then
        tmp = t_0
    else
        tmp = d1 * (d2 + d4)
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d4 <= 2.1e-203) {
		tmp = t_0;
	} else if (d4 <= 8e-168) {
		tmp = d1 * -d1;
	} else if (d4 <= 7.8e+58) {
		tmp = t_0;
	} else {
		tmp = d1 * (d2 + d4);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d3)
	tmp = 0
	if d4 <= 2.1e-203:
		tmp = t_0
	elif d4 <= 8e-168:
		tmp = d1 * -d1
	elif d4 <= 7.8e+58:
		tmp = t_0
	else:
		tmp = d1 * (d2 + d4)
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d3))
	tmp = 0.0
	if (d4 <= 2.1e-203)
		tmp = t_0;
	elseif (d4 <= 8e-168)
		tmp = Float64(d1 * Float64(-d1));
	elseif (d4 <= 7.8e+58)
		tmp = t_0;
	else
		tmp = Float64(d1 * Float64(d2 + d4));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d3);
	tmp = 0.0;
	if (d4 <= 2.1e-203)
		tmp = t_0;
	elseif (d4 <= 8e-168)
		tmp = d1 * -d1;
	elseif (d4 <= 7.8e+58)
		tmp = t_0;
	else
		tmp = d1 * (d2 + d4);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d4, 2.1e-203], t$95$0, If[LessEqual[d4, 8e-168], N[(d1 * (-d1)), $MachinePrecision], If[LessEqual[d4, 7.8e+58], t$95$0, N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d3\right)\\
\mathbf{if}\;d4 \leq 2.1 \cdot 10^{-203}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 8 \cdot 10^{-168}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

\mathbf{elif}\;d4 \leq 7.8 \cdot 10^{+58}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < 2.10000000000000002e-203 or 8.0000000000000004e-168 < d4 < 7.8000000000000002e58

    1. Initial program 89.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+89.8%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--91.3%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.9%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 82.8%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d4 around 0 66.4%

      \[\leadsto \color{blue}{\left(d2 - d3\right) \cdot d1} \]

    if 2.10000000000000002e-203 < d4 < 8.0000000000000004e-168

    1. Initial program 87.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+87.5%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--87.5%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--87.5%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around inf 50.8%

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
    5. Step-by-step derivation
      1. unpow250.8%

        \[\leadsto -1 \cdot \color{blue}{\left(d1 \cdot d1\right)} \]
      2. neg-mul-150.8%

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
    6. Simplified50.8%

      \[\leadsto \color{blue}{-d1 \cdot d1} \]

    if 7.8000000000000002e58 < d4

    1. Initial program 88.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.5%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.5%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--90.4%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 94.2%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d3 around 0 86.7%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    6. Step-by-step derivation
      1. +-commutative86.7%

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    7. Simplified86.7%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 2.1 \cdot 10^{-203}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 8 \cdot 10^{-168}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 7.8 \cdot 10^{+58}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \end{array} \]

Alternative 7: 91.4% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d1 \leq -1.5 \cdot 10^{+89} \lor \neg \left(d1 \leq 7.5 \cdot 10^{+20}\right):\\ \;\;\;\;d1 \cdot \left(d4 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (or (<= d1 -1.5e+89) (not (<= d1 7.5e+20)))
   (* d1 (- d4 (+ d1 d3)))
   (* d1 (- (+ d2 d4) d3))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d1 <= -1.5e+89) || !(d1 <= 7.5e+20)) {
		tmp = d1 * (d4 - (d1 + d3));
	} else {
		tmp = d1 * ((d2 + d4) - d3);
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if ((d1 <= (-1.5d+89)) .or. (.not. (d1 <= 7.5d+20))) then
        tmp = d1 * (d4 - (d1 + d3))
    else
        tmp = d1 * ((d2 + d4) - d3)
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d1 <= -1.5e+89) || !(d1 <= 7.5e+20)) {
		tmp = d1 * (d4 - (d1 + d3));
	} else {
		tmp = d1 * ((d2 + d4) - d3);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if (d1 <= -1.5e+89) or not (d1 <= 7.5e+20):
		tmp = d1 * (d4 - (d1 + d3))
	else:
		tmp = d1 * ((d2 + d4) - d3)
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if ((d1 <= -1.5e+89) || !(d1 <= 7.5e+20))
		tmp = Float64(d1 * Float64(d4 - Float64(d1 + d3)));
	else
		tmp = Float64(d1 * Float64(Float64(d2 + d4) - d3));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if ((d1 <= -1.5e+89) || ~((d1 <= 7.5e+20)))
		tmp = d1 * (d4 - (d1 + d3));
	else
		tmp = d1 * ((d2 + d4) - d3);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d1, -1.5e+89], N[Not[LessEqual[d1, 7.5e+20]], $MachinePrecision]], N[(d1 * N[(d4 - N[(d1 + d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d2 + d4), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d1 \leq -1.5 \cdot 10^{+89} \lor \neg \left(d1 \leq 7.5 \cdot 10^{+20}\right):\\
\;\;\;\;d1 \cdot \left(d4 - \left(d1 + d3\right)\right)\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d1 < -1.50000000000000006e89 or 7.5e20 < d1

    1. Initial program 74.0%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+74.0%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--76.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--84.6%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around 0 86.9%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - \left(d1 + d3\right)\right)} \]

    if -1.50000000000000006e89 < d1 < 7.5e20

    1. Initial program 100.0%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+100.0%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--99.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--99.9%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 97.4%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification93.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d1 \leq -1.5 \cdot 10^{+89} \lor \neg \left(d1 \leq 7.5 \cdot 10^{+20}\right):\\ \;\;\;\;d1 \cdot \left(d4 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \end{array} \]

Alternative 8: 39.2% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d4 \leq 2.9 \cdot 10^{-230}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 6.1 \cdot 10^{-167}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 1.3 \cdot 10^{+59}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d4 2.9e-230)
   (* d1 d2)
   (if (<= d4 6.1e-167)
     (* d1 (- d1))
     (if (<= d4 1.3e+59) (* d1 (- d3)) (* d1 d4)))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 2.9e-230) {
		tmp = d1 * d2;
	} else if (d4 <= 6.1e-167) {
		tmp = d1 * -d1;
	} else if (d4 <= 1.3e+59) {
		tmp = d1 * -d3;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if (d4 <= 2.9d-230) then
        tmp = d1 * d2
    else if (d4 <= 6.1d-167) then
        tmp = d1 * -d1
    else if (d4 <= 1.3d+59) then
        tmp = d1 * -d3
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 2.9e-230) {
		tmp = d1 * d2;
	} else if (d4 <= 6.1e-167) {
		tmp = d1 * -d1;
	} else if (d4 <= 1.3e+59) {
		tmp = d1 * -d3;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 2.9e-230:
		tmp = d1 * d2
	elif d4 <= 6.1e-167:
		tmp = d1 * -d1
	elif d4 <= 1.3e+59:
		tmp = d1 * -d3
	else:
		tmp = d1 * d4
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 2.9e-230)
		tmp = Float64(d1 * d2);
	elseif (d4 <= 6.1e-167)
		tmp = Float64(d1 * Float64(-d1));
	elseif (d4 <= 1.3e+59)
		tmp = Float64(d1 * Float64(-d3));
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= 2.9e-230)
		tmp = d1 * d2;
	elseif (d4 <= 6.1e-167)
		tmp = d1 * -d1;
	elseif (d4 <= 1.3e+59)
		tmp = d1 * -d3;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 2.9e-230], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, 6.1e-167], N[(d1 * (-d1)), $MachinePrecision], If[LessEqual[d4, 1.3e+59], N[(d1 * (-d3)), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 2.9 \cdot 10^{-230}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq 6.1 \cdot 10^{-167}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

\mathbf{elif}\;d4 \leq 1.3 \cdot 10^{+59}:\\
\;\;\;\;d1 \cdot \left(-d3\right)\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot d4\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d4 < 2.90000000000000005e-230

    1. Initial program 87.4%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+87.4%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.8%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--93.7%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around inf 30.3%

      \[\leadsto \color{blue}{d2 \cdot d1} \]

    if 2.90000000000000005e-230 < d4 < 6.0999999999999998e-167

    1. Initial program 86.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+86.7%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--93.3%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--93.3%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around inf 41.1%

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
    5. Step-by-step derivation
      1. unpow241.1%

        \[\leadsto -1 \cdot \color{blue}{\left(d1 \cdot d1\right)} \]
      2. neg-mul-141.1%

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
    6. Simplified41.1%

      \[\leadsto \color{blue}{-d1 \cdot d1} \]

    if 6.0999999999999998e-167 < d4 < 1.3e59

    1. Initial program 97.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+97.8%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--97.8%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--97.8%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d3 around inf 51.9%

      \[\leadsto \color{blue}{-1 \cdot \left(d1 \cdot d3\right)} \]
    5. Step-by-step derivation
      1. mul-1-neg51.9%

        \[\leadsto \color{blue}{-d1 \cdot d3} \]
      2. distribute-rgt-neg-out51.9%

        \[\leadsto \color{blue}{d1 \cdot \left(-d3\right)} \]
    6. Simplified51.9%

      \[\leadsto \color{blue}{d1 \cdot \left(-d3\right)} \]

    if 1.3e59 < d4

    1. Initial program 88.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.5%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.5%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--90.4%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d4 around inf 70.3%

      \[\leadsto \color{blue}{d1 \cdot d4} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification42.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 2.9 \cdot 10^{-230}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 6.1 \cdot 10^{-167}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 1.3 \cdot 10^{+59}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 9: 39.7% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d4 \leq 4.4 \cdot 10^{-232}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 1.46 \cdot 10^{-165}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 1.22 \cdot 10^{+51}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d4 4.4e-232)
   (* d1 d2)
   (if (<= d4 1.46e-165)
     (* d1 (- d1))
     (if (<= d4 1.22e+51) (* d1 d2) (* d1 d4)))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 4.4e-232) {
		tmp = d1 * d2;
	} else if (d4 <= 1.46e-165) {
		tmp = d1 * -d1;
	} else if (d4 <= 1.22e+51) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if (d4 <= 4.4d-232) then
        tmp = d1 * d2
    else if (d4 <= 1.46d-165) then
        tmp = d1 * -d1
    else if (d4 <= 1.22d+51) then
        tmp = d1 * d2
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 4.4e-232) {
		tmp = d1 * d2;
	} else if (d4 <= 1.46e-165) {
		tmp = d1 * -d1;
	} else if (d4 <= 1.22e+51) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 4.4e-232:
		tmp = d1 * d2
	elif d4 <= 1.46e-165:
		tmp = d1 * -d1
	elif d4 <= 1.22e+51:
		tmp = d1 * d2
	else:
		tmp = d1 * d4
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 4.4e-232)
		tmp = Float64(d1 * d2);
	elseif (d4 <= 1.46e-165)
		tmp = Float64(d1 * Float64(-d1));
	elseif (d4 <= 1.22e+51)
		tmp = Float64(d1 * d2);
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= 4.4e-232)
		tmp = d1 * d2;
	elseif (d4 <= 1.46e-165)
		tmp = d1 * -d1;
	elseif (d4 <= 1.22e+51)
		tmp = d1 * d2;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 4.4e-232], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, 1.46e-165], N[(d1 * (-d1)), $MachinePrecision], If[LessEqual[d4, 1.22e+51], N[(d1 * d2), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 4.4 \cdot 10^{-232}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq 1.46 \cdot 10^{-165}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

\mathbf{elif}\;d4 \leq 1.22 \cdot 10^{+51}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot d4\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < 4.40000000000000004e-232 or 1.4600000000000001e-165 < d4 < 1.22000000000000005e51

    1. Initial program 89.9%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+89.9%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--90.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.6%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around inf 32.6%

      \[\leadsto \color{blue}{d2 \cdot d1} \]

    if 4.40000000000000004e-232 < d4 < 1.4600000000000001e-165

    1. Initial program 86.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+86.7%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--93.3%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--93.3%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around inf 41.1%

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2}} \]
    5. Step-by-step derivation
      1. unpow241.1%

        \[\leadsto -1 \cdot \color{blue}{\left(d1 \cdot d1\right)} \]
      2. neg-mul-141.1%

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
    6. Simplified41.1%

      \[\leadsto \color{blue}{-d1 \cdot d1} \]

    if 1.22000000000000005e51 < d4

    1. Initial program 88.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.7%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.7%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--90.6%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d4 around inf 69.0%

      \[\leadsto \color{blue}{d1 \cdot d4} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification40.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 4.4 \cdot 10^{-232}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 1.46 \cdot 10^{-165}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 1.22 \cdot 10^{+51}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 10: 67.9% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d3 \leq -2.35 \cdot 10^{+92} \lor \neg \left(d3 \leq 8 \cdot 10^{+178}\right):\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (or (<= d3 -2.35e+92) (not (<= d3 8e+178)))
   (* d1 (- d3))
   (* d1 (+ d2 d4))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -2.35e+92) || !(d3 <= 8e+178)) {
		tmp = d1 * -d3;
	} else {
		tmp = d1 * (d2 + d4);
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if ((d3 <= (-2.35d+92)) .or. (.not. (d3 <= 8d+178))) then
        tmp = d1 * -d3
    else
        tmp = d1 * (d2 + d4)
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -2.35e+92) || !(d3 <= 8e+178)) {
		tmp = d1 * -d3;
	} else {
		tmp = d1 * (d2 + d4);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if (d3 <= -2.35e+92) or not (d3 <= 8e+178):
		tmp = d1 * -d3
	else:
		tmp = d1 * (d2 + d4)
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if ((d3 <= -2.35e+92) || !(d3 <= 8e+178))
		tmp = Float64(d1 * Float64(-d3));
	else
		tmp = Float64(d1 * Float64(d2 + d4));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if ((d3 <= -2.35e+92) || ~((d3 <= 8e+178)))
		tmp = d1 * -d3;
	else
		tmp = d1 * (d2 + d4);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d3, -2.35e+92], N[Not[LessEqual[d3, 8e+178]], $MachinePrecision]], N[(d1 * (-d3)), $MachinePrecision], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -2.35 \cdot 10^{+92} \lor \neg \left(d3 \leq 8 \cdot 10^{+178}\right):\\
\;\;\;\;d1 \cdot \left(-d3\right)\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d3 < -2.35e92 or 8.0000000000000004e178 < d3

    1. Initial program 85.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+85.5%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--89.4%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--92.1%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d3 around inf 83.1%

      \[\leadsto \color{blue}{-1 \cdot \left(d1 \cdot d3\right)} \]
    5. Step-by-step derivation
      1. mul-1-neg83.1%

        \[\leadsto \color{blue}{-d1 \cdot d3} \]
      2. distribute-rgt-neg-out83.1%

        \[\leadsto \color{blue}{d1 \cdot \left(-d3\right)} \]
    6. Simplified83.1%

      \[\leadsto \color{blue}{d1 \cdot \left(-d3\right)} \]

    if -2.35e92 < d3 < 8.0000000000000004e178

    1. Initial program 91.1%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+91.1%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--91.1%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.4%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d1 around 0 79.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d3\right)} \]
    5. Taylor expanded in d3 around 0 71.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    6. Step-by-step derivation
      1. +-commutative71.5%

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
    7. Simplified71.5%

      \[\leadsto \color{blue}{d1 \cdot \left(d4 + d2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification74.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -2.35 \cdot 10^{+92} \lor \neg \left(d3 \leq 8 \cdot 10^{+178}\right):\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \end{array} \]

Alternative 11: 39.6% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d4 \leq 2.8 \cdot 10^{+52}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d4 2.8e+52) (* d1 d2) (* d1 d4)))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 2.8e+52) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: tmp
    if (d4 <= 2.8d+52) then
        tmp = d1 * d2
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 2.8e+52) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 2.8e+52:
		tmp = d1 * d2
	else:
		tmp = d1 * d4
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 2.8e+52)
		tmp = Float64(d1 * d2);
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= 2.8e+52)
		tmp = d1 * d2;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 2.8e+52], N[(d1 * d2), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 2.8 \cdot 10^{+52}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{else}:\\
\;\;\;\;d1 \cdot d4\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d4 < 2.8e52

    1. Initial program 89.6%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+89.6%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--91.1%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.5%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d2 around inf 33.7%

      \[\leadsto \color{blue}{d2 \cdot d1} \]

    if 2.8e52 < d4

    1. Initial program 88.7%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Step-by-step derivation
      1. associate--l+88.7%

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--88.7%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--90.6%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
      4. distribute-lft-out100.0%

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
    4. Taylor expanded in d4 around inf 69.0%

      \[\leadsto \color{blue}{d1 \cdot d4} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification41.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 2.8 \cdot 10^{+52}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 12: 30.4% accurate, 5.0× speedup?

\[\begin{array}{l} \\ d1 \cdot d4 \end{array} \]
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 d4))
double code(double d1, double d2, double d3, double d4) {
	return d1 * d4;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = d1 * d4
end function
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * d4;
}
def code(d1, d2, d3, d4):
	return d1 * d4
function code(d1, d2, d3, d4)
	return Float64(d1 * d4)
end
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * d4;
end
code[d1_, d2_, d3_, d4_] := N[(d1 * d4), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot d4
\end{array}
Derivation
  1. Initial program 89.4%

    \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
  2. Step-by-step derivation
    1. associate--l+89.4%

      \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
    2. distribute-lft-out--90.6%

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
    3. distribute-rgt-out--93.7%

      \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
    4. distribute-lft-out100.0%

      \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
  4. Taylor expanded in d4 around inf 32.1%

    \[\leadsto \color{blue}{d1 \cdot d4} \]
  5. Final simplification32.1%

    \[\leadsto d1 \cdot d4 \]

Developer target: 100.0% accurate, 1.7× speedup?

\[\begin{array}{l} \\ d1 \cdot \left(\left(\left(d2 - d3\right) + d4\right) - d1\right) \end{array} \]
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 (- (+ (- d2 d3) d4) d1)))
double code(double d1, double d2, double d3, double d4) {
	return d1 * (((d2 - d3) + d4) - d1);
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    code = d1 * (((d2 - d3) + d4) - d1)
end function
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * (((d2 - d3) + d4) - d1);
}
def code(d1, d2, d3, d4):
	return d1 * (((d2 - d3) + d4) - d1)
function code(d1, d2, d3, d4)
	return Float64(d1 * Float64(Float64(Float64(d2 - d3) + d4) - d1))
end
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * (((d2 - d3) + d4) - d1);
end
code[d1_, d2_, d3_, d4_] := N[(d1 * N[(N[(N[(d2 - d3), $MachinePrecision] + d4), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot \left(\left(\left(d2 - d3\right) + d4\right) - d1\right)
\end{array}

Reproduce

?
herbie shell --seed 2023188 
(FPCore (d1 d2 d3 d4)
  :name "FastMath dist4"
  :precision binary64

  :herbie-target
  (* d1 (- (+ (- d2 d3) d4) d1))

  (- (+ (- (* d1 d2) (* d1 d3)) (* d4 d1)) (* d1 d1)))