FastMath dist4

Percentage Accurate: 87.5% → 100.0%
Time: 6.5s
Alternatives: 13
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 13 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.5% 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} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right) \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 (+ (- d2 d3) (- d4 d1))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	return d1 * ((d2 - d3) + (d4 - d1));
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * ((d2 - d3) + (d4 - d1));
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	return d1 * ((d2 - d3) + (d4 - d1))
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	return Float64(d1 * Float64(Float64(d2 - d3) + Float64(d4 - d1)))
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * ((d2 - d3) + (d4 - d1));
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := N[(d1 * N[(N[(d2 - d3), $MachinePrecision] + N[(d4 - d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)
\end{array}
Derivation
  1. Initial program 89.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+89.0%

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
    3. distribute-rgt-out--94.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. Final simplification100.0%

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

Alternative 2: 51.0% accurate, 0.8× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(-d3\right)\\ t_1 := d1 \cdot \left(-d1\right)\\ \mathbf{if}\;d4 \leq -280000000:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -1.08 \cdot 10^{-88}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d4 \leq -2.1 \cdot 10^{-201}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -1.05 \cdot 10^{-290}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 2.05 \cdot 10^{-119}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d4 \leq 2.2 \cdot 10^{+89}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 2.8 \cdot 10^{+118}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d4 \leq 5.1 \cdot 10^{+144}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d3))) (t_1 (* d1 (- d1))))
   (if (<= d4 -280000000.0)
     (* d1 d2)
     (if (<= d4 -1.08e-88)
       t_1
       (if (<= d4 -2.1e-201)
         (* d1 d2)
         (if (<= d4 -1.05e-290)
           t_0
           (if (<= d4 2.05e-119)
             t_1
             (if (<= d4 2.2e+89)
               t_0
               (if (<= d4 2.8e+118)
                 (* d1 d4)
                 (if (<= d4 5.1e+144) (* d1 d2) (* d1 d4)))))))))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double t_1 = d1 * -d1;
	double tmp;
	if (d4 <= -280000000.0) {
		tmp = d1 * d2;
	} else if (d4 <= -1.08e-88) {
		tmp = t_1;
	} else if (d4 <= -2.1e-201) {
		tmp = d1 * d2;
	} else if (d4 <= -1.05e-290) {
		tmp = t_0;
	} else if (d4 <= 2.05e-119) {
		tmp = t_1;
	} else if (d4 <= 2.2e+89) {
		tmp = t_0;
	} else if (d4 <= 2.8e+118) {
		tmp = d1 * d4;
	} else if (d4 <= 5.1e+144) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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) :: tmp
    t_0 = d1 * -d3
    t_1 = d1 * -d1
    if (d4 <= (-280000000.0d0)) then
        tmp = d1 * d2
    else if (d4 <= (-1.08d-88)) then
        tmp = t_1
    else if (d4 <= (-2.1d-201)) then
        tmp = d1 * d2
    else if (d4 <= (-1.05d-290)) then
        tmp = t_0
    else if (d4 <= 2.05d-119) then
        tmp = t_1
    else if (d4 <= 2.2d+89) then
        tmp = t_0
    else if (d4 <= 2.8d+118) then
        tmp = d1 * d4
    else if (d4 <= 5.1d+144) then
        tmp = d1 * d2
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double t_1 = d1 * -d1;
	double tmp;
	if (d4 <= -280000000.0) {
		tmp = d1 * d2;
	} else if (d4 <= -1.08e-88) {
		tmp = t_1;
	} else if (d4 <= -2.1e-201) {
		tmp = d1 * d2;
	} else if (d4 <= -1.05e-290) {
		tmp = t_0;
	} else if (d4 <= 2.05e-119) {
		tmp = t_1;
	} else if (d4 <= 2.2e+89) {
		tmp = t_0;
	} else if (d4 <= 2.8e+118) {
		tmp = d1 * d4;
	} else if (d4 <= 5.1e+144) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * -d3
	t_1 = d1 * -d1
	tmp = 0
	if d4 <= -280000000.0:
		tmp = d1 * d2
	elif d4 <= -1.08e-88:
		tmp = t_1
	elif d4 <= -2.1e-201:
		tmp = d1 * d2
	elif d4 <= -1.05e-290:
		tmp = t_0
	elif d4 <= 2.05e-119:
		tmp = t_1
	elif d4 <= 2.2e+89:
		tmp = t_0
	elif d4 <= 2.8e+118:
		tmp = d1 * d4
	elif d4 <= 5.1e+144:
		tmp = d1 * d2
	else:
		tmp = d1 * d4
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(-d3))
	t_1 = Float64(d1 * Float64(-d1))
	tmp = 0.0
	if (d4 <= -280000000.0)
		tmp = Float64(d1 * d2);
	elseif (d4 <= -1.08e-88)
		tmp = t_1;
	elseif (d4 <= -2.1e-201)
		tmp = Float64(d1 * d2);
	elseif (d4 <= -1.05e-290)
		tmp = t_0;
	elseif (d4 <= 2.05e-119)
		tmp = t_1;
	elseif (d4 <= 2.2e+89)
		tmp = t_0;
	elseif (d4 <= 2.8e+118)
		tmp = Float64(d1 * d4);
	elseif (d4 <= 5.1e+144)
		tmp = Float64(d1 * d2);
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * -d3;
	t_1 = d1 * -d1;
	tmp = 0.0;
	if (d4 <= -280000000.0)
		tmp = d1 * d2;
	elseif (d4 <= -1.08e-88)
		tmp = t_1;
	elseif (d4 <= -2.1e-201)
		tmp = d1 * d2;
	elseif (d4 <= -1.05e-290)
		tmp = t_0;
	elseif (d4 <= 2.05e-119)
		tmp = t_1;
	elseif (d4 <= 2.2e+89)
		tmp = t_0;
	elseif (d4 <= 2.8e+118)
		tmp = d1 * d4;
	elseif (d4 <= 5.1e+144)
		tmp = d1 * d2;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * (-d3)), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * (-d1)), $MachinePrecision]}, If[LessEqual[d4, -280000000.0], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, -1.08e-88], t$95$1, If[LessEqual[d4, -2.1e-201], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, -1.05e-290], t$95$0, If[LessEqual[d4, 2.05e-119], t$95$1, If[LessEqual[d4, 2.2e+89], t$95$0, If[LessEqual[d4, 2.8e+118], N[(d1 * d4), $MachinePrecision], If[LessEqual[d4, 5.1e+144], N[(d1 * d2), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]]]]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(-d3\right)\\
t_1 := d1 \cdot \left(-d1\right)\\
\mathbf{if}\;d4 \leq -280000000:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq -1.08 \cdot 10^{-88}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d4 \leq -2.1 \cdot 10^{-201}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq -1.05 \cdot 10^{-290}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 2.05 \cdot 10^{-119}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d4 \leq 2.2 \cdot 10^{+89}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 2.8 \cdot 10^{+118}:\\
\;\;\;\;d1 \cdot d4\\

\mathbf{elif}\;d4 \leq 5.1 \cdot 10^{+144}:\\
\;\;\;\;d1 \cdot d2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d4 < -2.8e8 or -1.07999999999999995e-88 < d4 < -2.10000000000000012e-201 or 2.79999999999999986e118 < d4 < 5.0999999999999999e144

    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--96.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 d2 around inf 25.6%

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

    if -2.8e8 < d4 < -1.07999999999999995e-88 or -1.0500000000000001e-290 < d4 < 2.0500000000000001e-119

    1. Initial program 95.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+95.0%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--98.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 49.9%

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

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

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
      3. distribute-rgt-neg-out49.9%

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

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

    if -2.10000000000000012e-201 < d4 < -1.0500000000000001e-290 or 2.0500000000000001e-119 < d4 < 2.2e89

    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--93.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--93.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 d3 around inf 44.3%

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

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

        \[\leadsto \color{blue}{\left(-d1\right)} \cdot d3 \]
      3. *-commutative44.3%

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

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

    if 2.2e89 < d4 < 2.79999999999999986e118 or 5.0999999999999999e144 < d4

    1. Initial program 77.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+77.7%

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

        \[\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-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 74.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -280000000:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -1.08 \cdot 10^{-88}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq -2.1 \cdot 10^{-201}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -1.05 \cdot 10^{-290}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d4 \leq 2.05 \cdot 10^{-119}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 2.2 \cdot 10^{+89}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d4 \leq 2.8 \cdot 10^{+118}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d4 \leq 5.1 \cdot 10^{+144}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 3: 72.2% accurate, 0.8× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d1\right)\\ t_1 := d1 \cdot \left(d2 - d3\right)\\ \mathbf{if}\;d4 \leq -1.9 \cdot 10^{-108}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq -1.3 \cdot 10^{-297}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d4 \leq 2.35 \cdot 10^{-122}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 9.7 \cdot 10^{-29}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d4 \leq 6 \cdot 10^{-20} \lor \neg \left(d4 \leq 8 \cdot 10^{+121}\right) \land d4 \leq 3.9 \cdot 10^{+139}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d1))) (t_1 (* d1 (- d2 d3))))
   (if (<= d4 -1.9e-108)
     t_0
     (if (<= d4 -1.3e-297)
       t_1
       (if (<= d4 2.35e-122)
         t_0
         (if (<= d4 9.7e-29)
           t_1
           (if (or (<= d4 6e-20) (and (not (<= d4 8e+121)) (<= d4 3.9e+139)))
             t_0
             (* d1 (- d4 d3)))))))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d1);
	double t_1 = d1 * (d2 - d3);
	double tmp;
	if (d4 <= -1.9e-108) {
		tmp = t_0;
	} else if (d4 <= -1.3e-297) {
		tmp = t_1;
	} else if (d4 <= 2.35e-122) {
		tmp = t_0;
	} else if (d4 <= 9.7e-29) {
		tmp = t_1;
	} else if ((d4 <= 6e-20) || (!(d4 <= 8e+121) && (d4 <= 3.9e+139))) {
		tmp = t_0;
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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) :: tmp
    t_0 = d1 * (d2 - d1)
    t_1 = d1 * (d2 - d3)
    if (d4 <= (-1.9d-108)) then
        tmp = t_0
    else if (d4 <= (-1.3d-297)) then
        tmp = t_1
    else if (d4 <= 2.35d-122) then
        tmp = t_0
    else if (d4 <= 9.7d-29) then
        tmp = t_1
    else if ((d4 <= 6d-20) .or. (.not. (d4 <= 8d+121)) .and. (d4 <= 3.9d+139)) then
        tmp = t_0
    else
        tmp = d1 * (d4 - d3)
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d1);
	double t_1 = d1 * (d2 - d3);
	double tmp;
	if (d4 <= -1.9e-108) {
		tmp = t_0;
	} else if (d4 <= -1.3e-297) {
		tmp = t_1;
	} else if (d4 <= 2.35e-122) {
		tmp = t_0;
	} else if (d4 <= 9.7e-29) {
		tmp = t_1;
	} else if ((d4 <= 6e-20) || (!(d4 <= 8e+121) && (d4 <= 3.9e+139))) {
		tmp = t_0;
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d1)
	t_1 = d1 * (d2 - d3)
	tmp = 0
	if d4 <= -1.9e-108:
		tmp = t_0
	elif d4 <= -1.3e-297:
		tmp = t_1
	elif d4 <= 2.35e-122:
		tmp = t_0
	elif d4 <= 9.7e-29:
		tmp = t_1
	elif (d4 <= 6e-20) or (not (d4 <= 8e+121) and (d4 <= 3.9e+139)):
		tmp = t_0
	else:
		tmp = d1 * (d4 - d3)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d1))
	t_1 = Float64(d1 * Float64(d2 - d3))
	tmp = 0.0
	if (d4 <= -1.9e-108)
		tmp = t_0;
	elseif (d4 <= -1.3e-297)
		tmp = t_1;
	elseif (d4 <= 2.35e-122)
		tmp = t_0;
	elseif (d4 <= 9.7e-29)
		tmp = t_1;
	elseif ((d4 <= 6e-20) || (!(d4 <= 8e+121) && (d4 <= 3.9e+139)))
		tmp = t_0;
	else
		tmp = Float64(d1 * Float64(d4 - d3));
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d1);
	t_1 = d1 * (d2 - d3);
	tmp = 0.0;
	if (d4 <= -1.9e-108)
		tmp = t_0;
	elseif (d4 <= -1.3e-297)
		tmp = t_1;
	elseif (d4 <= 2.35e-122)
		tmp = t_0;
	elseif (d4 <= 9.7e-29)
		tmp = t_1;
	elseif ((d4 <= 6e-20) || (~((d4 <= 8e+121)) && (d4 <= 3.9e+139)))
		tmp = t_0;
	else
		tmp = d1 * (d4 - d3);
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d4, -1.9e-108], t$95$0, If[LessEqual[d4, -1.3e-297], t$95$1, If[LessEqual[d4, 2.35e-122], t$95$0, If[LessEqual[d4, 9.7e-29], t$95$1, If[Or[LessEqual[d4, 6e-20], And[N[Not[LessEqual[d4, 8e+121]], $MachinePrecision], LessEqual[d4, 3.9e+139]]], t$95$0, N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d1\right)\\
t_1 := d1 \cdot \left(d2 - d3\right)\\
\mathbf{if}\;d4 \leq -1.9 \cdot 10^{-108}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq -1.3 \cdot 10^{-297}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d4 \leq 2.35 \cdot 10^{-122}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 9.7 \cdot 10^{-29}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d4 \leq 6 \cdot 10^{-20} \lor \neg \left(d4 \leq 8 \cdot 10^{+121}\right) \land d4 \leq 3.9 \cdot 10^{+139}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < -1.89999999999999987e-108 or -1.3e-297 < d4 < 2.35e-122 or 9.7000000000000002e-29 < d4 < 6.00000000000000057e-20 or 8.0000000000000003e121 < d4 < 3.90000000000000006e139

    1. Initial program 90.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+90.4%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--97.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 d3 around 0 80.4%

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

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

    if -1.89999999999999987e-108 < d4 < -1.3e-297 or 2.35e-122 < d4 < 9.7000000000000002e-29

    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.9%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--92.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 d4 around 0 100.0%

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

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

    if 6.00000000000000057e-20 < d4 < 8.0000000000000003e121 or 3.90000000000000006e139 < d4

    1. Initial program 83.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+83.3%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--85.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 d3 around 0 94.4%

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

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

        \[\leadsto \left(d2 + \left(\color{blue}{\left(-d3\right)} + d4\right)\right) \cdot d1 \]
      2. associate-+r+92.7%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -1.9 \cdot 10^{-108}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d4 \leq -1.3 \cdot 10^{-297}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 2.35 \cdot 10^{-122}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d4 \leq 9.7 \cdot 10^{-29}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 6 \cdot 10^{-20} \lor \neg \left(d4 \leq 8 \cdot 10^{+121}\right) \land d4 \leq 3.9 \cdot 10^{+139}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \]

Alternative 4: 50.4% accurate, 1.0× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(-d1\right)\\ \mathbf{if}\;d4 \leq -2.55 \cdot 10^{+14}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -3.3 \cdot 10^{-88}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq -3 \cdot 10^{-169}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 3650:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 2.6 \cdot 10^{+119}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d4 \leq 1.65 \cdot 10^{+145}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d1))))
   (if (<= d4 -2.55e+14)
     (* d1 d2)
     (if (<= d4 -3.3e-88)
       t_0
       (if (<= d4 -3e-169)
         (* d1 d2)
         (if (<= d4 3650.0)
           t_0
           (if (<= d4 2.6e+119)
             (* d1 d4)
             (if (<= d4 1.65e+145) (* d1 d2) (* d1 d4)))))))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d1;
	double tmp;
	if (d4 <= -2.55e+14) {
		tmp = d1 * d2;
	} else if (d4 <= -3.3e-88) {
		tmp = t_0;
	} else if (d4 <= -3e-169) {
		tmp = d1 * d2;
	} else if (d4 <= 3650.0) {
		tmp = t_0;
	} else if (d4 <= 2.6e+119) {
		tmp = d1 * d4;
	} else if (d4 <= 1.65e+145) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 * -d1
    if (d4 <= (-2.55d+14)) then
        tmp = d1 * d2
    else if (d4 <= (-3.3d-88)) then
        tmp = t_0
    else if (d4 <= (-3d-169)) then
        tmp = d1 * d2
    else if (d4 <= 3650.0d0) then
        tmp = t_0
    else if (d4 <= 2.6d+119) then
        tmp = d1 * d4
    else if (d4 <= 1.65d+145) then
        tmp = d1 * d2
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d1;
	double tmp;
	if (d4 <= -2.55e+14) {
		tmp = d1 * d2;
	} else if (d4 <= -3.3e-88) {
		tmp = t_0;
	} else if (d4 <= -3e-169) {
		tmp = d1 * d2;
	} else if (d4 <= 3650.0) {
		tmp = t_0;
	} else if (d4 <= 2.6e+119) {
		tmp = d1 * d4;
	} else if (d4 <= 1.65e+145) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * -d1
	tmp = 0
	if d4 <= -2.55e+14:
		tmp = d1 * d2
	elif d4 <= -3.3e-88:
		tmp = t_0
	elif d4 <= -3e-169:
		tmp = d1 * d2
	elif d4 <= 3650.0:
		tmp = t_0
	elif d4 <= 2.6e+119:
		tmp = d1 * d4
	elif d4 <= 1.65e+145:
		tmp = d1 * d2
	else:
		tmp = d1 * d4
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(-d1))
	tmp = 0.0
	if (d4 <= -2.55e+14)
		tmp = Float64(d1 * d2);
	elseif (d4 <= -3.3e-88)
		tmp = t_0;
	elseif (d4 <= -3e-169)
		tmp = Float64(d1 * d2);
	elseif (d4 <= 3650.0)
		tmp = t_0;
	elseif (d4 <= 2.6e+119)
		tmp = Float64(d1 * d4);
	elseif (d4 <= 1.65e+145)
		tmp = Float64(d1 * d2);
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * -d1;
	tmp = 0.0;
	if (d4 <= -2.55e+14)
		tmp = d1 * d2;
	elseif (d4 <= -3.3e-88)
		tmp = t_0;
	elseif (d4 <= -3e-169)
		tmp = d1 * d2;
	elseif (d4 <= 3650.0)
		tmp = t_0;
	elseif (d4 <= 2.6e+119)
		tmp = d1 * d4;
	elseif (d4 <= 1.65e+145)
		tmp = d1 * d2;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * (-d1)), $MachinePrecision]}, If[LessEqual[d4, -2.55e+14], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, -3.3e-88], t$95$0, If[LessEqual[d4, -3e-169], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, 3650.0], t$95$0, If[LessEqual[d4, 2.6e+119], N[(d1 * d4), $MachinePrecision], If[LessEqual[d4, 1.65e+145], N[(d1 * d2), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(-d1\right)\\
\mathbf{if}\;d4 \leq -2.55 \cdot 10^{+14}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq -3.3 \cdot 10^{-88}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq -3 \cdot 10^{-169}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq 3650:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 2.6 \cdot 10^{+119}:\\
\;\;\;\;d1 \cdot d4\\

\mathbf{elif}\;d4 \leq 1.65 \cdot 10^{+145}:\\
\;\;\;\;d1 \cdot d2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < -2.55e14 or -3.29999999999999994e-88 < d4 < -2.9999999999999999e-169 or 2.6e119 < d4 < 1.65000000000000013e145

    1. Initial program 88.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+88.1%

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

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

        \[\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 27.0%

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

    if -2.55e14 < d4 < -3.29999999999999994e-88 or -2.9999999999999999e-169 < d4 < 3650

    1. Initial program 93.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+93.3%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--96.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 inf 48.4%

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

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

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
      3. distribute-rgt-neg-out48.4%

        \[\leadsto \color{blue}{d1 \cdot \left(-d1\right)} \]
    6. Simplified48.4%

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

    if 3650 < d4 < 2.6e119 or 1.65000000000000013e145 < d4

    1. Initial program 82.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+82.0%

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

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

        \[\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 62.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -2.55 \cdot 10^{+14}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -3.3 \cdot 10^{-88}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq -3 \cdot 10^{-169}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 3650:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 2.6 \cdot 10^{+119}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d4 \leq 1.65 \cdot 10^{+145}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 5: 75.2% accurate, 1.0× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d1\right)\\ \mathbf{if}\;d4 \leq -1 \cdot 10^{-108}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 4.9 \cdot 10^{-259}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 1020:\\ \;\;\;\;\left(d1 + d3\right) \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 8.2 \cdot 10^{+121} \lor \neg \left(d4 \leq 4 \cdot 10^{+139}\right):\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d1))))
   (if (<= d4 -1e-108)
     t_0
     (if (<= d4 4.9e-259)
       (* d1 (- d2 d3))
       (if (<= d4 1020.0)
         (* (+ d1 d3) (- d1))
         (if (or (<= d4 8.2e+121) (not (<= d4 4e+139)))
           (* d1 (- d4 d3))
           t_0))))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d1);
	double tmp;
	if (d4 <= -1e-108) {
		tmp = t_0;
	} else if (d4 <= 4.9e-259) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 1020.0) {
		tmp = (d1 + d3) * -d1;
	} else if ((d4 <= 8.2e+121) || !(d4 <= 4e+139)) {
		tmp = d1 * (d4 - d3);
	} else {
		tmp = t_0;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 - d1)
    if (d4 <= (-1d-108)) then
        tmp = t_0
    else if (d4 <= 4.9d-259) then
        tmp = d1 * (d2 - d3)
    else if (d4 <= 1020.0d0) then
        tmp = (d1 + d3) * -d1
    else if ((d4 <= 8.2d+121) .or. (.not. (d4 <= 4d+139))) then
        tmp = d1 * (d4 - d3)
    else
        tmp = t_0
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d1);
	double tmp;
	if (d4 <= -1e-108) {
		tmp = t_0;
	} else if (d4 <= 4.9e-259) {
		tmp = d1 * (d2 - d3);
	} else if (d4 <= 1020.0) {
		tmp = (d1 + d3) * -d1;
	} else if ((d4 <= 8.2e+121) || !(d4 <= 4e+139)) {
		tmp = d1 * (d4 - d3);
	} else {
		tmp = t_0;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d1)
	tmp = 0
	if d4 <= -1e-108:
		tmp = t_0
	elif d4 <= 4.9e-259:
		tmp = d1 * (d2 - d3)
	elif d4 <= 1020.0:
		tmp = (d1 + d3) * -d1
	elif (d4 <= 8.2e+121) or not (d4 <= 4e+139):
		tmp = d1 * (d4 - d3)
	else:
		tmp = t_0
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d1))
	tmp = 0.0
	if (d4 <= -1e-108)
		tmp = t_0;
	elseif (d4 <= 4.9e-259)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d4 <= 1020.0)
		tmp = Float64(Float64(d1 + d3) * Float64(-d1));
	elseif ((d4 <= 8.2e+121) || !(d4 <= 4e+139))
		tmp = Float64(d1 * Float64(d4 - d3));
	else
		tmp = t_0;
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d1);
	tmp = 0.0;
	if (d4 <= -1e-108)
		tmp = t_0;
	elseif (d4 <= 4.9e-259)
		tmp = d1 * (d2 - d3);
	elseif (d4 <= 1020.0)
		tmp = (d1 + d3) * -d1;
	elseif ((d4 <= 8.2e+121) || ~((d4 <= 4e+139)))
		tmp = d1 * (d4 - d3);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d4, -1e-108], t$95$0, If[LessEqual[d4, 4.9e-259], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 1020.0], N[(N[(d1 + d3), $MachinePrecision] * (-d1)), $MachinePrecision], If[Or[LessEqual[d4, 8.2e+121], N[Not[LessEqual[d4, 4e+139]], $MachinePrecision]], N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d1\right)\\
