FastMath dist4

Percentage Accurate: 87.5% → 100.0%
Time: 7.5s
Alternatives: 12
Speedup: 1.7×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 12 alternatives:

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

Initial Program: 87.5% accurate, 1.0× speedup?

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

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

Alternative 1: 100.0% accurate, 1.7× speedup?

\[\begin{array}{l} \\ 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.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+89.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.2%

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

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

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

    \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 - d3\right) + \left(d4 - d1\right)\right)} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 2: 69.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d3\right)\\ t_1 := d1 \cdot \left(d2 - d1\right)\\ \mathbf{if}\;d3 \leq -3.8 \cdot 10^{+135}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d3 \leq -6 \cdot 10^{-282}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d3 \leq -9.6 \cdot 10^{-303}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d3 \leq 4.5 \cdot 10^{+74}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d3))) (t_1 (* d1 (- d2 d1))))
   (if (<= d3 -3.8e+135)
     t_0
     (if (<= d3 -6e-282)
       t_1
       (if (<= d3 -9.6e-303) (* d1 d4) (if (<= d3 4.5e+74) t_1 t_0))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double t_1 = d1 * (d2 - d1);
	double tmp;
	if (d3 <= -3.8e+135) {
		tmp = t_0;
	} else if (d3 <= -6e-282) {
		tmp = t_1;
	} else if (d3 <= -9.6e-303) {
		tmp = d1 * d4;
	} else if (d3 <= 4.5e+74) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	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 * (d2 - d3)
    t_1 = d1 * (d2 - d1)
    if (d3 <= (-3.8d+135)) then
        tmp = t_0
    else if (d3 <= (-6d-282)) then
        tmp = t_1
    else if (d3 <= (-9.6d-303)) then
        tmp = d1 * d4
    else if (d3 <= 4.5d+74) then
        tmp = t_1
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double t_1 = d1 * (d2 - d1);
	double tmp;
	if (d3 <= -3.8e+135) {
		tmp = t_0;
	} else if (d3 <= -6e-282) {
		tmp = t_1;
	} else if (d3 <= -9.6e-303) {
		tmp = d1 * d4;
	} else if (d3 <= 4.5e+74) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d3)
	t_1 = d1 * (d2 - d1)
	tmp = 0
	if d3 <= -3.8e+135:
		tmp = t_0
	elif d3 <= -6e-282:
		tmp = t_1
	elif d3 <= -9.6e-303:
		tmp = d1 * d4
	elif d3 <= 4.5e+74:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d3))
	t_1 = Float64(d1 * Float64(d2 - d1))
	tmp = 0.0
	if (d3 <= -3.8e+135)
		tmp = t_0;
	elseif (d3 <= -6e-282)
		tmp = t_1;
	elseif (d3 <= -9.6e-303)
		tmp = Float64(d1 * d4);
	elseif (d3 <= 4.5e+74)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d3);
	t_1 = d1 * (d2 - d1);
	tmp = 0.0;
	if (d3 <= -3.8e+135)
		tmp = t_0;
	elseif (d3 <= -6e-282)
		tmp = t_1;
	elseif (d3 <= -9.6e-303)
		tmp = d1 * d4;
	elseif (d3 <= 4.5e+74)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d3, -3.8e+135], t$95$0, If[LessEqual[d3, -6e-282], t$95$1, If[LessEqual[d3, -9.6e-303], N[(d1 * d4), $MachinePrecision], If[LessEqual[d3, 4.5e+74], t$95$1, t$95$0]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;d3 \leq -6 \cdot 10^{-282}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d3 \leq -9.6 \cdot 10^{-303}:\\
\;\;\;\;d1 \cdot d4\\

\mathbf{elif}\;d3 \leq 4.5 \cdot 10^{+74}:\\
\;\;\;\;t\_1\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -3.8000000000000001e135 or 4.5e74 < d3

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

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

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

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

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

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

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

    if -3.8000000000000001e135 < d3 < -6.0000000000000001e-282 or -9.6000000000000004e-303 < d3 < 4.5e74

    1. Initial program 88.2%

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

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

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

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

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

    if -6.0000000000000001e-282 < d3 < -9.6000000000000004e-303

    1. Initial program 100.0%

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

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

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

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

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

      \[\leadsto \color{blue}{d1 \cdot d4} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 66.1% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := d1 \cdot \left(-d3\right)\\ t_1 := d1 \cdot \left(d2 - d1\right)\\ \mathbf{if}\;d3 \leq -3 \cdot 10^{+136}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d3 \leq -1.8 \cdot 10^{-291}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;d3 \leq -9.6 \cdot 10^{-303}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d3 \leq 3.9 \cdot 10^{+131}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d3))) (t_1 (* d1 (- d2 d1))))
   (if (<= d3 -3e+136)
     t_0
     (if (<= d3 -1.8e-291)
       t_1
       (if (<= d3 -9.6e-303) (* d1 d4) (if (<= d3 3.9e+131) t_1 t_0))))))
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * -d3;
	double t_1 = d1 * (d2 - d1);
	double tmp;
	if (d3 <= -3e+136) {
		tmp = t_0;
	} else if (d3 <= -1.8e-291) {
		tmp = t_1;
	} else if (d3 <= -9.6e-303) {
		tmp = d1 * d4;
	} else if (d3 <= 3.9e+131) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	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 * (d2 - d1)
    if (d3 <= (-3d+136)) then
        tmp = t_0
    else if (d3 <= (-1.8d-291)) then
        tmp = t_1
    else if (d3 <= (-9.6d-303)) then
        tmp = d1 * d4
    else if (d3 <= 3.9d+131) then
        tmp = t_1
    else
        tmp = t_0
    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 * (d2 - d1);
	double tmp;
	if (d3 <= -3e+136) {
		tmp = t_0;
	} else if (d3 <= -1.8e-291) {
		tmp = t_1;
	} else if (d3 <= -9.6e-303) {
		tmp = d1 * d4;
	} else if (d3 <= 3.9e+131) {
		tmp = t_1;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	t_0 = d1 * -d3
	t_1 = d1 * (d2 - d1)
	tmp = 0
	if d3 <= -3e+136:
		tmp = t_0
	elif d3 <= -1.8e-291:
		tmp = t_1
	elif d3 <= -9.6e-303:
		tmp = d1 * d4
	elif d3 <= 3.9e+131:
		tmp = t_1
	else:
		tmp = t_0
	return tmp
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(-d3))
	t_1 = Float64(d1 * Float64(d2 - d1))
	tmp = 0.0
	if (d3 <= -3e+136)
		tmp = t_0;
	elseif (d3 <= -1.8e-291)
		tmp = t_1;
	elseif (d3 <= -9.6e-303)
		tmp = Float64(d1 * d4);
	elseif (d3 <= 3.9e+131)
		tmp = t_1;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * -d3;
	t_1 = d1 * (d2 - d1);
	tmp = 0.0;
	if (d3 <= -3e+136)
		tmp = t_0;
	elseif (d3 <= -1.8e-291)
		tmp = t_1;
	elseif (d3 <= -9.6e-303)
		tmp = d1 * d4;
	elseif (d3 <= 3.9e+131)
		tmp = t_1;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * (-d3)), $MachinePrecision]}, Block[{t$95$1 = N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d3, -3e+136], t$95$0, If[LessEqual[d3, -1.8e-291], t$95$1, If[LessEqual[d3, -9.6e-303], N[(d1 * d4), $MachinePrecision], If[LessEqual[d3, 3.9e+131], t$95$1, t$95$0]]]]]]
