(x + y) * 0.5

Percentage Accurate: 100.0% → 100.0%
Time: 801.0ms
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\left(-1000000000 \leq x \land x \leq 1000000000\right) \land \left(-1000000000 \leq y \land y \leq 1000000000\right)\]
\[\left(x + y\right) \cdot 0.5 \]
(FPCore (x y)
  :precision binary64
  (* (+ x y) 0.5))
double code(double x, double y) {
	return (x + y) * 0.5;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) * 0.5d0
end function
public static double code(double x, double y) {
	return (x + y) * 0.5;
}
def code(x, y):
	return (x + y) * 0.5
function code(x, y)
	return Float64(Float64(x + y) * 0.5)
end
function tmp = code(x, y)
	tmp = (x + y) * 0.5;
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * 0.5), $MachinePrecision]
\left(x + y\right) \cdot 0.5

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: 100.0% accurate, 1.0× speedup?

\[\left(x + y\right) \cdot 0.5 \]
(FPCore (x y)
  :precision binary64
  (* (+ x y) 0.5))
double code(double x, double y) {
	return (x + y) * 0.5;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (x + y) * 0.5d0
end function
public static double code(double x, double y) {
	return (x + y) * 0.5;
}
def code(x, y):
	return (x + y) * 0.5
function code(x, y)
	return Float64(Float64(x + y) * 0.5)
end
function tmp = code(x, y)
	tmp = (x + y) * 0.5;
end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * 0.5), $MachinePrecision]
\left(x + y\right) \cdot 0.5

Alternative 1: 95.8% accurate, 0.3× speedup?

\[\begin{array}{l} \mathbf{if}\;\left(\mathsf{min}\left(x, y\right) + \mathsf{max}\left(x, y\right)\right) \cdot 0.5 \leq -5 \cdot 10^{-294}:\\ \;\;\;\;0.5 \cdot \mathsf{min}\left(x, y\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{max}\left(x, y\right) \cdot 0.5\\ \end{array} \]
(FPCore (x y)
  :precision binary64
  (if (<= (* (+ (fmin x y) (fmax x y)) 0.5) -5e-294)
  (* 0.5 (fmin x y))
  (* (fmax x y) 0.5)))
double code(double x, double y) {
	double tmp;
	if (((fmin(x, y) + fmax(x, y)) * 0.5) <= -5e-294) {
		tmp = 0.5 * fmin(x, y);
	} else {
		tmp = fmax(x, y) * 0.5;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (((merge(y, merge(x, min(x, y), y /= y), x /= x) + merge(y, merge(x, max(x, y), y /= y), x /= x)) * 0.5d0) <= (-5d-294)) then
        tmp = 0.5d0 * merge(y, merge(x, min(x, y), y /= y), x /= x)
    else
        tmp = merge(y, merge(x, max(x, y), y /= y), x /= x) * 0.5d0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (((fmin(x, y) + fmax(x, y)) * 0.5) <= -5e-294) {
		tmp = 0.5 * fmin(x, y);
	} else {
		tmp = fmax(x, y) * 0.5;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if ((fmin(x, y) + fmax(x, y)) * 0.5) <= -5e-294:
		tmp = 0.5 * fmin(x, y)
	else:
		tmp = fmax(x, y) * 0.5
	return tmp
function code(x, y)
	tmp = 0.0
	if (Float64(Float64(((x != x) ? y : ((y != y) ? x : min(x, y))) + ((x != x) ? y : ((y != y) ? x : max(x, y)))) * 0.5) <= -5e-294)
		tmp = Float64(0.5 * ((x != x) ? y : ((y != y) ? x : min(x, y))));
	else
		tmp = Float64(((x != x) ? y : ((y != y) ? x : max(x, y))) * 0.5);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (((min(x, y) + max(x, y)) * 0.5) <= -5e-294)
		tmp = 0.5 * min(x, y);
	else
		tmp = max(x, y) * 0.5;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[(N[(N[Min[x, y], $MachinePrecision] + N[Max[x, y], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], -5e-294], N[(0.5 * N[Min[x, y], $MachinePrecision]), $MachinePrecision], N[(N[Max[x, y], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\left(\mathsf{min}\left(x, y\right) + \mathsf{max}\left(x, y\right)\right) \cdot 0.5 \leq -5 \cdot 10^{-294}:\\
\;\;\;\;0.5 \cdot \mathsf{min}\left(x, y\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{max}\left(x, y\right) \cdot 0.5\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (+.f64 x y) #s(literal 1/2 binary64)) < -5.0000000000000003e-294

    1. Initial program 100.0%

      \[\left(x + y\right) \cdot 0.5 \]
    2. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot x} \]
    3. Step-by-step derivation
      1. lower-*.f6451.7%

        \[\leadsto 0.5 \cdot \color{blue}{x} \]
    4. Applied rewrites51.7%

      \[\leadsto \color{blue}{0.5 \cdot x} \]

    if -5.0000000000000003e-294 < (*.f64 (+.f64 x y) #s(literal 1/2 binary64))

    1. Initial program 100.0%

      \[\left(x + y\right) \cdot 0.5 \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{y} \cdot 0.5 \]
    3. Step-by-step derivation
      1. Applied rewrites49.1%

        \[\leadsto \color{blue}{y} \cdot 0.5 \]
    4. Recombined 2 regimes into one program.
    5. Add Preprocessing

    Alternative 2: 50.8% accurate, 0.9× speedup?

    \[0.5 \cdot \mathsf{min}\left(x, y\right) \]
    (FPCore (x y)
      :precision binary64
      (* 0.5 (fmin x y)))
    double code(double x, double y) {
    	return 0.5 * fmin(x, y);
    }
    
    real(8) function code(x, y)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        code = 0.5d0 * merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    
    public static double code(double x, double y) {
    	return 0.5 * fmin(x, y);
    }
    
    def code(x, y):
    	return 0.5 * fmin(x, y)
    
    function code(x, y)
    	return Float64(0.5 * ((x != x) ? y : ((y != y) ? x : min(x, y))))
    end
    
    function tmp = code(x, y)
    	tmp = 0.5 * min(x, y);
    end
    
    code[x_, y_] := N[(0.5 * N[Min[x, y], $MachinePrecision]), $MachinePrecision]
    
    0.5 \cdot \mathsf{min}\left(x, y\right)
    
    Derivation
    1. Initial program 100.0%

      \[\left(x + y\right) \cdot 0.5 \]
    2. Taylor expanded in x around inf

      \[\leadsto \color{blue}{\frac{1}{2} \cdot x} \]
    3. Step-by-step derivation
      1. lower-*.f6451.7%

        \[\leadsto 0.5 \cdot \color{blue}{x} \]
    4. Applied rewrites51.7%

      \[\leadsto \color{blue}{0.5 \cdot x} \]
    5. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 1 
    (FPCore (x y)
      :name "(x + y) * 0.5"
      :precision binary64
      :pre (and (and (<= -1000000000.0 x) (<= x 1000000000.0)) (and (<= -1000000000.0 y) (<= y 1000000000.0)))
      (* (+ x y) 0.5))