FastMath dist4

Percentage Accurate: 87.7% → 98.5%
Time: 2.0min
Alternatives: 16
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 16 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.7% 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: 98.5% accurate, 0.6× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} \mathbf{if}\;\left(\left(d2 \cdot d1 - d1 \cdot d3\right) + d1 \cdot d4\right) - d1 \cdot d1 \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(d2 - d3, d1, d1 \cdot \left(d4 - d1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (if (<= (- (+ (- (* d2 d1) (* d1 d3)) (* d1 d4)) (* d1 d1)) INFINITY)
   (fma (- d2 d3) d1 (* d1 (- d4 d1)))
   (* d1 (- d2 (+ d1 d3)))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double tmp;
	if (((((d2 * d1) - (d1 * d3)) + (d1 * d4)) - (d1 * d1)) <= ((double) INFINITY)) {
		tmp = fma((d2 - d3), d1, (d1 * (d4 - d1)));
	} else {
		tmp = d1 * (d2 - (d1 + d3));
	}
	return tmp;
}
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	tmp = 0.0
	if (Float64(Float64(Float64(Float64(d2 * d1) - Float64(d1 * d3)) + Float64(d1 * d4)) - Float64(d1 * d1)) <= Inf)
		tmp = fma(Float64(d2 - d3), d1, Float64(d1 * Float64(d4 - d1)));
	else
		tmp = Float64(d1 * Float64(d2 - Float64(d1 + d3)));
	end
	return tmp
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := If[LessEqual[N[(N[(N[(N[(d2 * d1), $MachinePrecision] - N[(d1 * d3), $MachinePrecision]), $MachinePrecision] + N[(d1 * d4), $MachinePrecision]), $MachinePrecision] - N[(d1 * d1), $MachinePrecision]), $MachinePrecision], Infinity], N[(N[(d2 - d3), $MachinePrecision] * d1 + N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(d1 * N[(d2 - N[(d1 + d3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(\left(d2 \cdot d1 - d1 \cdot d3\right) + d1 \cdot d4\right) - d1 \cdot d1 \leq \infty:\\
\;\;\;\;\mathsf{fma}\left(d2 - d3, d1, d1 \cdot \left(d4 - d1\right)\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (+.f64 (-.f64 (*.f64 d1 d2) (*.f64 d1 d3)) (*.f64 d4 d1)) (*.f64 d1 d1)) < +inf.0

    1. Initial program 100.0%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \left(\left(\color{blue}{d1 \cdot d2} - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      2. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      3. lift--.f64N/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right)} + d4 \cdot d1\right) - d1 \cdot d1 \]
      4. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + \color{blue}{d4 \cdot d1}\right) - d1 \cdot d1 \]
      5. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{d1 \cdot d1} \]
      6. associate--l+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      7. lift--.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      8. lift-*.f64N/A

        \[\leadsto \left(\color{blue}{d1 \cdot d2} - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      9. lift-*.f64N/A

        \[\leadsto \left(d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      10. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      11. *-commutativeN/A

        \[\leadsto \color{blue}{\left(d2 - d3\right) \cdot d1} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      12. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(d2 - d3, d1, d4 \cdot d1 - d1 \cdot d1\right)} \]
      13. lower--.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{d2 - d3}, d1, d4 \cdot d1 - d1 \cdot d1\right) \]
      14. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \color{blue}{d4 \cdot d1} - d1 \cdot d1\right) \]
      15. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right) \]
      16. distribute-rgt-out--N/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \color{blue}{d1 \cdot \left(d4 - d1\right)}\right) \]
      17. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, \color{blue}{d1 \cdot \left(d4 - d1\right)}\right) \]
      18. lower--.f64100.0

        \[\leadsto \mathsf{fma}\left(d2 - d3, d1, d1 \cdot \color{blue}{\left(d4 - d1\right)}\right) \]
    4. Applied egg-rr100.0%

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

    if +inf.0 < (-.f64 (+.f64 (-.f64 (*.f64 d1 d2) (*.f64 d1 d3)) (*.f64 d4 d1)) (*.f64 d1 d1))

    1. Initial program 0.0%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \left(\left(\color{blue}{d1 \cdot d2} - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      2. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      3. lift--.f64N/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right)} + d4 \cdot d1\right) - d1 \cdot d1 \]
      4. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + \color{blue}{d4 \cdot d1}\right) - d1 \cdot d1 \]
      5. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{d1 \cdot d1} \]
      6. associate--l+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      7. lift--.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      8. sub-negN/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1 \cdot d3\right)\right)\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      9. associate-+l+N/A

        \[\leadsto \color{blue}{d1 \cdot d2 + \left(\left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right)} \]
      10. lift-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot d2} + \left(\left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      11. *-commutativeN/A

        \[\leadsto \color{blue}{d2 \cdot d1} + \left(\left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      12. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(d2, d1, \left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right)} \]
      13. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \left(\mathsf{neg}\left(\color{blue}{d1 \cdot d3}\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      14. distribute-rgt-neg-inN/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \color{blue}{d1 \cdot \left(\mathsf{neg}\left(d3\right)\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      15. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right)\right) \]
      16. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right)\right) \]
      17. distribute-rgt-out--N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)}\right) \]
      18. distribute-lft-outN/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \color{blue}{d1 \cdot \left(\left(\mathsf{neg}\left(d3\right)\right) + \left(d4 - d1\right)\right)}\right) \]
      19. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \color{blue}{d1 \cdot \left(\left(\mathsf{neg}\left(d3\right)\right) + \left(d4 - d1\right)\right)}\right) \]
      20. lower-+.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \color{blue}{\left(\left(\mathsf{neg}\left(d3\right)\right) + \left(d4 - d1\right)\right)}\right) \]
    4. Applied egg-rr87.0%

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

      \[\leadsto \color{blue}{-1 \cdot \left(d1 \cdot \left(d1 + d3\right)\right) + d1 \cdot d2} \]
    6. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{d1 \cdot d2 + -1 \cdot \left(d1 \cdot \left(d1 + d3\right)\right)} \]
      2. mul-1-negN/A

        \[\leadsto d1 \cdot d2 + \color{blue}{\left(\mathsf{neg}\left(d1 \cdot \left(d1 + d3\right)\right)\right)} \]
      3. unsub-negN/A

        \[\leadsto \color{blue}{d1 \cdot d2 - d1 \cdot \left(d1 + d3\right)} \]
      4. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - \left(d1 + d3\right)\right)} \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - \left(d1 + d3\right)\right)} \]
      6. lower--.f64N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d2 - \left(d1 + d3\right)\right)} \]
      7. lower-+.f6491.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\left(d2 \cdot d1 - d1 \cdot d3\right) + d1 \cdot d4\right) - d1 \cdot d1 \leq \infty:\\ \;\;\;\;\mathsf{fma}\left(d2 - d3, d1, d1 \cdot \left(d4 - d1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - \left(d1 + d3\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 71.6% accurate, 1.1× speedup?

\[\begin{array}{l} [d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\ \\ \begin{array}{l} t_0 := d1 \cdot \left(d2 - d3\right)\\ \mathbf{if}\;d3 \leq -3 \cdot 10^{+79}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;d3 \leq -1.4 \cdot 10^{+48}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.25 \cdot 10^{+95}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
(FPCore (d1 d2 d3 d4)
 :precision binary64
 (let* ((t_0 (* d1 (- d2 d3))))
   (if (<= d3 -3e+79)
     t_0
     (if (<= d3 -1.4e+48)
       (* d1 (- d4 d1))
       (if (<= d3 1.25e+95) (* d1 (+ d2 d4)) t_0)))))
assert(d1 < d2 && d2 < d3 && d3 < d4);
double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d3 <= -3e+79) {
		tmp = t_0;
	} else if (d3 <= -1.4e+48) {
		tmp = d1 * (d4 - d1);
	} else if (d3 <= 1.25e+95) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = t_0;
	}
	return tmp;
}
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
real(8) function code(d1, d2, d3, d4)
    real(8), intent (in) :: d1
    real(8), intent (in) :: d2
    real(8), intent (in) :: d3
    real(8), intent (in) :: d4
    real(8) :: t_0
    real(8) :: tmp
    t_0 = d1 * (d2 - d3)
    if (d3 <= (-3d+79)) then
        tmp = t_0
    else if (d3 <= (-1.4d+48)) then
        tmp = d1 * (d4 - d1)
    else if (d3 <= 1.25d+95) then
        tmp = d1 * (d2 + d4)
    else
        tmp = t_0
    end if
    code = tmp
end function
assert d1 < d2 && d2 < d3 && d3 < d4;
public static double code(double d1, double d2, double d3, double d4) {
	double t_0 = d1 * (d2 - d3);
	double tmp;
	if (d3 <= -3e+79) {
		tmp = t_0;
	} else if (d3 <= -1.4e+48) {
		tmp = d1 * (d4 - d1);
	} else if (d3 <= 1.25e+95) {
		tmp = d1 * (d2 + d4);
	} else {
		tmp = t_0;
	}
	return tmp;
}
[d1, d2, d3, d4] = sort([d1, d2, d3, d4])
def code(d1, d2, d3, d4):
	t_0 = d1 * (d2 - d3)
	tmp = 0
	if d3 <= -3e+79:
		tmp = t_0
	elif d3 <= -1.4e+48:
		tmp = d1 * (d4 - d1)
	elif d3 <= 1.25e+95:
		tmp = d1 * (d2 + d4)
	else:
		tmp = t_0
	return tmp
d1, d2, d3, d4 = sort([d1, d2, d3, d4])
function code(d1, d2, d3, d4)
	t_0 = Float64(d1 * Float64(d2 - d3))
	tmp = 0.0
	if (d3 <= -3e+79)
		tmp = t_0;
	elseif (d3 <= -1.4e+48)
		tmp = Float64(d1 * Float64(d4 - d1));
	elseif (d3 <= 1.25e+95)
		tmp = Float64(d1 * Float64(d2 + d4));
	else
		tmp = t_0;
	end
	return tmp
end
d1, d2, d3, d4 = num2cell(sort([d1, d2, d3, d4])){:}
function tmp_2 = code(d1, d2, d3, d4)
	t_0 = d1 * (d2 - d3);
	tmp = 0.0;
	if (d3 <= -3e+79)
		tmp = t_0;
	elseif (d3 <= -1.4e+48)
		tmp = d1 * (d4 - d1);
	elseif (d3 <= 1.25e+95)
		tmp = d1 * (d2 + d4);
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
NOTE: d1, d2, d3, and d4 should be sorted in increasing order before calling this function.
code[d1_, d2_, d3_, d4_] := Block[{t$95$0 = N[(d1 * N[(d2 - d3), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d3, -3e+79], t$95$0, If[LessEqual[d3, -1.4e+48], N[(d1 * N[(d4 - d1), $MachinePrecision]), $MachinePrecision], If[LessEqual[d3, 1.25e+95], N[(d1 * N[(d2 + d4), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
[d1, d2, d3, d4] = \mathsf{sort}([d1, d2, d3, d4])\\
\\
\begin{array}{l}
t_0 := d1 \cdot \left(d2 - d3\right)\\
\mathbf{if}\;d3 \leq -3 \cdot 10^{+79}:\\
\;\;\;\;t\_0\\

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if d3 < -2.99999999999999974e79 or 1.25000000000000006e95 < d3

    1. Initial program 82.4%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \left(\left(\color{blue}{d1 \cdot d2} - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      2. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - \color{blue}{d1 \cdot d3}\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
      3. lift--.f64N/A

        \[\leadsto \left(\color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right)} + d4 \cdot d1\right) - d1 \cdot d1 \]
      4. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + \color{blue}{d4 \cdot d1}\right) - d1 \cdot d1 \]
      5. lift-*.f64N/A

        \[\leadsto \left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - \color{blue}{d1 \cdot d1} \]
      6. associate--l+N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)} \]
      7. lift--.f64N/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 - d1 \cdot d3\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      8. sub-negN/A

        \[\leadsto \color{blue}{\left(d1 \cdot d2 + \left(\mathsf{neg}\left(d1 \cdot d3\right)\right)\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right) \]
      9. associate-+l+N/A

        \[\leadsto \color{blue}{d1 \cdot d2 + \left(\left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right)} \]
      10. lift-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot d2} + \left(\left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      11. *-commutativeN/A

        \[\leadsto \color{blue}{d2 \cdot d1} + \left(\left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      12. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(d2, d1, \left(\mathsf{neg}\left(d1 \cdot d3\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right)} \]
      13. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \left(\mathsf{neg}\left(\color{blue}{d1 \cdot d3}\right)\right) + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      14. distribute-rgt-neg-inN/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \color{blue}{d1 \cdot \left(\mathsf{neg}\left(d3\right)\right)} + \left(d4 \cdot d1 - d1 \cdot d1\right)\right) \]
      15. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \left(\color{blue}{d4 \cdot d1} - d1 \cdot d1\right)\right) \]
      16. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \left(d4 \cdot d1 - \color{blue}{d1 \cdot d1}\right)\right) \]
      17. distribute-rgt-out--N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \left(\mathsf{neg}\left(d3\right)\right) + \color{blue}{d1 \cdot \left(d4 - d1\right)}\right) \]
      18. distribute-lft-outN/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \color{blue}{d1 \cdot \left(\left(\mathsf{neg}\left(d3\right)\right) + \left(d4 - d1\right)\right)}\right) \]
      19. lower-*.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, \color{blue}{d1 \cdot \left(\left(\mathsf{neg}\left(d3\right)\right) + \left(d4 - d1\right)\right)}\right) \]
      20. lower-+.f64N/A

        \[\leadsto \mathsf{fma}\left(d2, d1, d1 \cdot \color{blue}{\left(\left(\mathsf{neg}\left(d3\right)\right) + \left(d4 - d1\right)\right)}\right) \]
    4. Applied egg-rr98.4%

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

      \[\leadsto \color{blue}{-1 \cdot \left(d1 \cdot \left(d1 + d3\right)\right) + d1 \cdot d2} \]
    6. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{d1 \cdot d2 + -1 \cdot \left(d1 \cdot \left(d1 + d3\right)\right)} \]
      2. mul-1-negN/A

        \[\leadsto d1 \cdot d2 + \color{blue}{\left(\mathsf{neg}\left(d1 \cdot \left(d1 + d3\right)\right)\right)} \]
      3. unsub-negN/A

        \[\leadsto \color{blue}{d1 \cdot d2 - d1 \cdot \left(d1 + d3\right)} \]
      4. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - \left(d1 + d3\right)\right)} \]
      5. lower-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 - \left(d1 + d3\right)\right)} \]
      6. lower--.f64N/A

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

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 - d3\right)} \]
    9. Step-by-step derivation
      1. +-rgt-identityN/A

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d2 - d3\right) + 0\right)} \]
      2. +-commutativeN/A

        \[\leadsto d1 \cdot \color{blue}{\left(0 + \left(d2 - d3\right)\right)} \]
      3. *-rgt-identityN/A

        \[\leadsto d1 \cdot \left(0 + \color{blue}{\left(d2 - d3\right) \cdot 1}\right) \]
      4. cancel-sign-subN/A

        \[\leadsto d1 \cdot \color{blue}{\left(0 - \left(\mathsf{neg}\left(\left(d2 - d3\right)\right)\right) \cdot 1\right)} \]
      5. mul-1-negN/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\left(-1 \cdot \left(d2 - d3\right)\right)} \cdot 1\right) \]
      6. *-inversesN/A

        \[\leadsto d1 \cdot \left(0 - \left(-1 \cdot \left(d2 - d3\right)\right) \cdot \color{blue}{\frac{d1}{d1}}\right) \]
      7. associate-/l*N/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\frac{\left(-1 \cdot \left(d2 - d3\right)\right) \cdot d1}{d1}}\right) \]
      8. associate-*l/N/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\frac{-1 \cdot \left(d2 - d3\right)}{d1} \cdot d1}\right) \]
      9. associate-*r/N/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\left(-1 \cdot \frac{d2 - d3}{d1}\right)} \cdot d1\right) \]
      10. neg-sub0N/A

        \[\leadsto d1 \cdot \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \frac{d2 - d3}{d1}\right) \cdot d1\right)\right)} \]
      11. lower-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(\mathsf{neg}\left(\left(-1 \cdot \frac{d2 - d3}{d1}\right) \cdot d1\right)\right)} \]
      12. neg-sub0N/A

        \[\leadsto d1 \cdot \color{blue}{\left(0 - \left(-1 \cdot \frac{d2 - d3}{d1}\right) \cdot d1\right)} \]
      13. associate-*r/N/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\frac{-1 \cdot \left(d2 - d3\right)}{d1}} \cdot d1\right) \]
      14. associate-*l/N/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\frac{\left(-1 \cdot \left(d2 - d3\right)\right) \cdot d1}{d1}}\right) \]
      15. associate-/l*N/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\left(-1 \cdot \left(d2 - d3\right)\right) \cdot \frac{d1}{d1}}\right) \]
      16. *-inversesN/A

        \[\leadsto d1 \cdot \left(0 - \left(-1 \cdot \left(d2 - d3\right)\right) \cdot \color{blue}{1}\right) \]
      17. mul-1-negN/A

        \[\leadsto d1 \cdot \left(0 - \color{blue}{\left(\mathsf{neg}\left(\left(d2 - d3\right)\right)\right)} \cdot 1\right) \]
      18. cancel-sign-subN/A

        \[\leadsto d1 \cdot \color{blue}{\left(0 + \left(d2 - d3\right) \cdot 1\right)} \]
      19. *-rgt-identityN/A

        \[\leadsto d1 \cdot \left(0 + \color{blue}{\left(d2 - d3\right)}\right) \]
      20. +-commutativeN/A

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d2 - d3\right) + 0\right)} \]
      21. +-rgt-identityN/A

        \[\leadsto d1 \cdot \color{blue}{\left(d2 - d3\right)} \]
      22. lower--.f6479.5

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

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

    if -2.99999999999999974e79 < d3 < -1.40000000000000006e48

    1. Initial program 90.5%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Taylor expanded in d3 around 0

      \[\leadsto \color{blue}{\left(d1 \cdot d2 + d1 \cdot d4\right) - {d1}^{2}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} - {d1}^{2} \]
      2. unpow2N/A

        \[\leadsto d1 \cdot \left(d2 + d4\right) - \color{blue}{d1 \cdot d1} \]
      3. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d1\right)} \]
      4. unsub-negN/A

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d2 + d4\right) + \left(\mathsf{neg}\left(d1\right)\right)\right)} \]
      5. mul-1-negN/A

        \[\leadsto d1 \cdot \left(\left(d2 + d4\right) + \color{blue}{-1 \cdot d1}\right) \]
      6. associate-+r+N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 + -1 \cdot d1\right)\right)} \]
      7. lower-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + \left(d4 + -1 \cdot d1\right)\right)} \]
      8. lower-+.f64N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 + -1 \cdot d1\right)\right)} \]
      9. mul-1-negN/A

        \[\leadsto d1 \cdot \left(d2 + \left(d4 + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)}\right)\right) \]
      10. unsub-negN/A

        \[\leadsto d1 \cdot \left(d2 + \color{blue}{\left(d4 - d1\right)}\right) \]
      11. lower--.f6475.9

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d4 - d1\right)} \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

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

        \[\leadsto d1 \cdot \color{blue}{\left(d4 - d1\right)} \]
    8. Simplified53.2%

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

    if -1.40000000000000006e48 < d3 < 1.25000000000000006e95

    1. Initial program 90.8%

      \[\left(\left(d1 \cdot d2 - d1 \cdot d3\right) + d4 \cdot d1\right) - d1 \cdot d1 \]
    2. Add Preprocessing
    3. Taylor expanded in d3 around 0

      \[\leadsto \color{blue}{\left(d1 \cdot d2 + d1 \cdot d4\right) - {d1}^{2}} \]
    4. Step-by-step derivation
      1. distribute-lft-outN/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} - {d1}^{2} \]
      2. unpow2N/A

        \[\leadsto d1 \cdot \left(d2 + d4\right) - \color{blue}{d1 \cdot d1} \]
      3. distribute-lft-out--N/A

        \[\leadsto \color{blue}{d1 \cdot \left(\left(d2 + d4\right) - d1\right)} \]
      4. unsub-negN/A

        \[\leadsto d1 \cdot \color{blue}{\left(\left(d2 + d4\right) + \left(\mathsf{neg}\left(d1\right)\right)\right)} \]
      5. mul-1-negN/A

        \[\leadsto d1 \cdot \left(\left(d2 + d4\right) + \color{blue}{-1 \cdot d1}\right) \]
      6. associate-+r+N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 + -1 \cdot d1\right)\right)} \]
      7. lower-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + \left(d4 + -1 \cdot d1\right)\right)} \]
      8. lower-+.f64N/A

        \[\leadsto d1 \cdot \color{blue}{\left(d2 + \left(d4 + -1 \cdot d1\right)\right)} \]
      9. mul-1-negN/A

        \[\leadsto d1 \cdot \left(d2 + \left(d4 + \color{blue}{\left(\mathsf{neg}\left(d1\right)\right)}\right)\right) \]
      10. unsub-negN/A

        \[\leadsto d1 \cdot \left(d2 + \color{blue}{\left(d4 - d1\right)}\right) \]
      11. lower--.f6494.5

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

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

      \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{d1 \cdot \left(d2 + d4\right)} \]
      2. +-commutativeN/A

        \[\leadsto d1 \cdot \color{blue}{\left(d4 + d2\right)} \]
      3. lower-+.f6467.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d3 \leq -3 \cdot 10^{+79}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \mathbf{elif}\;d3 \leq -1.4 \cdot 10^{+48}:\\ \;\;\;\;d1 \cdot \left(d4 - d1\right)\\ \mathbf{elif}\;d3 \leq 1.25 \cdot 10^{+95}:\\ \;\;\;\;d1 \cdot \left(d2 + d4\right)\\ \mathbf{else}:\\ \;\;\;\;d1 \cdot \left(d2 - d3\right)\\ \end{array} \]
  5. Add Preprocessing

Developer Target 1: 100.0% accurate, 2.0× 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 2024218 
(FPCore (d1 d2 d3 d4)
  :name "FastMath dist4"
  :precision binary64

  :alt
  (! :herbie-platform default (* d1 (- (+ (- d2 d3) d4) d1)))

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