FastMath dist4

Percentage Accurate: 86.9% → 100.0%
Time: 7.0s
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: 86.9% 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 86.7%

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

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

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

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

Alternative 2: 66.8% accurate, 0.9× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := -d1 \cdot d3\\ t_1 := d1 \cdot \left(d2 + d4\right)\\ \mathbf{if}\;d3 \leq -9 \cdot 10^{+139}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq 1.95 \cdot 10^{-90}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d3 \leq 3.6 \cdot 10^{+31}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.2 \cdot 10^{+82} \lor \neg \left(d3 \leq 1.35 \cdot 10^{+117}\right) \land d3 \leq 4.2 \cdot 10^{+133}:\\ \;\;\;\;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 -9e+139)
     t_0
     (if (<= d3 1.95e-90)
       t_1
       (if (<= d3 3.6e+31)
         (* d1 (- d2 d1))
         (if (or (<= d3 1.2e+82)
                 (and (not (<= d3 1.35e+117)) (<= d3 4.2e+133)))
           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 <= -9e+139) {
		tmp = t_0;
	} else if (d3 <= 1.95e-90) {
		tmp = t_1;
	} else if (d3 <= 3.6e+31) {
		tmp = d1 * (d2 - d1);
	} else if ((d3 <= 1.2e+82) || (!(d3 <= 1.35e+117) && (d3 <= 4.2e+133))) {
		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 <= (-9d+139)) then
        tmp = t_0
    else if (d3 <= 1.95d-90) then
        tmp = t_1
    else if (d3 <= 3.6d+31) then
        tmp = d1 * (d2 - d1)
    else if ((d3 <= 1.2d+82) .or. (.not. (d3 <= 1.35d+117)) .and. (d3 <= 4.2d+133)) 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 <= -9e+139) {
		tmp = t_0;
	} else if (d3 <= 1.95e-90) {
		tmp = t_1;
	} else if (d3 <= 3.6e+31) {
		tmp = d1 * (d2 - d1);
	} else if ((d3 <= 1.2e+82) || (!(d3 <= 1.35e+117) && (d3 <= 4.2e+133))) {
		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 <= -9e+139:
		tmp = t_0
	elif d3 <= 1.95e-90:
		tmp = t_1
	elif d3 <= 3.6e+31:
		tmp = d1 * (d2 - d1)
	elif (d3 <= 1.2e+82) or (not (d3 <= 1.35e+117) and (d3 <= 4.2e+133)):
		tmp = t_1
	else:
		tmp = t_0
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(-Float64(d1 * d3))
	t_1 = Float64(d1 * Float64(d2 + d4))
	tmp = 0.0
	if (d3 <= -9e+139)
		tmp = t_0;
	elseif (d3 <= 1.95e-90)
		tmp = t_1;
	elseif (d3 <= 3.6e+31)
		tmp = Float64(d1 * Float64(d2 - d1));
	elseif ((d3 <= 1.2e+82) || (!(d3 <= 1.35e+117) && (d3 <= 4.2e+133)))
		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 <= -9e+139)
		tmp = t_0;
	elseif (d3 <= 1.95e-90)
		tmp = t_1;
	elseif (d3 <= 3.6e+31)
		tmp = d1 * (d2 - d1);
	elseif ((d3 <= 1.2e+82) || (~((d3 <= 1.35e+117)) && (d3 <= 4.2e+133)))
		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, -9e+139], t$95$0, If[LessEqual[d3, 1.95e-90], t$95$1, If[LessEqual[d3, 3.6e+31], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[d3, 1.2e+82], And[N[Not[LessEqual[d3, 1.35e+117]], $MachinePrecision], LessEqual[d3, 4.2e+133]]], t$95$1, t$95$0]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := -d1 \cdot d3\\
t_1 := d1 \cdot \left(d2 + d4\right)\\
\mathbf{if}\;d3 \leq -9 \cdot 10^{+139}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq 1.95 \cdot 10^{-90}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d3 \leq 3.6 \cdot 10^{+31}:\\
\;\;\;\;d1 \cdot \left(d2 - d1\right)\\