\mathbf{if}\;d4 \leq -1 \cdot 10^{-108}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 4.9 \cdot 10^{-259}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

\mathbf{elif}\;d4 \leq 1020:\\
\;\;\;\;\left(d1 + d3\right) \cdot \left(-d1\right)\\

\mathbf{elif}\;d4 \leq 8.2 \cdot 10^{+121} \lor \neg \left(d4 \leq 4 \cdot 10^{+139}\right):\\
\;\;\;\;d1 \cdot \left(d4 - d3\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if d4 < -1.00000000000000004e-108 or 8.2e121 < d4 < 4.00000000000000013e139

    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--89.1%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--97.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 d3 around 0 81.3%

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

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

    if -1.00000000000000004e-108 < d4 < 4.90000000000000023e-259

    1. Initial program 95.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+95.2%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--95.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 d4 around 0 100.0%

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

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

    if 4.90000000000000023e-259 < d4 < 1020

    1. Initial program 92.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+92.3%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--96.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 d4 around 0 100.0%

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

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

        \[\leadsto \color{blue}{\left(-\left(d1 + d3\right)\right)} \cdot d1 \]
      2. distribute-neg-in79.7%

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

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

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

    if 1020 < d4 < 8.2e121 or 4.00000000000000013e139 < d4

    1. Initial program 82.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+82.3%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--84.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 d3 around 0 94.1%

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

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

        \[\leadsto \left(d2 + \left(\color{blue}{\left(-d3\right)} + d4\right)\right) \cdot d1 \]
      2. associate-+r+94.1%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -1 \cdot 10^{-108}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d4 \leq 4.9 \cdot 10^{-259}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d4 \leq 1020:\\ \;\;\;\;\left(d1 + d3\right) \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 8.2 \cdot 10^{+121} \lor \neg \left(d4 \leq 4 \cdot 10^{+139}\right):\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \end{array} \]