\begin{array}{l}

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

\mathbf{elif}\;d3 \leq -1.8 \cdot 10^{-291}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;d3 \leq -9.6 \cdot 10^{-303}:\\
\;\;\;\;d1 \cdot d4\\

\mathbf{elif}\;d3 \leq 3.9 \cdot 10^{+131}:\\
\;\;\;\;t\_1\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -2.99999999999999979e136 or 3.9e131 < d3

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

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

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

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

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

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

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

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

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

    if -2.99999999999999979e136 < d3 < -1.79999999999999983e-291 or -9.6000000000000004e-303 < d3 < 3.9e131

    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--94.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. Add Preprocessing
    5. Taylor expanded in d3 around 0 96.6%

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

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

    if -1.79999999999999983e-291 < d3 < -9.6000000000000004e-303

    1. Initial program 100.0%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -3 \cdot 10^{+136}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{elif}\;d3 \leq -1.8 \cdot 10^{-291}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d3 \leq -9.6 \cdot 10^{-303}:\\ \;\;\;\;d1 \cdot d4\\ \mathbf{elif}\;d3 \leq 3.9 \cdot 10^{+131}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 62.1% accurate, 0.7× speedup?

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

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

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

\mathbf{elif}\;d4 \leq 9.6 \cdot 10^{+148}:\\
\;\;\;\;d1 \cdot \left(d4 - d1\right)\\

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


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

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.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. Add Preprocessing
    5. Taylor expanded in d3 around 0 78.4%

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

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

    if 3.10000000000000016e-133 < d4 < 4.0000000000000003e69

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

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

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

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

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

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

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

    if 4.0000000000000003e69 < d4 < 9.59999999999999979e148

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

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

        \[\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. Add Preprocessing
    5. Taylor expanded in d3 around 0 93.9%

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

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

    if 9.59999999999999979e148 < d4

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

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

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

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

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

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

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

Alternative 5: 39.1% accurate, 0.8× speedup?

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

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

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

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

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


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

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

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

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

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

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

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

    if -7.60000000000000003e-109 < d4 < 9.9999999999999999e-133

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

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

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

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

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

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

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

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

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

    if 9.9999999999999999e-133 < d4 < 2.35000000000000015e64

    1. Initial program 91.8%

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

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

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

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

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

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

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

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

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

    if 2.35000000000000015e64 < d4

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d4 \leq -7.6 \cdot 10^{-109}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d4 \leq 10^{-132}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{elif}\;d4 \leq 2.35 \cdot 10^{+64}:\\ \;\;\;\;d1 \cdot \left(-d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 92.7% accurate, 0.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;d3 \leq -3 \cdot 10^{+134} \lor \neg \left(d3 \leq 2.1 \cdot 10^{+68}\right):\\
\;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if d3 < -2.99999999999999997e134 or 2.10000000000000001e68 < d3

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

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

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

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

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

    if -2.99999999999999997e134 < d3 < 2.10000000000000001e68

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -3 \cdot 10^{+134} \lor \neg \left(d3 \leq 2.1 \cdot 10^{+68}\right):\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(\left(d2 + d4\right) - d1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 89.0% accurate, 0.9× speedup?

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

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

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

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


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

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

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

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

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

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

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

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

    if -5.00000000000000008e140 < d3 < 7.2000000000000001e126

    1. Initial program 88.9%

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.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. Add Preprocessing
    5. Taylor expanded in d3 around 0 96.7%

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

    if 7.2000000000000001e126 < d3

    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. *-commutative94.3%

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

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

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

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

Alternative 8: 61.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d4 \leq 3.1 \cdot 10^{-133}:\\ \;\;\;\;d1 \cdot \left(d2 - d1\right)\\ \mathbf{elif}\;d4 \leq 3.6 \cdot 10^{+69}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d4 3.1e-133)
   (* d1 (- d2 d1))
   (if (<= d4 3.6e+69) (* d1 (- d2 d3)) (* d1 (- d4 d1)))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d4 <= 3.1e-133) {
		tmp = d1 * (d2 - d1);
	} else if (d4 <= 3.6e+69) {
		tmp = d1 * (d2 - d3);
	} 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 (d4 <= 3.1d-133) then
        tmp = d1 * (d2 - d1)
    else if (d4 <= 3.6d+69) then
        tmp = d1 * (d2 - d3)
    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 (d4 <= 3.1e-133) {
		tmp = d1 * (d2 - d1);
	} else if (d4 <= 3.6e+69) {
		tmp = d1 * (d2 - d3);
	} else {
		tmp = d1 * (d4 - d1);
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if d4 <= 3.1e-133:
		tmp = d1 * (d2 - d1)
	elif d4 <= 3.6e+69:
		tmp = d1 * (d2 - d3)
	else:
		tmp = d1 * (d4 - d1)
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d4 <= 3.1e-133)
		tmp = Float64(d1 * Float64(d2 - d1));
	elseif (d4 <= 3.6e+69)
		tmp = Float64(d1 * Float64(d2 - d3));
	else
		tmp = Float64(d1 * Float64(d4 - d1));
	end
	return tmp
end
function tmp_2 = code(d1, d2, d3, d4)
	tmp = 0.0;
	if (d4 <= 3.1e-133)
		tmp = d1 * (d2 - d1);
	elseif (d4 <= 3.6e+69)
		tmp = d1 * (d2 - d3);
	else
		tmp = d1 * (d4 - d1);
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[LessEqual[d4, 3.1e-133], N[(d1 * N[(d2 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d4, 3.6e+69], N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

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

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


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

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

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

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      3. distribute-rgt-out--94.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. Add Preprocessing
    5. Taylor expanded in d3 around 0 78.4%

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

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

    if 3.10000000000000016e-133 < d4 < 3.6000000000000003e69

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

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

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

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

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

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

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

    if 3.6000000000000003e69 < d4

    1. Initial program 87.5%

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

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

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

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

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

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

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

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

Alternative 9: 38.9% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;d2 \leq -8.2 \cdot 10^{+100}:\\ \;\;\;\;d1 \cdot d2\\ \mathbf{elif}\;d2 \leq 5.1 \cdot 10^{-84}:\\ \;\;\;\;d1 \cdot \left(-d1\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot d4\\ \end{array} \end{array} \]
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= d2 -8.2e+100) (* d1 d2) (if (<= d2 5.1e-84) (* d1 (- d1)) (* d1 d4))))
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (d2 <= -8.2e+100) {
		tmp = d1 * d2;
	} else if (d2 <= 5.1e-84) {
		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 <= (-8.2d+100)) then
        tmp = d1 * d2
    else if (d2 <= 5.1d-84) 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 <= -8.2e+100) {
		tmp = d1 * d2;
	} else if (d2 <= 5.1e-84) {
		tmp = d1 * -d1;
	} else {
		tmp = d1 * d4;
	}
	return tmp;
}
def code(d1, d2, d3, d4):
	tmp = 0
	if d2 <= -8.2e+100:
		tmp = d1 * d2
	elif d2 <= 5.1e-84:
		tmp = d1 * -d1
	else:
		tmp = d1 * d4
	return tmp
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (d2 <= -8.2e+100)
		tmp = Float64(d1 * d2);
	elseif (d2 <= 5.1e-84)
		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 <= -8.2e+100)
		tmp = d1 * d2;
	elseif (d2 <= 5.1e-84)
		tmp = d1 * -d1;
	else
		tmp = d1 * d4;
	end
	tmp_2 = tmp;