\mathbf{elif}\;d3 \leq 1.2 \cdot 10^{+82} \lor \neg \left(d3 \leq 1.35 \cdot 10^{+117}\right) \land d3 \leq 4.2 \cdot 10^{+133}:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -8.9999999999999999e139 or 1.19999999999999999e82 < d3 < 1.3500000000000001e117 or 4.2e133 < d3

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

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

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

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

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

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

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

    if -8.9999999999999999e139 < d3 < 1.95000000000000002e-90 or 3.59999999999999996e31 < d3 < 1.19999999999999999e82 or 1.3500000000000001e117 < d3 < 4.2e133

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

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

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

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

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

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

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

    if 1.95000000000000002e-90 < d3 < 3.59999999999999996e31

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -9 \cdot 10^{+139}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d3 \leq 1.95 \cdot 10^{-90}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d3 \leq 3.6 \cdot 10^{+31}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.2 \cdot 10^{+82} \lor \neg \left(d3 \leq 1.35 \cdot 10^{+117}\right) \land d3 \leq 4.2 \cdot 10^{+133}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;-d1 \cdot d3\\ \end{array} \]

Alternative 3: 52.9% accurate, 0.9× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := -d1 \cdot d3\\ \mathbf{if}\;d4 \leq -1.65 \cdot 10^{-74}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -3.8 \cdot 10^{-103}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq -1.7 \cdot 10^{-236}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 1.32 \cdot 10^{+17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d4 \leq 2.3 \cdot 10^{+41}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 4.8 \cdot 10^{+58}:\\ \;\;\;\;t_0\\ \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))))
   (if (<= d4 -1.65e-74)
     (* d1 d2)
     (if (<= d4 -3.8e-103)
       t_0
       (if (<= d4 -1.7e-236)
         (* d1 d2)
         (if (<= d4 1.32e+17)
           t_0
           (if (<= d4 2.3e+41)
             (* d1 d2)
             (if (<= d4 4.8e+58) t_0 (* d1 d4)))))))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = -(d1 * d3);
	double tmp;
	if (d4 <= -1.65e-74) {
		tmp = d1 * d2;
	} else if (d4 <= -3.8e-103) {
		tmp = t_0;
	} else if (d4 <= -1.7e-236) {
		tmp = d1 * d2;
	} else if (d4 <= 1.32e+17) {
		tmp = t_0;
	} else if (d4 <= 2.3e+41) {
		tmp = d1 * d2;
	} else if (d4 <= 4.8e+58) {
		tmp = t_0;
	} 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 * d3)
    if (d4 <= (-1.65d-74)) then
        tmp = d1 * d2
    else if (d4 <= (-3.8d-103)) then
        tmp = t_0
    else if (d4 <= (-1.7d-236)) then
        tmp = d1 * d2
    else if (d4 <= 1.32d+17) then
        tmp = t_0
    else if (d4 <= 2.3d+41) then
        tmp = d1 * d2
    else if (d4 <= 4.8d+58) then
        tmp = t_0
    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 tmp;
	if (d4 <= -1.65e-74) {
		tmp = d1 * d2;
	} else if (d4 <= -3.8e-103) {
		tmp = t_0;
	} else if (d4 <= -1.7e-236) {
		tmp = d1 * d2;
	} else if (d4 <= 1.32e+17) {
		tmp = t_0;
	} else if (d4 <= 2.3e+41) {
		tmp = d1 * d2;
	} else if (d4 <= 4.8e+58) {
		tmp = t_0;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	t_0 = -(d1 * d3)
	tmp = 0
	if d4 <= -1.65e-74:
		tmp = d1 * d2
	elif d4 <= -3.8e-103:
		tmp = t_0
	elif d4 <= -1.7e-236:
		tmp = d1 * d2
	elif d4 <= 1.32e+17:
		tmp = t_0
	elif d4 <= 2.3e+41:
		tmp = d1 * d2
	elif d4 <= 4.8e+58:
		tmp = t_0
	else:
		tmp = d1 * d4
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(-Float64(d1 * d3))
	tmp = 0.0
	if (d4 <= -1.65e-74)
		tmp = Float64(d1 * d2);
	elseif (d4 <= -3.8e-103)
		tmp = t_0;
	elseif (d4 <= -1.7e-236)
		tmp = Float64(d1 * d2);
	elseif (d4 <= 1.32e+17)
		tmp = t_0;
	elseif (d4 <= 2.3e+41)
		tmp = Float64(d1 * d2);
	elseif (d4 <= 4.8e+58)
		tmp = t_0;
	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);
	tmp = 0.0;
	if (d4 <= -1.65e-74)
		tmp = d1 * d2;
	elseif (d4 <= -3.8e-103)
		tmp = t_0;
	elseif (d4 <= -1.7e-236)
		tmp = d1 * d2;
	elseif (d4 <= 1.32e+17)
		tmp = t_0;
	elseif (d4 <= 2.3e+41)
		tmp = d1 * d2;
	elseif (d4 <= 4.8e+58)
		tmp = t_0;
	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])}, If[LessEqual[d4, -1.65e-74], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, -3.8e-103], t$95$0, If[LessEqual[d4, -1.7e-236], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, 1.32e+17], t$95$0, If[LessEqual[d4, 2.3e+41], N[(d1 * d2), $MachinePrecision], If[LessEqual[d4, 4.8e+58], t$95$0, N[(d1 * d4), $MachinePrecision]]]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := -d1 \cdot d3\\