Alternative 6: 66.5% accurate, 1.1× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(-d3\right)\\ t_1 := d1 \cdot \left(d2 + d4\right)\\ \mathbf{if}\;d3 \leq -1.2 \cdot 10^{+85}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq -4.1 \cdot 10^{-41}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d3 \leq -3.4 \cdot 10^{-73}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d3 \leq 6.2 \cdot 10^{+128}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d3))) (t_1 (* d1 (+ d2 d4))))
   (if (<= d3 -1.2e+85)
     t_0
     (if (<= d3 -4.1e-41)
       t_1
       (if (<= d3 -3.4e-73) (* d1 (- d1)) (if (<= d3 6.2e+128) t_1 t_0))))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double t_1 = d1 * (d2 + d4);
	double tmp;
	if (d3 <= -1.2e+85) {
		tmp = t_0;
	} else if (d3 <= -4.1e-41) {
		tmp = t_1;
	} else if (d3 <= -3.4e-73) {
		tmp = d1 * -d1;
	} else if (d3 <= 6.2e+128) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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) :: tmp
    t_0 = d1 * -d3
    t_1 = d1 * (d2 + d4)
    if (d3 <= (-1.2d+85)) then
        tmp = t_0
    else if (d3 <= (-4.1d-41)) then
        tmp = t_1
    else if (d3 <= (-3.4d-73)) then
        tmp = d1 * -d1
    else if (d3 <= 6.2d+128) then
        tmp = t_1
    else
        tmp = t_0
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double t_1 = d1 * (d2 + d4);
	double tmp;
	if (d3 <= -1.2e+85) {
		tmp = t_0;
	} else if (d3 <= -4.1e-41) {
		tmp = t_1;
	} else if (d3 <= -3.4e-73) {
		tmp = d1 * -d1;
	} else if (d3 <= 6.2e+128) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * -d3
	t_1 = d1 * (d2 + d4)
	tmp = 0
	if d3 <= -1.2e+85:
		tmp = t_0
	elif d3 <= -4.1e-41:
		tmp = t_1
	elif d3 <= -3.4e-73:
		tmp = d1 * -d1
	elif d3 <= 6.2e+128:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(-d3))
	t_1 = Float64(d1 * Float64(d2 + d4))
	tmp = 0.0
	if (d3 <= -1.2e+85)
		tmp = t_0;
	elseif (d3 <= -4.1e-41)
		tmp = t_1;
	elseif (d3 <= -3.4e-73)
		tmp = Float64(d1 * Float64(-d1));
	elseif (d3 <= 6.2e+128)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * -d3;
	t_1 = d1 * (d2 + d4);
	tmp = 0.0;
	if (d3 <= -1.2e+85)
		tmp = t_0;
	elseif (d3 <= -4.1e-41)
		tmp = t_1;
	elseif (d3 <= -3.4e-73)
		tmp = d1 * -d1;
	elseif (d3 <= 6.2e+128)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * (-d3)), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d3, -1.2e+85], t$95$0, If[LessEqual[d3, -4.1e-41], t$95$1, If[LessEqual[d3, -3.4e-73], N[(d1 * (-d1)), $MachinePrecision], If[LessEqual[d3, 6.2e+128], t$95$1, t$95$0]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(-d3\right)\\
t_1 := d1 \cdot \left(d2 + d4\right)\\
\mathbf{if}\;d3 \leq -1.2 \cdot 10^{+85}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq -4.1 \cdot 10^{-41}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d3 \leq -3.4 \cdot 10^{-73}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

\mathbf{elif}\;d3 \leq 6.2 \cdot 10^{+128}:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -1.19999999999999998e85 or 6.20000000000000008e128 < d3

    1. Initial program 83.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+83.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.1%

        \[\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 d3 around inf 82.7%

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

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

        \[\leadsto \color{blue}{\left(-d1\right)} \cdot d3 \]
      3. *-commutative82.7%

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

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

    if -1.19999999999999998e85 < d3 < -4.10000000000000014e-41 or -3.40000000000000021e-73 < d3 < 6.20000000000000008e128

    1. Initial program 92.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+92.0%

        \[\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.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 d3 around 0 94.4%

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

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

    if -4.10000000000000014e-41 < d3 < -3.40000000000000021e-73

    1. Initial program 85.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+85.7%

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

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

        \[\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 87.0%

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

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

        \[\leadsto \color{blue}{-d1 \cdot d1} \]
      3. distribute-rgt-neg-out87.0%

        \[\leadsto \color{blue}{d1 \cdot \left(-d1\right)} \]
    6. Simplified87.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -1.2 \cdot 10^{+85}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d3 \leq -4.1 \cdot 10^{-41}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d3 \leq -3.4 \cdot 10^{-73}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d3 \leq 6.2 \cdot 10^{+128}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \end{array} \]

Alternative 7: 67.5% accurate, 1.3× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(-d3\right)\\ \mathbf{if}\;d3 \leq -6 \cdot 10^{+92}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq -5.8 \cdot 10^{-175}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.02 \cdot 10^{+129}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d3))))
   (if (<= d3 -6e+92)
     t_0
     (if (<= d3 -5.8e-175)
       (* d1 (- d2 d1))
       (if (<= d3 1.02e+129) (* d1 (+ d2 d4)) t_0)))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double tmp;
	if (d3 <= -6e+92) {
		tmp = t_0;
	} else if (d3 <= -5.8e-175) {
		tmp = d1 * (d2 - d1);
	} else if (d3 <= 1.02e+129) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = t_0;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 * -d3
    if (d3 <= (-6d+92)) then
        tmp = t_0
    else if (d3 <= (-5.8d-175)) then
        tmp = d1 * (d2 - d1)
    else if (d3 <= 1.02d+129) then
        tmp = d1 * (d2 + d4)
    else
        tmp = t_0
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double tmp;
	if (d3 <= -6e+92) {
		tmp = t_0;
	} else if (d3 <= -5.8e-175) {
		tmp = d1 * (d2 - d1);
	} else if (d3 <= 1.02e+129) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = t_0;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * -d3
	tmp = 0
	if d3 <= -6e+92:
		tmp = t_0
	elif d3 <= -5.8e-175:
		tmp = d1 * (d2 - d1)
	elif d3 <= 1.02e+129:
		tmp = d1 * (d2 + d4)
	else:
		tmp = t_0
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(-d3))
	tmp = 0.0
	if (d3 <= -6e+92)
		tmp = t_0;
	elseif (d3 <= -5.8e-175)
		tmp = Float64(d1 * Float64(d2 - d1));
	elseif (d3 <= 1.02e+129)
		tmp = Float64(d1 * Float64(d2 + d4));
	else
		tmp = t_0;
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * -d3;
	tmp = 0.0;
	if (d3 <= -6e+92)
		tmp = t_0;
	elseif (d3 <= -5.8e-175)
		tmp = d1 * (d2 - d1);
	elseif (d3 <= 1.02e+129)
		tmp = d1 * (d2 + d4);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * (-d3)), $MachinePrecision]}, If[LessEqual[d3, -6e+92], t$95$0, If[LessEqual[d3, -5.8e-175], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 1.02e+129], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(-d3\right)\\
\mathbf{if}\;d3 \leq -6 \cdot 10^{+92}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq -5.8 \cdot 10^{-175}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

\mathbf{elif}\;d3 \leq 1.02 \cdot 10^{+129}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -6.00000000000000026e92 or 1.01999999999999996e129 < d3

    1. Initial program 83.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+83.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.1%

        \[\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 d3 around inf 82.7%

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

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

        \[\leadsto \color{blue}{\left(-d1\right)} \cdot d3 \]
      3. *-commutative82.7%

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

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

    if -6.00000000000000026e92 < d3 < -5.79999999999999998e-175

    1. Initial program 92.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+92.5%

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

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

        \[\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 d3 around 0 98.0%

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

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

    if -5.79999999999999998e-175 < d3 < 1.01999999999999996e129

    1. Initial program 91.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+91.4%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--95.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 d3 around 0 93.1%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -6 \cdot 10^{+92}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d3 \leq -5.8 \cdot 10^{-175}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.02 \cdot 10^{+129}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \end{array} \]

Alternative 8: 71.4% accurate, 1.3× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d3\right)\\ \mathbf{if}\;d3 \leq -4.1 \cdot 10^{+82}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq -1.3 \cdot 10^{-173}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.12 \cdot 10^{+74}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d3))))
   (if (<= d3 -4.1e+82)
     t_0
     (if (<= d3 -1.3e-173)
       (* d1 (- d2 d1))
       (if (<= d3 1.12e+74) (* d1 (+ d2 d4)) t_0)))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d3 <= -4.1e+82) {
		tmp = t_0;
	} else if (d3 <= -1.3e-173) {
		tmp = d1 * (d2 - d1);
	} else if (d3 <= 1.12e+74) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = t_0;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 (d3 <= (-4.1d+82)) then
        tmp = t_0
    else if (d3 <= (-1.3d-173)) then
        tmp = d1 * (d2 - d1)
    else if (d3 <= 1.12d+74) then
        tmp = d1 * (d2 + d4)
    else
        tmp = t_0
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d3 <= -4.1e+82) {
		tmp = t_0;
	} else if (d3 <= -1.3e-173) {
		tmp = d1 * (d2 - d1);
	} else if (d3 <= 1.12e+74) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = t_0;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d3)
	tmp = 0
	if d3 <= -4.1e+82:
		tmp = t_0
	elif d3 <= -1.3e-173:
		tmp = d1 * (d2 - d1)
	elif d3 <= 1.12e+74:
		tmp = d1 * (d2 + d4)
	else:
		tmp = t_0
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d3))
	tmp = 0.0
	if (d3 <= -4.1e+82)
		tmp = t_0;
	elseif (d3 <= -1.3e-173)
		tmp = Float64(d1 * Float64(d2 - d1));
	elseif (d3 <= 1.12e+74)
		tmp = Float64(d1 * Float64(d2 + d4));
	else
		tmp = t_0;
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d3);
	tmp = 0.0;
	if (d3 <= -4.1e+82)
		tmp = t_0;
	elseif (d3 <= -1.3e-173)
		tmp = d1 * (d2 - d1);
	elseif (d3 <= 1.12e+74)
		tmp = d1 * (d2 + d4);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d3, -4.1e+82], t$95$0, If[LessEqual[d3, -1.3e-173], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 1.12e+74], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d3\right)\\
