FastMath dist4

Percentage Accurate: 87.3% → 100.0%
Time: 5.9s
Alternatives: 11
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 11 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.3% accurate, 1.0× speedup?

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

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

Alternative 1: 100.0% accurate, 1.7× speedup?

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

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

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

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

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

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

Alternative 2: 64.0% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := -d1 \cdot d3\\ t_1 := d1 \cdot \left(-d1\right)\\ t_2 := d1 \cdot \left(d2 + d4\right)\\ \mathbf{if}\;d1 \leq -2.2 \cdot 10^{+125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d1 \leq -5.8 \cdot 10^{-63}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d1 \leq -1.5 \cdot 10^{-79}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d1 \leq 4.7 \cdot 10^{-130}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d1 \leq 4.2 \cdot 10^{-47}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d1 \leq 3.5 \cdot 10^{+178}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (- (* d1 d3))) (t_1 (* d1 (- d1))) (t_2 (* d1 (+ d2 d4))))
   (if (<= d1 -2.2e+125)
     t_1
     (if (<= d1 -5.8e-63)
       t_2
       (if (<= d1 -1.5e-79)
         t_0
         (if (<= d1 4.7e-130)
           t_2
           (if (<= d1 4.2e-47) t_0 (if (<= d1 3.5e+178) t_2 t_1))))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = -(d1 * d3);
	double t_1 = d1 * -d1;
	double t_2 = d1 * (d2 + d4);
	double tmp;
	if (d1 <= -2.2e+125) {
		tmp = t_1;
	} else if (d1 <= -5.8e-63) {
		tmp = t_2;
	} else if (d1 <= -1.5e-79) {
		tmp = t_0;
	} else if (d1 <= 4.7e-130) {
		tmp = t_2;
	} else if (d1 <= 4.2e-47) {
		tmp = t_0;
	} else if (d1 <= 3.5e+178) {
		tmp = t_2;
	} else {
		tmp = t_1;
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = -(d1 * d3)
    t_1 = d1 * -d1
    t_2 = d1 * (d2 + d4)
    if (d1 <= (-2.2d+125)) then
        tmp = t_1
    else if (d1 <= (-5.8d-63)) then
        tmp = t_2
    else if (d1 <= (-1.5d-79)) then
        tmp = t_0
    else if (d1 <= 4.7d-130) then
        tmp = t_2
    else if (d1 <= 4.2d-47) then
        tmp = t_0
    else if (d1 <= 3.5d+178) then
        tmp = t_2
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = -(d1 * d3);
	double t_1 = d1 * -d1;
	double t_2 = d1 * (d2 + d4);
	double tmp;
	if (d1 <= -2.2e+125) {
		tmp = t_1;
	} else if (d1 <= -5.8e-63) {
		tmp = t_2;
	} else if (d1 <= -1.5e-79) {
		tmp = t_0;
	} else if (d1 <= 4.7e-130) {
		tmp = t_2;
	} else if (d1 <= 4.2e-47) {
		tmp = t_0;
	} else if (d1 <= 3.5e+178) {
		tmp = t_2;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = -(d1 * d3)
	t_1 = d1 * -d1
	t_2 = d1 * (d2 + d4)
	tmp = 0
	if d1 <= -2.2e+125:
		tmp = t_1
	elif d1 <= -5.8e-63:
		tmp = t_2
	elif d1 <= -1.5e-79:
		tmp = t_0
	elif d1 <= 4.7e-130:
		tmp = t_2
	elif d1 <= 4.2e-47:
		tmp = t_0
	elif d1 <= 3.5e+178:
		tmp = t_2
	else:
		tmp = t_1
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(-Float64(d1 * d3))
	t_1 = Float64(d1 * Float64(-d1))
	t_2 = Float64(d1 * Float64(d2 + d4))
	tmp = 0.0
	if (d1 <= -2.2e+125)
		tmp = t_1;
	elseif (d1 <= -5.8e-63)
		tmp = t_2;
	elseif (d1 <= -1.5e-79)
		tmp = t_0;
	elseif (d1 <= 4.7e-130)
		tmp = t_2;
	elseif (d1 <= 4.2e-47)
		tmp = t_0;
	elseif (d1 <= 3.5e+178)
		tmp = t_2;
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = -(d1 * d3);
	t_1 = d1 * -d1;
	t_2 = d1 * (d2 + d4);
	tmp = 0.0;
	if (d1 <= -2.2e+125)
		tmp = t_1;
	elseif (d1 <= -5.8e-63)
		tmp = t_2;
	elseif (d1 <= -1.5e-79)
		tmp = t_0;
	elseif (d1 <= 4.7e-130)
		tmp = t_2;
	elseif (d1 <= 4.2e-47)
		tmp = t_0;
	elseif (d1 <= 3.5e+178)
		tmp = t_2;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = (-N[(d1 * d3), $MachinePrecision])}, Block[{t$95$1 = N[(d1 * (-d1)), $MachinePrecision]}, Block[{t$95$2 = N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d1, -2.2e+125], t$95$1, If[LessEqual[d1, -5.8e-63], t$95$2, If[LessEqual[d1, -1.5e-79], t$95$0, If[LessEqual[d1, 4.7e-130], t$95$2, If[LessEqual[d1, 4.2e-47], t$95$0, If[LessEqual[d1, 3.5e+178], t$95$2, t$95$1]]]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;d1 \leq -5.8 \cdot 10^{-63}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d1 \leq -1.5 \cdot 10^{-79}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d1 \leq 4.7 \cdot 10^{-130}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;d1 \leq 4.2 \cdot 10^{-47}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d1 \leq 3.5 \cdot 10^{+178}:\\
\;\;\;\;t_2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d1 < -2.19999999999999991e125 or 3.5e178 < d1

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

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

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

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

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

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

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

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

    if -2.19999999999999991e125 < d1 < -5.7999999999999995e-63 or -1.5e-79 < d1 < 4.69999999999999968e-130 or 4.2000000000000001e-47 < d1 < 3.5e178

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

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

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

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

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

    if -5.7999999999999995e-63 < d1 < -1.5e-79 or 4.69999999999999968e-130 < d1 < 4.2000000000000001e-47

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d1 \leq -2.2 \cdot 10^{+125}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d1 \leq -5.8 \cdot 10^{-63}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d1 \leq -1.5 \cdot 10^{-79}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d1 \leq 4.7 \cdot 10^{-130}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{elif}\;d1 \leq 4.2 \cdot 10^{-47}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d1 \leq 3.5 \cdot 10^{+178}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \end{array} \]

Alternative 3: 39.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := -d1 \cdot d3\\ t_1 := d1 \cdot \left(-d1\right)\\ \mathbf{if}\;d2 \leq -1.45 \cdot 10^{+88}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq -5.6 \cdot 10^{-104}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d2 \leq -8.2 \cdot 10^{-272}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d2 \leq 6 \cdot 10^{-251}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d2 \leq 2.75 \cdot 10^{-188}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (- (* d1 d3))) (t_1 (* d1 (- d1))))
   (if (<= d2 -1.45e+88)
     (* d1 d2)
     (if (<= d2 -5.6e-104)
       t_1
       (if (<= d2 -8.2e-272)
         t_0
         (if (<= d2 6e-251) t_1 (if (<= d2 2.75e-188) t_0 (* d1 d4))))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = -(d1 * d3);
	double t_1 = d1 * -d1;
	double tmp;
	if (d2 <= -1.45e+88) {
		tmp = d1 * d2;
	} else if (d2 <= -5.6e-104) {
		tmp = t_1;
	} else if (d2 <= -8.2e-272) {
		tmp = t_0;
	} else if (d2 <= 6e-251) {
		tmp = t_1;
	} else if (d2 <= 2.75e-188) {
		tmp = t_0;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = -(d1 * d3)
    t_1 = d1 * -d1
    if (d2 <= (-1.45d+88)) then
        tmp = d1 * d2
    else if (d2 <= (-5.6d-104)) then
        tmp = t_1
    else if (d2 <= (-8.2d-272)) then
        tmp = t_0
    else if (d2 <= 6d-251) then
        tmp = t_1
    else if (d2 <= 2.75d-188) then
        tmp = t_0
    else
        tmp = d1 * d4
    end if
    code = tmp
end function
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 (d2 <= -1.45e+88) {
		tmp = d1 * d2;
	} else if (d2 <= -5.6e-104) {
		tmp = t_1;
	} else if (d2 <= -8.2e-272) {
		tmp = t_0;
	} else if (d2 <= 6e-251) {
		tmp = t_1;
	} else if (d2 <= 2.75e-188) {
		tmp = t_0;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = -(d1 * d3)
	t_1 = d1 * -d1
	tmp = 0
	if d2 <= -1.45e+88:
		tmp = d1 * d2
	elif d2 <= -5.6e-104:
		tmp = t_1
	elif d2 <= -8.2e-272:
		tmp = t_0
	elif d2 <= 6e-251:
		tmp = t_1
	elif d2 <= 2.75e-188:
		tmp = t_0
	else:
		tmp = d1 * d4
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(-Float64(d1 * d3))
	t_1 = Float64(d1 * Float64(-d1))
	tmp = 0.0
	if (d2 <= -1.45e+88)
		tmp = Float64(d1 * d2);
	elseif (d2 <= -5.6e-104)
		tmp = t_1;
	elseif (d2 <= -8.2e-272)
		tmp = t_0;
	elseif (d2 <= 6e-251)
		tmp = t_1;
	elseif (d2 <= 2.75e-188)
		tmp = t_0;
	else
		tmp = Float64(d1 * d4);
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = -(d1 * d3);
	t_1 = d1 * -d1;
	tmp = 0.0;
	if (d2 <= -1.45e+88)
		tmp = d1 * d2;
	elseif (d2 <= -5.6e-104)
		tmp = t_1;
	elseif (d2 <= -8.2e-272)
		tmp = t_0;
	elseif (d2 <= 6e-251)
		tmp = t_1;
	elseif (d2 <= 2.75e-188)
		tmp = t_0;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = (-N[(d1 * d3), $MachinePrecision])}, Block[{t$95$1 = N[(d1 * (-d1)), $MachinePrecision]}, If[LessEqual[d2, -1.45e+88], N[(d1 * d2), $MachinePrecision], If[LessEqual[d2, -5.6e-104], t$95$1, If[LessEqual[d2, -8.2e-272], t$95$0, If[LessEqual[d2, 6e-251], t$95$1, If[LessEqual[d2, 2.75e-188], t$95$0, N[(d1 * d4), $MachinePrecision]]]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;d2 \leq -5.6 \cdot 10^{-104}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d2 \leq -8.2 \cdot 10^{-272}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;d2 \leq 6 \cdot 10^{-251}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d2 \leq 2.75 \cdot 10^{-188}:\\
\;\;\;\;t_0\\

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


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

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

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

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

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

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

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

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

    if -1.45e88 < d2 < -5.6e-104 or -8.1999999999999995e-272 < d2 < 5.9999999999999997e-251

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

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

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

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

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

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

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

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

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

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

    if -5.6e-104 < d2 < -8.1999999999999995e-272 or 5.9999999999999997e-251 < d2 < 2.7500000000000001e-188

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

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

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

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

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

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

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

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

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

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

    if 2.7500000000000001e-188 < d2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -1.45 \cdot 10^{+88}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq -5.6 \cdot 10^{-104}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d2 \leq -8.2 \cdot 10^{-272}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{elif}\;d2 \leq 6 \cdot 10^{-251}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d2 \leq 2.75 \cdot 10^{-188}:\\ \;\;\;\;-d1 \cdot d3\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 4: 62.2% accurate, 1.1× speedup?

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

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

\mathbf{elif}\;d4 \leq 2.4 \cdot 10^{-38} \lor \neg \left(d4 \leq 420000000\right) \land d4 \leq 1.5 \cdot 10^{+71}:\\
\;\;\;\;\left(d1 + d3\right) \cdot \left(-d1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d4 < 2.59999999999999975e-203

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

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

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

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

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

    if 2.59999999999999975e-203 < d4 < 2.40000000000000022e-38 or 4.2e8 < d4 < 1.50000000000000006e71

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

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

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

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

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

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

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

    if 2.40000000000000022e-38 < d4 < 4.2e8 or 1.50000000000000006e71 < d4

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq 2.6 \cdot 10^{-203}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d4 \leq 2.4 \cdot 10^{-38} \lor \neg \left(d4 \leq 420000000\right) \land d4 \leq 1.5 \cdot 10^{+71}:\\ \;\;\;\;\left(d1 + d3\right) \cdot \left(-d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \end{array} \]

Alternative 5: 39.2% accurate, 1.2× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -1.42 \cdot 10^{+88}:\\
\;\;\;\;d1 \cdot d2\\

\mathbf{elif}\;d2 \leq -1.3 \cdot 10^{-116} \lor \neg \left(d2 \leq -8 \cdot 10^{-256}\right) \land d2 \leq 7.5 \cdot 10^{-251}:\\
\;\;\;\;d1 \cdot \left(-d1\right)\\

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


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

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

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

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

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

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

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

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

    if -1.41999999999999996e88 < d2 < -1.3e-116 or -7.99999999999999982e-256 < d2 < 7.5000000000000004e-251

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

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

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

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

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

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

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

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

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

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

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

    if -1.3e-116 < d2 < -7.99999999999999982e-256 or 7.5000000000000004e-251 < d2

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d2 \leq -1.42 \cdot 10^{+88}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq -1.3 \cdot 10^{-116} \lor \neg \left(d2 \leq -8 \cdot 10^{-256}\right) \land d2 \leq 7.5 \cdot 10^{-251}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 6: 82.7% accurate, 1.7× speedup?

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

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

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


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

    1. Initial program 88.9%

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

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

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

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

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

    if -7.50000000000000065e153 < d2

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

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

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

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

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

Alternative 7: 83.6% accurate, 1.7× speedup?

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

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

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


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

    1. Initial program 88.9%

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

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

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

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

    if -2.70000000000000019e148 < d2

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

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

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

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

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

Alternative 8: 62.5% accurate, 2.1× speedup?

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

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

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


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

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

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

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

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

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

    if -9.20000000000000022e97 < d2

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

        \[\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 d2 around 0 79.4%

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

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

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

Alternative 9: 62.0% accurate, 2.1× speedup?

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

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

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


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

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

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

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

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

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

    if -3.2e53 < d2

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

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

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

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

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

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

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

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

Alternative 10: 39.0% accurate, 3.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;d2 \leq -2.5 \cdot 10^{+55}:\\
\;\;\;\;d1 \cdot d2\\

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


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

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

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

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

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

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

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

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

    if -2.50000000000000023e55 < d2

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

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

      \[\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 -2.5 \cdot 10^{+55}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]

Alternative 11: 30.7% accurate, 5.0× speedup?

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

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

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

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

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

    \[\leadsto \color{blue}{d1 \cdot d4} \]
  5. Final simplification28.0%

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