\mathbf{if}\;d4 \leq -1.65 \cdot 10^{-74}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq -3.8 \cdot 10^{-103}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq -1.7 \cdot 10^{-236}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d4 \leq 1.32 \cdot 10^{+17}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d4 \leq 2.3 \cdot 10^{+41}:\\
\;\;\;\;d1 \cdot d2\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < -1.64999999999999998e-74 or -3.8000000000000001e-103 < d4 < -1.6999999999999999e-236 or 1.32e17 < d4 < 2.2999999999999998e41

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

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

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

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

    if -1.64999999999999998e-74 < d4 < -3.8000000000000001e-103 or -1.6999999999999999e-236 < d4 < 1.32e17 or 2.2999999999999998e41 < d4 < 4.8e58

    1. Initial program 88.5%

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

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

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

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

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

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

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

    if 4.8e58 < d4

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

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      2. distribute-lft-out--83.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 inf 72.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -1.65 \cdot 10^{-74}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq -3.8 \cdot 10^{-103}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d4 \leq -1.7 \cdot 10^{-236}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 1.32 \cdot 10^{+17}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d4 \leq 2.3 \cdot 10^{+41}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 4.8 \cdot 10^{+58}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 4: 66.5% accurate, 1.1× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} t_0 := -d1 \cdot d3\\ t_1 := d1 \cdot \left(d2 + d4\right)\\ \mathbf{if}\;d3 \leq -3.2 \cdot 10^{+136}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d3 \leq 2.7 \cdot 10^{-9}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d3 \leq 8.5 \cdot 10^{+27}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d3 \leq 5.4 \cdot 10^{+101}:\\ \;\;\;\;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 -3.2e+136)
     t_0
     (if (<= d3 2.7e-9)
       t_1
       (if (<= d3 8.5e+27) (* d1 (- d1)) (if (<= d3 5.4e+101) 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 <= -3.2e+136) {
		tmp = t_0;
	} else if (d3 <= 2.7e-9) {
		tmp = t_1;
	} else if (d3 <= 8.5e+27) {
		tmp = d1 * -d1;
	} else if (d3 <= 5.4e+101) {
		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 <= (-3.2d+136)) then
        tmp = t_0
    else if (d3 <= 2.7d-9) then
        tmp = t_1
    else if (d3 <= 8.5d+27) then
        tmp = d1 * -d1
    else if (d3 <= 5.4d+101) 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 <= -3.2e+136) {
		tmp = t_0;
	} else if (d3 <= 2.7e-9) {
		tmp = t_1;
	} else if (d3 <= 8.5e+27) {
		tmp = d1 * -d1;
	} else if (d3 <= 5.4e+101) {
		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 <= -3.2e+136:
		tmp = t_0
	elif d3 <= 2.7e-9:
		tmp = t_1
	elif d3 <= 8.5e+27:
		tmp = d1 * -d1
	elif d3 <= 5.4e+101:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(-Float64(d1 * d3))
	t_1 = Float64(d1 * Float64(d2 + d4))
	tmp = 0.0
	if (d3 <= -3.2e+136)
		tmp = t_0;
	elseif (d3 <= 2.7e-9)
		tmp = t_1;
	elseif (d3 <= 8.5e+27)
		tmp = Float64(d1 * Float64(-d1));
	elseif (d3 <= 5.4e+101)
		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 <= -3.2e+136)
		tmp = t_0;
	elseif (d3 <= 2.7e-9)
		tmp = t_1;
	elseif (d3 <= 8.5e+27)
		tmp = d1 * -d1;
	elseif (d3 <= 5.4e+101)
		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, -3.2e+136], t$95$0, If[LessEqual[d3, 2.7e-9], t$95$1, If[LessEqual[d3, 8.5e+27], N[(d1 * (-d1)), $MachinePrecision], If[LessEqual[d3, 5.4e+101], t$95$1, t$95$0]]]]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