\mathbf{if}\;d3 \leq -4.1 \cdot 10^{+82}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq -1.3 \cdot 10^{-173}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

\mathbf{elif}\;d3 \leq 1.12 \cdot 10^{+74}:\\
\;\;\;\;d1 \cdot \left(d2 + d4\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -4.09999999999999995e82 or 1.12000000000000003e74 < d3

    1. Initial program 83.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+83.3%

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

        \[\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 0 91.5%

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

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

    if -4.09999999999999995e82 < d3 < -1.30000000000000002e-173

    1. Initial program 92.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+92.5%

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

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

        \[\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 d3 around 0 98.0%

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

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

    if -1.30000000000000002e-173 < d3 < 1.12000000000000003e74

    1. Initial program 92.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+92.5%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--96.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 d3 around 0 96.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -4.1 \cdot 10^{+82}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d3 \leq -1.3 \cdot 10^{-173}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.12 \cdot 10^{+74}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \end{array} \]

Alternative 9: 89.2% accurate, 1.3× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -2.2 \cdot 10^{+97} \lor \neg \left(d3 \leq 2.9 \cdot 10^{+164}\right):\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d1\right)\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (or (<= d3 -2.2e+97) (not (<= d3 2.9e+164)))
   (* d1 (- d4 d3))
   (* d1 (- (+ d2 d4) d1))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -2.2e+97) || !(d3 <= 2.9e+164)) {
		tmp = d1 * (d4 - d3);
	} else {
		tmp = d1 * ((d2 + d4) - d1);
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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.2d+97)) .or. (.not. (d3 <= 2.9d+164))) then
        tmp = d1 * (d4 - d3)
    else
        tmp = d1 * ((d2 + d4) - d1)
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -2.2e+97) || !(d3 <= 2.9e+164)) {
		tmp = d1 * (d4 - d3);
	} else {
		tmp = d1 * ((d2 + d4) - d1);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if (d3 <= -2.2e+97) or not (d3 <= 2.9e+164):
		tmp = d1 * (d4 - d3)
	else:
		tmp = d1 * ((d2 + d4) - d1)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if ((d3 <= -2.2e+97) || !(d3 <= 2.9e+164))
		tmp = Float64(d1 * Float64(d4 - d3));
	else
		tmp = Float64(d1 * Float64(Float64(d2 + d4) - d1));
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if ((d3 <= -2.2e+97) || ~((d3 <= 2.9e+164)))
		tmp = d1 * (d4 - d3);
	else
		tmp = d1 * ((d2 + d4) - d1);
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d3, -2.2e+97], N[Not[LessEqual[d3, 2.9e+164]], $MachinePrecision]], N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d2 + d4), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -2.2 \cdot 10^{+97} \lor \neg \left(d3 \leq 2.9 \cdot 10^{+164}\right):\\
\;\;\;\;d1 \cdot \left(d4 - d3\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d3 < -2.2000000000000001e97 or 2.8999999999999999e164 < d3

    1. Initial program 84.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+84.9%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--91.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 0 93.2%

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

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

        \[\leadsto \left(d2 + \left(\color{blue}{\left(-d3\right)} + d4\right)\right) \cdot d1 \]
      2. associate-+r+95.8%

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

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

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

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

    if -2.2000000000000001e97 < d3 < 2.8999999999999999e164

    1. Initial program 90.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+90.7%

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

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

        \[\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 0 92.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -2.2 \cdot 10^{+97} \lor \neg \left(d3 \leq 2.9 \cdot 10^{+164}\right):\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d1\right)\\ \end{array} \]