end
code[d1_, d2_, d3_, d4_] := If[LessEqual[d2, -8.2e+100], N[(d1 * d2), $MachinePrecision], If[LessEqual[d2, 5.1e-84], N[(d1 * (-d1)), $MachinePrecision], N[(d1 * d4), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;d2 \leq 5.1 \cdot 10^{-84}:\\
\;\;\;\;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.2000000000000006e100

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

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

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

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

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

    if -8.2000000000000006e100 < d2 < 5.0999999999999996e-84

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

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

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

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

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

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

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

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

    if 5.0999999999999996e-84 < d2

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

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

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

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

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

      \[\leadsto \color{blue}{d1 \cdot d4} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 10: 39.3% accurate, 1.9× speedup?

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

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

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


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

    1. Initial program 89.4%

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

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

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

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

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

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

    if 1.26000000000000005e69 < d4

    1. Initial program 87.5%

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

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

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

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

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

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

Alternative 11: 31.0% accurate, 5.0× speedup?

\[\begin{array}{l} \\ d1 \cdot d2 \end{array} \]
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 d2))
double code(double d1, double d2, double d3, double d4) {
	return d1 * d2;
}
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
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * d2;
}
def code(d1, d2, d3, d4):
	return d1 * d2
function code(d1, d2, d3, d4)
	return Float64(d1 * d2)
end
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * d2;
end
code[d1_, d2_, d3_, d4_] := N[(d1 * d2), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot d2
\end{array}
Derivation
  1. Initial program 89.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+89.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.2%

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

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

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

    \[\leadsto \color{blue}{d1 \cdot d2} \]
  6. Add Preprocessing

Alternative 12: 7.9% accurate, 5.0× speedup?

\[\begin{array}{l} \\ d1 \cdot d1 \end{array} \]
(FPCore (d1 d2 d3 d4) :precision binary64 (* d1 d1))
double code(double d1, double d2, double d3, double d4) {
	return 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 * d1
end function
public static double code(double d1, double d2, double d3, double d4) {
	return d1 * d1;
}
def code(d1, d2, d3, d4):
	return d1 * d1
function code(d1, d2, d3, d4)
	return Float64(d1 * d1)
end
function tmp = code(d1, d2, d3, d4)
	tmp = d1 * d1;
end
code[d1_, d2_, d3_, d4_] := N[(d1 * d1), $MachinePrecision]
\begin{array}{l}

\\
d1 \cdot d1
\end{array}
Derivation
  1. Initial program 89.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+89.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.2%

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

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

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

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

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

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

    \[\leadsto d1 \cdot \color{blue}{\left(-d1\right)} \]
  8. Step-by-step derivation
    1. neg-sub034.3%

      \[\leadsto d1 \cdot \color{blue}{\left(0 - d1\right)} \]
    2. sub-neg34.3%

      \[\leadsto d1 \cdot \color{blue}{\left(0 + \left(-d1\right)\right)} \]
    3. add-sqr-sqrt17.2%

      \[\leadsto d1 \cdot \left(0 + \color{blue}{\sqrt{-d1} \cdot \sqrt{-d1}}\right) \]
    4. sqrt-unprod20.4%

      \[\leadsto d1 \cdot \left(0 + \color{blue}{\sqrt{\left(-d1\right) \cdot \left(-d1\right)}}\right) \]
    5. sqr-neg20.4%

      \[\leadsto d1 \cdot \left(0 + \sqrt{\color{blue}{d1 \cdot d1}}\right) \]
    6. sqrt-unprod3.1%

      \[\leadsto d1 \cdot \left(0 + \color{blue}{\sqrt{d1} \cdot \sqrt{d1}}\right) \]
    7. add-sqr-sqrt5.8%

      \[\leadsto d1 \cdot \left(0 + \color{blue}{d1}\right) \]
  9. Applied egg-rr5.8%

    \[\leadsto d1 \cdot \color{blue}{\left(0 + d1\right)} \]
  10. Step-by-step derivation
    1. +-lft-identity5.8%

      \[\leadsto d1 \cdot \color{blue}{d1} \]
  11. Simplified5.8%

    \[\leadsto d1 \cdot \color{blue}{d1} \]
  12. Add Preprocessing

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 2024111 
(FPCore (d1 d2 d3 d4)
  :name "FastMath dist4"
  :precision binary64

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

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