t_0 := -d1 \cdot d3\\
t_1 := d1 \cdot \left(d2 + d4\right)\\
\mathbf{if}\;d3 \leq -3.2 \cdot 10^{+136}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d3 \leq 2.7 \cdot 10^{-9}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d3 \leq 8.5 \cdot 10^{+27}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

\mathbf{elif}\;d3 \leq 5.4 \cdot 10^{+101}:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -3.19999999999999988e136 or 5.40000000000000012e101 < d3

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

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

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

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

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

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

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

    if -3.19999999999999988e136 < d3 < 2.7000000000000002e-9 or 8.5e27 < d3 < 5.40000000000000012e101

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

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

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

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

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

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

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

    if 2.7000000000000002e-9 < d3 < 8.5e27

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{-{d1}^{2}} \]
      2. unpow271.7%

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

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

      \[\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 -3.2 \cdot 10^{+136}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d3 \leq 2.7 \cdot 10^{-9}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d3 \leq 8.5 \cdot 10^{+27}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d3 \leq 5.4 \cdot 10^{+101}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;-d1 \cdot d3\\ \end{array} \]

Alternative 5: 91.0% accurate, 1.3× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -2.1 \cdot 10^{+84} \lor \neg \left(d3 \leq 5.2 \cdot 10^{+101}\right):\\ \;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d1\right)\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.1e+84) (not (<= d3 5.2e+101)))
   (* d1 (- d2 (+ d1 d3)))
   (* d1 (+ d2 (- d4 d1)))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if ((d3 <= -2.1e+84) || !(d3 <= 5.2e+101)) {
		tmp = d1 * (d2 - (d1 + 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.1d+84)) .or. (.not. (d3 <= 5.2d+101))) then
        tmp = d1 * (d2 - (d1 + 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.1e+84) || !(d3 <= 5.2e+101)) {
		tmp = d1 * (d2 - (d1 + 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.1e+84) or not (d3 <= 5.2e+101):
		tmp = d1 * (d2 - (d1 + 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.1e+84) || !(d3 <= 5.2e+101))
		tmp = Float64(d1 * Float64(d2 - Float64(d1 + d3)));
	else
		tmp = Float64(d1 * Float64(d2 + Float64(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.1e+84) || ~((d3 <= 5.2e+101)))
		tmp = d1 * (d2 - (d1 + 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.1e+84], N[Not[LessEqual[d3, 5.2e+101]], $MachinePrecision]], N[(d1 * N[(d2 - N[(d1 + d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d2 + N[(d4 - d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -2.1 \cdot 10^{+84} \lor \neg \left(d3 \leq 5.2 \cdot 10^{+101}\right):\\
\;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d3 < -2.10000000000000019e84 or 5.2e101 < d3

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

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

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

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

    if -2.10000000000000019e84 < d3 < 5.2e101

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -2.1 \cdot 10^{+84} \lor \neg \left(d3 \leq 5.2 \cdot 10^{+101}\right):\\ \;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d1\right)\right)\\ \end{array} \]

Alternative 6: 87.7% accurate, 1.3× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d3 \leq -1.46 \cdot 10^{+85}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d3 \leq 5.8 \cdot 10^{+134}:\\ \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(-d1\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 (<= d3 -1.46e+85)
   (* d1 (- d2 d3))
   (if (<= d3 5.8e+134) (* d1 (+ d2 (- d4 d1))) (* d1 (- (- d1) d3)))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d3 <= -1.46e+85) {
		tmp = d1 * (d2 - d3);
	} else if (d3 <= 5.8e+134) {
		tmp = d1 * (d2 + (d4 - d1));
	} else {
		tmp = d1 * (-d1 - 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 (d3 <= (-1.46d+85)) then
        tmp = d1 * (d2 - d3)
    else if (d3 <= 5.8d+134) then
        tmp = d1 * (d2 + (d4 - d1))
    else
        tmp = d1 * (-d1 - 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 (d3 <= -1.46e+85) {
		tmp = d1 * (d2 - d3);
	} else if (d3 <= 5.8e+134) {
		tmp = d1 * (d2 + (d4 - d1));
	} else {
		tmp = d1 * (-d1 - d3);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d3 <= -1.46e+85:
		tmp = d1 * (d2 - d3)
	elif d3 <= 5.8e+134:
		tmp = d1 * (d2 + (d4 - d1))
	else:
		tmp = d1 * (-d1 - d3)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d3 <= -1.46e+85)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d3 <= 5.8e+134)
		tmp = Float64(d1 * Float64(d2 + Float64(d4 - d1)));
	else
		tmp = Float64(d1 * Float64(Float64(-d1) - d3));
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d3 <= -1.46e+85)
		tmp = d1 * (d2 - d3);
	elseif (d3 <= 5.8e+134)
		tmp = d1 * (d2 + (d4 - d1));
	else
		tmp = d1 * (-d1 - 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[d3, -1.46e+85], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 5.8e+134], N[(d1 * N[(d2 + N[(d4 - d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[((-d1) - d3), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -1.46 \cdot 10^{+85}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -1.46e85

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

        \[\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.4%

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

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

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

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

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

    if -1.46e85 < d3 < 5.80000000000000023e134

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

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

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

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

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

    if 5.80000000000000023e134 < d3

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--76.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. Step-by-step derivation
      1. +-commutative100.0%

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

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

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right) + -1 \cdot {d1}^{2}} \]
      2. mul-1-neg72.1%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{\left(-{d1}^{2}\right)} \]
      3. unpow272.1%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \left(-\color{blue}{d1 \cdot d1}\right) \]
      4. distribute-rgt-neg-in72.1%

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

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(-d1\right)\right)} \]
      6. unsub-neg95.4%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -1.46 \cdot 10^{+85}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d3 \leq 5.8 \cdot 10^{+134}:\\ \;\;\;\;d1 \cdot \left(d2 + \left(d4 - d1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(-d1\right) - d3\right)\\ \end{array} \]

Alternative 7: 76.5% accurate, 1.5× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -9 \cdot 10^{+36}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d2 \leq 7 \cdot 10^{-295}:\\ \;\;\;\;d1 \cdot \left(\left(-d1\right) - d3\right)\\ \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
 (if (<= d2 -9e+36)
   (* d1 (- d2 d3))
   (if (<= d2 7e-295) (* d1 (- (- d1) d3)) (* d1 (- d4 d3)))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -9e+36) {
		tmp = d1 * (d2 - d3);
	} else if (d2 <= 7e-295) {
		tmp = d1 * (-d1 - d3);
	} 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) :: tmp
    if (d2 <= (-9d+36)) then
        tmp = d1 * (d2 - d3)
    else if (d2 <= 7d-295) then
        tmp = d1 * (-d1 - d3)
    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 tmp;
	if (d2 <= -9e+36) {
		tmp = d1 * (d2 - d3);
	} else if (d2 <= 7e-295) {
		tmp = d1 * (-d1 - d3);
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -9e+36:
		tmp = d1 * (d2 - d3)
	elif d2 <= 7e-295:
		tmp = d1 * (-d1 - d3)
	else:
		tmp = d1 * (d4 - d3)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -9e+36)
		tmp = Float64(d1 * Float64(d2 - d3));
	elseif (d2 <= 7e-295)
		tmp = Float64(d1 * Float64(Float64(-d1) - d3));
	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)
	tmp = 0.0;
	if (d2 <= -9e+36)
		tmp = d1 * (d2 - d3);
	elseif (d2 <= 7e-295)
		tmp = d1 * (-d1 - d3);
	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_] := If[LessEqual[d2, -9e+36], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], If[LessEqual[d2, 7e-295], N[(d1 * N[((-d1) - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -9 \cdot 10^{+36}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

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

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


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

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

        \[\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--83.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 0 85.7%

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

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

    if -8.99999999999999994e36 < d2 < 6.99999999999999977e-295

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

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

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right) + d1 \cdot \left(d2 - d3\right)} \]
    5. Applied egg-rr90.1%

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

      \[\leadsto \color{blue}{-1 \cdot {d1}^{2} + d1 \cdot \left(d2 - d3\right)} \]
    7. Step-by-step derivation
      1. +-commutative62.1%

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right) + -1 \cdot {d1}^{2}} \]
      2. mul-1-neg62.1%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \color{blue}{\left(-{d1}^{2}\right)} \]
      3. unpow262.1%

        \[\leadsto d1 \cdot \left(d2 - d3\right) + \left(-\color{blue}{d1 \cdot d1}\right) \]
      4. distribute-rgt-neg-in62.1%

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

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(-d1\right)\right)} \]
      6. unsub-neg73.2%

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d2 - d3\right) - d1\right)} \]
    8. Simplified73.2%

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

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

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

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

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

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

    if 6.99999999999999977e-295 < d2

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -9 \cdot 10^{+36}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d2 \leq 7 \cdot 10^{-295}:\\ \;\;\;\;d1 \cdot \left(\left(-d1\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d3\right)\\ \end{array} \]