Alternative 10: 93.2% accurate, 1.3× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -1.45 \cdot 10^{+87} \lor \neg \left(d3 \leq 1.12 \cdot 10^{+67}\right):\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d1\right)\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (or (<= d3 -1.45e+87) (not (<= d3 1.12e+67)))
   (* d1 (- (+ d2 d4) d3))
   (* d1 (- (+ d2 d4) d1))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -1.45e+87) || !(d3 <= 1.12e+67)) {
		tmp = d1 * ((d2 + d4) - d3);
	} else {
		tmp = d1 * ((d2 + d4) - d1);
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 <= (-1.45d+87)) .or. (.not. (d3 <= 1.12d+67))) then
        tmp = d1 * ((d2 + d4) - d3)
    else
        tmp = d1 * ((d2 + d4) - d1)
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -1.45e+87) || !(d3 <= 1.12e+67)) {
		tmp = d1 * ((d2 + d4) - d3);
	} else {
		tmp = d1 * ((d2 + d4) - d1);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if (d3 <= -1.45e+87) or not (d3 <= 1.12e+67):
		tmp = d1 * ((d2 + d4) - d3)
	else:
		tmp = d1 * ((d2 + d4) - d1)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if ((d3 <= -1.45e+87) || !(d3 <= 1.12e+67))
		tmp = Float64(d1 * Float64(Float64(d2 + d4) - d3));
	else
		tmp = Float64(d1 * Float64(Float64(d2 + d4) - d1));
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if ((d3 <= -1.45e+87) || ~((d3 <= 1.12e+67)))
		tmp = d1 * ((d2 + d4) - d3);
	else
		tmp = d1 * ((d2 + d4) - d1);
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[Or[LessEqual[d3, -1.45e+87], N[Not[LessEqual[d3, 1.12e+67]], $MachinePrecision]], N[(d1 * N[(N[(d2 + d4), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d2 + d4), $MachinePrecision] - d1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -1.45 \cdot 10^{+87} \lor \neg \left(d3 \leq 1.12 \cdot 10^{+67}\right):\\
\;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d3 < -1.4499999999999999e87 or 1.12e67 < d3

    1. Initial program 83.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+83.6%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--90.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 d1 around 0 91.6%

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

    if -1.4499999999999999e87 < d3 < 1.12e67

    1. Initial program 92.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+92.4%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--96.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 d3 around 0 96.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -1.45 \cdot 10^{+87} \lor \neg \left(d3 \leq 1.12 \cdot 10^{+67}\right):\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d1\right)\\ \end{array} \]

Alternative 11: 93.3% accurate, 1.7× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 410:\\ \;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d4 410.0) (* d1 (- d2 (+ d1 d3))) (* d1 (- (+ d2 d4) d3))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 410.0) {
		tmp = d1 * (d2 - (d1 + d3));
	} else {
		tmp = d1 * ((d2 + d4) - d3);
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 <= 410.0d0) then
        tmp = d1 * (d2 - (d1 + d3))
    else
        tmp = d1 * ((d2 + d4) - d3)
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 410.0) {
		tmp = d1 * (d2 - (d1 + d3));
	} else {
		tmp = d1 * ((d2 + d4) - d3);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 410.0:
		tmp = d1 * (d2 - (d1 + d3))
	else:
		tmp = d1 * ((d2 + d4) - d3)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 410.0)
		tmp = Float64(d1 * Float64(d2 - Float64(d1 + d3)));
	else
		tmp = Float64(d1 * Float64(Float64(d2 + d4) - d3));
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= 410.0)
		tmp = d1 * (d2 - (d1 + d3));
	else
		tmp = d1 * ((d2 + d4) - d3);
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 410.0], N[(d1 * N[(d2 - N[(d1 + d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(N[(d2 + d4), $MachinePrecision] - d3), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 410:\\
\;\;\;\;d1 \cdot \left(d2 - \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 d4 < 410

    1. Initial program 92.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+92.2%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--96.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 0 84.6%

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

    if 410 < d4

    1. Initial program 79.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+79.0%

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--87.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 d1 around 0 88.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 410:\\ \;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \end{array} \]

Alternative 12: 49.2% accurate, 3.0× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -82:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d2 -82.0) (* d1 d2) (* d1 d4)))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -82.0) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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 <= (-82.0d0)) then
        tmp = d1 * d2
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -82.0) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -82.0:
		tmp = d1 * d2
	else:
		tmp = d1 * d4
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -82.0)
		tmp = Float64(d1 * d2);
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d2 <= -82.0)
		tmp = d1 * d2;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -82.0], N[(d1 * d2), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -82:\\
\;\;\;\;d1 \cdot d2\\

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


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

    1. Initial program 76.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+76.9%

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

        \[\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 inf 54.5%

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

    if -82 < d2

    1. Initial program 92.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+92.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.6%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--96.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 31.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -82:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 13: 30.2% accurate, 5.0× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ d1 \cdot d4 \end{array} \]
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 d4))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	return d1 * d4;
}
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
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
assert d2 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * d4;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	return d1 * d4
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	return Float64(d1 * d4)
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * d4;
end
NOTE: d2 and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := N[(d1 * d4), $MachinePrecision]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
d1 \cdot d4
\end{array}
Derivation
  1. Initial program 89.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+89.0%

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
    3. distribute-rgt-out--94.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 d4 around inf 31.3%

    \[\leadsto \color{blue}{d1 \cdot d4} \]
  5. Final simplification31.3%

    \[\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 2023171 
(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)))