Radioactive exchange between two surfaces

Percentage Accurate: 86.0% → 90.9%
Time: 3.6s
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.0% 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: 90.9% accurate, 1.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.5 \cdot 10^{+97}:\\ \;\;\;\;{x}^{4} - {y}^{4}\\ \mathbf{elif}\;x \leq 5.7 \cdot 10^{+131}:\\ \;\;\;\;-{y}^{4}\\ \mathbf{else}:\\ \;\;\;\;{x}^{4}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= x 1.5e+97)
   (- (pow x 4.0) (pow y 4.0))
   (if (<= x 5.7e+131) (- (pow y 4.0)) (pow x 4.0))))
x = abs(x);
double code(double x, double y) {
	double tmp;
	if (x <= 1.5e+97) {
		tmp = pow(x, 4.0) - pow(y, 4.0);
	} else if (x <= 5.7e+131) {
		tmp = -pow(y, 4.0);
	} else {
		tmp = pow(x, 4.0);
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= 1.5d+97) then
        tmp = (x ** 4.0d0) - (y ** 4.0d0)
    else if (x <= 5.7d+131) then
        tmp = -(y ** 4.0d0)
    else
        tmp = x ** 4.0d0
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x, double y) {
	double tmp;
	if (x <= 1.5e+97) {
		tmp = Math.pow(x, 4.0) - Math.pow(y, 4.0);
	} else if (x <= 5.7e+131) {
		tmp = -Math.pow(y, 4.0);
	} else {
		tmp = Math.pow(x, 4.0);
	}
	return tmp;
}
x = abs(x)
def code(x, y):
	tmp = 0
	if x <= 1.5e+97:
		tmp = math.pow(x, 4.0) - math.pow(y, 4.0)
	elif x <= 5.7e+131:
		tmp = -math.pow(y, 4.0)
	else:
		tmp = math.pow(x, 4.0)
	return tmp
x = abs(x)
function code(x, y)
	tmp = 0.0
	if (x <= 1.5e+97)
		tmp = Float64((x ^ 4.0) - (y ^ 4.0));
	elseif (x <= 5.7e+131)
		tmp = Float64(-(y ^ 4.0));
	else
		tmp = x ^ 4.0;
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= 1.5e+97)
		tmp = (x ^ 4.0) - (y ^ 4.0);
	elseif (x <= 5.7e+131)
		tmp = -(y ^ 4.0);
	else
		tmp = x ^ 4.0;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_, y_] := If[LessEqual[x, 1.5e+97], N[(N[Power[x, 4.0], $MachinePrecision] - N[Power[y, 4.0], $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 5.7e+131], (-N[Power[y, 4.0], $MachinePrecision]), N[Power[x, 4.0], $MachinePrecision]]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.5 \cdot 10^{+97}:\\
\;\;\;\;{x}^{4} - {y}^{4}\\

\mathbf{elif}\;x \leq 5.7 \cdot 10^{+131}:\\
\;\;\;\;-{y}^{4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if x < 1.4999999999999999e97

    1. Initial program 89.6%

      \[{x}^{4} - {y}^{4} \]

    if 1.4999999999999999e97 < x < 5.7e131

    1. Initial program 50.0%

      \[{x}^{4} - {y}^{4} \]
    2. Taylor expanded in x around 0 50.8%

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

        \[\leadsto \color{blue}{-{y}^{4}} \]
    4. Simplified50.8%

      \[\leadsto \color{blue}{-{y}^{4}} \]

    if 5.7e131 < x

    1. Initial program 62.7%

      \[{x}^{4} - {y}^{4} \]
    2. Taylor expanded in x around inf 82.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.5 \cdot 10^{+97}:\\ \;\;\;\;{x}^{4} - {y}^{4}\\ \mathbf{elif}\;x \leq 5.7 \cdot 10^{+131}:\\ \;\;\;\;-{y}^{4}\\ \mathbf{else}:\\ \;\;\;\;{x}^{4}\\ \end{array} \]

Alternative 2: 81.6% accurate, 1.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;{x}^{4} \leq 4.4 \cdot 10^{+285}:\\ \;\;\;\;-{y}^{4}\\ \mathbf{else}:\\ \;\;\;\;{x}^{4}\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= (pow x 4.0) 4.4e+285) (- (pow y 4.0)) (pow x 4.0)))
x = abs(x);
double code(double x, double y) {
	double tmp;
	if (pow(x, 4.0) <= 4.4e+285) {
		tmp = -pow(y, 4.0);
	} else {
		tmp = pow(x, 4.0);
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((x ** 4.0d0) <= 4.4d+285) then
        tmp = -(y ** 4.0d0)
    else
        tmp = x ** 4.0d0
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x, double y) {
	double tmp;
	if (Math.pow(x, 4.0) <= 4.4e+285) {
		tmp = -Math.pow(y, 4.0);
	} else {
		tmp = Math.pow(x, 4.0);
	}
	return tmp;
}
x = abs(x)
def code(x, y):
	tmp = 0
	if math.pow(x, 4.0) <= 4.4e+285:
		tmp = -math.pow(y, 4.0)
	else:
		tmp = math.pow(x, 4.0)
	return tmp
x = abs(x)
function code(x, y)
	tmp = 0.0
	if ((x ^ 4.0) <= 4.4e+285)
		tmp = Float64(-(y ^ 4.0));
	else
		tmp = x ^ 4.0;
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((x ^ 4.0) <= 4.4e+285)
		tmp = -(y ^ 4.0);
	else
		tmp = x ^ 4.0;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_, y_] := If[LessEqual[N[Power[x, 4.0], $MachinePrecision], 4.4e+285], (-N[Power[y, 4.0], $MachinePrecision]), N[Power[x, 4.0], $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;{x}^{4} \leq 4.4 \cdot 10^{+285}:\\
\;\;\;\;-{y}^{4}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 x 4) < 4.4e285

    1. Initial program 100.0%

      \[{x}^{4} - {y}^{4} \]
    2. Taylor expanded in x around 0 90.1%

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

        \[\leadsto \color{blue}{-{y}^{4}} \]
    4. Simplified90.1%

      \[\leadsto \color{blue}{-{y}^{4}} \]

    if 4.4e285 < (pow.f64 x 4)

    1. Initial program 59.6%

      \[{x}^{4} - {y}^{4} \]
    2. Taylor expanded in x around inf 79.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;{x}^{4} \leq 4.4 \cdot 10^{+285}:\\ \;\;\;\;-{y}^{4}\\ \mathbf{else}:\\ \;\;\;\;{x}^{4}\\ \end{array} \]

Alternative 3: 58.1% accurate, 2.0× speedup?

\[\begin{array}{l} x = |x|\\ \\ {x}^{4} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y) :precision binary64 (pow x 4.0))
x = abs(x);
double code(double x, double y) {
	return pow(x, 4.0);
}
NOTE: x should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x ** 4.0d0
end function
x = Math.abs(x);
public static double code(double x, double y) {
	return Math.pow(x, 4.0);
}
x = abs(x)
def code(x, y):
	return math.pow(x, 4.0)
x = abs(x)
function code(x, y)
	return x ^ 4.0
end
x = abs(x)
function tmp = code(x, y)
	tmp = x ^ 4.0;
end
NOTE: x should be positive before calling this function
code[x_, y_] := N[Power[x, 4.0], $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
{x}^{4}
\end{array}
Derivation
  1. Initial program 83.6%

    \[{x}^{4} - {y}^{4} \]
  2. Taylor expanded in x around inf 52.5%

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

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

Reproduce

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