Alternative 8: 93.5% accurate, 1.7× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 6.2 \cdot 10^{+54}:\\ \;\;\;\;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 6.2e+54) (* 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 <= 6.2e+54) {
		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 <= 6.2d+54) 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 <= 6.2e+54) {
		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 <= 6.2e+54:
		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 <= 6.2e+54)
		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 <= 6.2e+54)
		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, 6.2e+54], 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 6.2 \cdot 10^{+54}:\\
\;\;\;\;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 < 6.1999999999999999e54

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

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

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

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

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

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

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

    if 6.1999999999999999e54 < d4

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

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

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

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

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

Alternative 9: 53.0% accurate, 1.8× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -8.8 \cdot 10^{+36}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq 1.4 \cdot 10^{-293}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \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 -8.8e+36) (* d1 d2) (if (<= d2 1.4e-293) (* d1 (- d1)) (* d1 d4))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -8.8e+36) {
		tmp = d1 * d2;
	} else if (d2 <= 1.4e-293) {
		tmp = d1 * -d1;
	} 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 <= (-8.8d+36)) then
        tmp = d1 * d2
    else if (d2 <= 1.4d-293) then
        tmp = d1 * -d1
    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 <= -8.8e+36) {
		tmp = d1 * d2;
	} else if (d2 <= 1.4e-293) {
		tmp = d1 * -d1;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -8.8e+36:
		tmp = d1 * d2
	elif d2 <= 1.4e-293:
		tmp = d1 * -d1
	else:
		tmp = d1 * d4
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -8.8e+36)
		tmp = Float64(d1 * d2);
	elseif (d2 <= 1.4e-293)
		tmp = Float64(d1 * Float64(-d1));
	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 <= -8.8e+36)
		tmp = d1 * d2;
	elseif (d2 <= 1.4e-293)
		tmp = d1 * -d1;
	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, -8.8e+36], N[(d1 * d2), $MachinePrecision], If[LessEqual[d2, 1.4e-293], N[(d1 * (-d1)), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -8.8 \cdot 10^{+36}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d2 \leq 1.4 \cdot 10^{-293}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

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


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

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

        \[\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--83.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 d2 around inf 53.8%

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

    if -8.80000000000000002e36 < d2 < 1.40000000000000013e-293

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

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

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

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

        \[\leadsto \color{blue}{-{d1}^{2}} \]
      2. unpow233.2%

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

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

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

    if 1.40000000000000013e-293 < d2

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -8.8 \cdot 10^{+36}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq 1.4 \cdot 10^{-293}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 10: 72.3% accurate, 2.1× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 4.7 \cdot 10^{+57}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\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 4.7e+57) (* d1 (- d2 d3)) (* d1 (+ d2 d4))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 4.7e+57) {
		tmp = d1 * (d2 - d3);
	} else {
		tmp = d1 * (d2 + 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 (d4 <= 4.7d+57) then
        tmp = d1 * (d2 - d3)
    else
        tmp = d1 * (d2 + 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 (d4 <= 4.7e+57) {
		tmp = d1 * (d2 - d3);
	} else {
		tmp = d1 * (d2 + d4);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 4.7e+57:
		tmp = d1 * (d2 - d3)
	else:
		tmp = d1 * (d2 + d4)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 4.7e+57)
		tmp = Float64(d1 * Float64(d2 - d3));
	else
		tmp = Float64(d1 * Float64(d2 + d4));
	end
	return tmp
end
d2, d4 = num2cell(sort([d2, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= 4.7e+57)
		tmp = d1 * (d2 - d3);
	else
		tmp = d1 * (d2 + 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[d4, 4.7e+57], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 4.7 \cdot 10^{+57}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

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


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

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

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

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

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

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

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

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

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

    if 4.7000000000000003e57 < d4

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

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

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

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

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

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

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

Alternative 11: 74.2% accurate, 2.1× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d2 \leq -4.5 \cdot 10^{+36}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \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
 (if (<= d2 -4.5e+36) (* d1 (- d2 d3)) (* d1 (- d4 d3))))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -4.5e+36) {
		tmp = d1 * (d2 - d3);
	} 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) :: tmp
    if (d2 <= (-4.5d+36)) then
        tmp = d1 * (d2 - d3)
    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 tmp;
	if (d2 <= -4.5e+36) {
		tmp = d1 * (d2 - d3);
	} else {
		tmp = d1 * (d4 - d3);
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -4.5e+36:
		tmp = d1 * (d2 - d3)
	else:
		tmp = d1 * (d4 - d3)
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -4.5e+36)
		tmp = Float64(d1 * Float64(d2 - d3));
	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)
	tmp = 0.0;
	if (d2 <= -4.5e+36)
		tmp = d1 * (d2 - d3);
	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_] := If[LessEqual[d2, -4.5e+36], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d4 - d3), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -4.5 \cdot 10^{+36}:\\
\;\;\;\;d1 \cdot \left(d2 - d3\right)\\

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


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

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

        \[\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--83.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 0 85.7%

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

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

    if -4.49999999999999997e36 < d2

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

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

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

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

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

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

Alternative 12: 50.5% accurate, 3.0× speedup?

\[\begin{array}{l} [d2, d4] = \mathsf{sort}([d2, d4])\\ \\ \begin{array}{l} \mathbf{if}\;d4 \leq 3 \cdot 10^{+37}:\\ \;\;\;\;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 (<= d4 3e+37) (* d1 d2) (* d1 d4)))
assert(d2 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 3e+37) {
		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 (d4 <= 3d+37) 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 (d4 <= 3e+37) {
		tmp = d1 * d2;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
[d2, d4] = sort([d2, d4])
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 3e+37:
		tmp = d1 * d2
	else:
		tmp = d1 * d4
	return tmp
d2, d4 = sort([d2, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 3e+37)
		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 (d4 <= 3e+37)
		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[d4, 3e+37], N[(d1 * d2), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]
\begin{array}{l}
[d2, d4] = \mathsf{sort}([d2, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;d4 \leq 3 \cdot 10^{+37}:\\
\;\;\;\;d1 \cdot d2\\

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


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

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

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

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

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

    if 3.00000000000000022e37 < 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--85.0%

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

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

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

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

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

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

Alternative 13: 30.8% accurate, 5.0× speedup?

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

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

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

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

    \[\leadsto \color{blue}{d1 \cdot d2} \]
  5. Final simplification23.3%

    \[\leadsto d1 \cdot d2 \]

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 2023297 
(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)))