Radioactive exchange between two surfaces

Percentage Accurate: 86.2% → 91.5%
Time: 2.0s
Alternatives: 3
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 3 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.2% 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: 91.5% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y_m \leq 8.2 \cdot 10^{+126}:\\
\;\;\;\;{x}^{4} - {y_m}^{4}\\

\mathbf{elif}\;y_m \leq 2 \cdot 10^{+142}:\\
\;\;\;\;{x}^{4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < 8.2000000000000001e126

    1. Initial program 90.7%

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

    if 8.2000000000000001e126 < y < 2.0000000000000001e142

    1. Initial program 0.0%

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

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

    if 2.0000000000000001e142 < y

    1. Initial program 68.4%

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

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

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

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

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

Alternative 2: 81.7% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;{y_m}^{4} \leq 1.26 \cdot 10^{+88}:\\
\;\;\;\;{x}^{4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 y 4) < 1.26e88

    1. Initial program 100.0%

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

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

    if 1.26e88 < (pow.f64 y 4)

    1. Initial program 71.8%

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

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

        \[\leadsto \color{blue}{-{y}^{4}} \]
    5. Simplified83.9%

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

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

Alternative 3: 57.5% accurate, 2.0× speedup?

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

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

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

    \[\leadsto \color{blue}{{x}^{4}} \]
  4. Final simplification54.3%

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

Reproduce

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