Radioactive exchange between two surfaces

Percentage Accurate: 86.4% → 95.3%
Time: 3.1s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ {x}^{4} - {y}^{4} \end{array} \]
(FPCore (x y) :precision binary64 (- (pow x 4.0) (pow y 4.0)))
double code(double x, double y) {
	return pow(x, 4.0) - pow(y, 4.0);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x ** 4.0d0) - (y ** 4.0d0)
end function
public static double code(double x, double y) {
	return Math.pow(x, 4.0) - Math.pow(y, 4.0);
}
def code(x, y):
	return math.pow(x, 4.0) - math.pow(y, 4.0)
function code(x, y)
	return Float64((x ^ 4.0) - (y ^ 4.0))
end
function tmp = code(x, y)
	tmp = (x ^ 4.0) - (y ^ 4.0);
end
code[x_, y_] := N[(N[Power[x, 4.0], $MachinePrecision] - N[Power[y, 4.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
{x}^{4} - {y}^{4}
\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 4 alternatives:

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

Initial Program: 86.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ {x}^{4} - {y}^{4} \end{array} \]
(FPCore (x y) :precision binary64 (- (pow x 4.0) (pow y 4.0)))
double code(double x, double y) {
	return pow(x, 4.0) - pow(y, 4.0);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x ** 4.0d0) - (y ** 4.0d0)
end function
public static double code(double x, double y) {
	return Math.pow(x, 4.0) - Math.pow(y, 4.0);
}
def code(x, y):
	return math.pow(x, 4.0) - math.pow(y, 4.0)
function code(x, y)
	return Float64((x ^ 4.0) - (y ^ 4.0))
end
function tmp = code(x, y)
	tmp = (x ^ 4.0) - (y ^ 4.0);
end
code[x_, y_] := N[(N[Power[x, 4.0], $MachinePrecision] - N[Power[y, 4.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
{x}^{4} - {y}^{4}
\end{array}

Alternative 1: 95.3% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 2.1 \cdot 10^{+127}:\\ \;\;\;\;\mathsf{fma}\left({x\_m}^{2}, {x\_m}^{2}, -{y}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;{x\_m}^{4}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y)
 :precision binary64
 (if (<= x_m 2.1e+127)
   (fma (pow x_m 2.0) (pow x_m 2.0) (- (pow y 4.0)))
   (pow x_m 4.0)))
x_m = fabs(x);
double code(double x_m, double y) {
	double tmp;
	if (x_m <= 2.1e+127) {
		tmp = fma(pow(x_m, 2.0), pow(x_m, 2.0), -pow(y, 4.0));
	} else {
		tmp = pow(x_m, 4.0);
	}
	return tmp;
}
x_m = abs(x)
function code(x_m, y)
	tmp = 0.0
	if (x_m <= 2.1e+127)
		tmp = fma((x_m ^ 2.0), (x_m ^ 2.0), Float64(-(y ^ 4.0)));
	else
		tmp = x_m ^ 4.0;
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := If[LessEqual[x$95$m, 2.1e+127], N[(N[Power[x$95$m, 2.0], $MachinePrecision] * N[Power[x$95$m, 2.0], $MachinePrecision] + (-N[Power[y, 4.0], $MachinePrecision])), $MachinePrecision], N[Power[x$95$m, 4.0], $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 2.1 \cdot 10^{+127}:\\
\;\;\;\;\mathsf{fma}\left({x\_m}^{2}, {x\_m}^{2}, -{y}^{4}\right)\\

\mathbf{else}:\\
\;\;\;\;{x\_m}^{4}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 2.09999999999999992e127

    1. Initial program 91.7%

      \[{x}^{4} - {y}^{4} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. sqr-pow91.6%

        \[\leadsto \color{blue}{{x}^{\left(\frac{4}{2}\right)} \cdot {x}^{\left(\frac{4}{2}\right)}} - {y}^{4} \]
      2. fma-neg95.8%

        \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{\left(\frac{4}{2}\right)}, {x}^{\left(\frac{4}{2}\right)}, -{y}^{4}\right)} \]
      3. metadata-eval95.8%

        \[\leadsto \mathsf{fma}\left({x}^{\color{blue}{2}}, {x}^{\left(\frac{4}{2}\right)}, -{y}^{4}\right) \]
      4. metadata-eval95.8%

        \[\leadsto \mathsf{fma}\left({x}^{2}, {x}^{\color{blue}{2}}, -{y}^{4}\right) \]
    4. Applied egg-rr95.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, {x}^{2}, -{y}^{4}\right)} \]

    if 2.09999999999999992e127 < x

    1. Initial program 65.0%

      \[{x}^{4} - {y}^{4} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 87.5%

      \[\leadsto \color{blue}{{x}^{4}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification94.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 2.1 \cdot 10^{+127}:\\ \;\;\;\;\mathsf{fma}\left({x}^{2}, {x}^{2}, -{y}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;{x}^{4}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 93.1% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 1.15 \cdot 10^{+77}:\\ \;\;\;\;{x\_m}^{4} - {y}^{4}\\ \mathbf{else}:\\ \;\;\;\;{x\_m}^{4}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y)
 :precision binary64
 (if (<= x_m 1.15e+77) (- (pow x_m 4.0) (pow y 4.0)) (pow x_m 4.0)))
x_m = fabs(x);
double code(double x_m, double y) {
	double tmp;
	if (x_m <= 1.15e+77) {
		tmp = pow(x_m, 4.0) - pow(y, 4.0);
	} else {
		tmp = pow(x_m, 4.0);
	}
	return tmp;
}
x_m = abs(x)
real(8) function code(x_m, y)
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x_m <= 1.15d+77) then
        tmp = (x_m ** 4.0d0) - (y ** 4.0d0)
    else
        tmp = x_m ** 4.0d0
    end if
    code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
	double tmp;
	if (x_m <= 1.15e+77) {
		tmp = Math.pow(x_m, 4.0) - Math.pow(y, 4.0);
	} else {
		tmp = Math.pow(x_m, 4.0);
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m, y):
	tmp = 0
	if x_m <= 1.15e+77:
		tmp = math.pow(x_m, 4.0) - math.pow(y, 4.0)
	else:
		tmp = math.pow(x_m, 4.0)
	return tmp
x_m = abs(x)
function code(x_m, y)
	tmp = 0.0
	if (x_m <= 1.15e+77)
		tmp = Float64((x_m ^ 4.0) - (y ^ 4.0));
	else
		tmp = x_m ^ 4.0;
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m, y)
	tmp = 0.0;
	if (x_m <= 1.15e+77)
		tmp = (x_m ^ 4.0) - (y ^ 4.0);
	else
		tmp = x_m ^ 4.0;
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := If[LessEqual[x$95$m, 1.15e+77], N[(N[Power[x$95$m, 4.0], $MachinePrecision] - N[Power[y, 4.0], $MachinePrecision]), $MachinePrecision], N[Power[x$95$m, 4.0], $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x\_m \leq 1.15 \cdot 10^{+77}:\\
\;\;\;\;{x\_m}^{4} - {y}^{4}\\

\mathbf{else}:\\
\;\;\;\;{x\_m}^{4}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.14999999999999997e77

    1. Initial program 93.6%

      \[{x}^{4} - {y}^{4} \]
    2. Add Preprocessing

    if 1.14999999999999997e77 < x

    1. Initial program 63.5%

      \[{x}^{4} - {y}^{4} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 82.7%

      \[\leadsto \color{blue}{{x}^{4}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.15 \cdot 10^{+77}:\\ \;\;\;\;{x}^{4} - {y}^{4}\\ \mathbf{else}:\\ \;\;\;\;{x}^{4}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 82.4% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;{y}^{4} \leq 3 \cdot 10^{-23}:\\ \;\;\;\;{x\_m}^{4}\\ \mathbf{else}:\\ \;\;\;\;-{y}^{4}\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y)
 :precision binary64
 (if (<= (pow y 4.0) 3e-23) (pow x_m 4.0) (- (pow y 4.0))))
x_m = fabs(x);
double code(double x_m, double y) {
	double tmp;
	if (pow(y, 4.0) <= 3e-23) {
		tmp = pow(x_m, 4.0);
	} else {
		tmp = -pow(y, 4.0);
	}
	return tmp;
}
x_m = abs(x)
real(8) function code(x_m, y)
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y ** 4.0d0) <= 3d-23) then
        tmp = x_m ** 4.0d0
    else
        tmp = -(y ** 4.0d0)
    end if
    code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
	double tmp;
	if (Math.pow(y, 4.0) <= 3e-23) {
		tmp = Math.pow(x_m, 4.0);
	} else {
		tmp = -Math.pow(y, 4.0);
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m, y):
	tmp = 0
	if math.pow(y, 4.0) <= 3e-23:
		tmp = math.pow(x_m, 4.0)
	else:
		tmp = -math.pow(y, 4.0)
	return tmp
x_m = abs(x)
function code(x_m, y)
	tmp = 0.0
	if ((y ^ 4.0) <= 3e-23)
		tmp = x_m ^ 4.0;
	else
		tmp = Float64(-(y ^ 4.0));
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m, y)
	tmp = 0.0;
	if ((y ^ 4.0) <= 3e-23)
		tmp = x_m ^ 4.0;
	else
		tmp = -(y ^ 4.0);
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := If[LessEqual[N[Power[y, 4.0], $MachinePrecision], 3e-23], N[Power[x$95$m, 4.0], $MachinePrecision], (-N[Power[y, 4.0], $MachinePrecision])]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;{y}^{4} \leq 3 \cdot 10^{-23}:\\
\;\;\;\;{x\_m}^{4}\\

\mathbf{else}:\\
\;\;\;\;-{y}^{4}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 y 4) < 3.00000000000000003e-23

    1. Initial program 100.0%

      \[{x}^{4} - {y}^{4} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 93.7%

      \[\leadsto \color{blue}{{x}^{4}} \]

    if 3.00000000000000003e-23 < (pow.f64 y 4)

    1. Initial program 74.8%

      \[{x}^{4} - {y}^{4} \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 78.0%

      \[\leadsto \color{blue}{-1 \cdot {y}^{4}} \]
    4. Step-by-step derivation
      1. neg-mul-178.0%

        \[\leadsto \color{blue}{-{y}^{4}} \]
    5. Simplified78.0%

      \[\leadsto \color{blue}{-{y}^{4}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification85.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;{y}^{4} \leq 3 \cdot 10^{-23}:\\ \;\;\;\;{x}^{4}\\ \mathbf{else}:\\ \;\;\;\;-{y}^{4}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 56.7% accurate, 2.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ {x\_m}^{4} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y) :precision binary64 (pow x_m 4.0))
x_m = fabs(x);
double code(double x_m, double y) {
	return pow(x_m, 4.0);
}
x_m = abs(x)
real(8) function code(x_m, y)
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    code = x_m ** 4.0d0
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
	return Math.pow(x_m, 4.0);
}
x_m = math.fabs(x)
def code(x_m, y):
	return math.pow(x_m, 4.0)
x_m = abs(x)
function code(x_m, y)
	return x_m ^ 4.0
end
x_m = abs(x);
function tmp = code(x_m, y)
	tmp = x_m ^ 4.0;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := N[Power[x$95$m, 4.0], $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
{x\_m}^{4}
\end{array}
Derivation
  1. Initial program 87.5%

    \[{x}^{4} - {y}^{4} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf 58.6%

    \[\leadsto \color{blue}{{x}^{4}} \]
  4. Final simplification58.6%

    \[\leadsto {x}^{4} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024044 
(FPCore (x y)
  :name "Radioactive exchange between two surfaces"
  :precision binary64
  (- (pow x 4.0) (pow y 4.0)))