forward-half-diff

Percentage Accurate: 100.0% → 100.0%
Time: 35.4s
Alternatives: 3
Speedup: 1.1×

Specification

?
\[0.5 \cdot \left(W - \frac{1}{W}\right) \]
(FPCore (W)
  :precision binary64
  :pre TRUE
  (* 0.5 (- W (/ 1.0 W))))
double code(double W) {
	return 0.5 * (W - (1.0 / W));
}
real(8) function code(w)
use fmin_fmax_functions
    real(8), intent (in) :: w
    code = 0.5d0 * (w - (1.0d0 / w))
end function
public static double code(double W) {
	return 0.5 * (W - (1.0 / W));
}
def code(W):
	return 0.5 * (W - (1.0 / W))
function code(W)
	return Float64(0.5 * Float64(W - Float64(1.0 / W)))
end
function tmp = code(W)
	tmp = 0.5 * (W - (1.0 / W));
end
code[W_] := N[(0.5 * N[(W - N[(1.0 / W), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
f(W):
	W in [-inf, +inf]
code: THEORY
BEGIN
f(W: real): real =
	(5e-1) * (W - ((1) / W))
END code
0.5 \cdot \left(W - \frac{1}{W}\right)

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?

\[0.5 \cdot \left(W - \frac{1}{W}\right) \]
(FPCore (W)
  :precision binary64
  :pre TRUE
  (* 0.5 (- W (/ 1.0 W))))
double code(double W) {
	return 0.5 * (W - (1.0 / W));
}
real(8) function code(w)
use fmin_fmax_functions
    real(8), intent (in) :: w
    code = 0.5d0 * (w - (1.0d0 / w))
end function
public static double code(double W) {
	return 0.5 * (W - (1.0 / W));
}
def code(W):
	return 0.5 * (W - (1.0 / W))
function code(W)
	return Float64(0.5 * Float64(W - Float64(1.0 / W)))
end
function tmp = code(W)
	tmp = 0.5 * (W - (1.0 / W));
end
code[W_] := N[(0.5 * N[(W - N[(1.0 / W), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
f(W):
	W in [-inf, +inf]
code: THEORY
BEGIN
f(W: real): real =
	(5e-1) * (W - ((1) / W))
END code
0.5 \cdot \left(W - \frac{1}{W}\right)

Alternative 1: 100.0% accurate, 1.1× speedup?

\[\mathsf{fma}\left(W, 0.5, \frac{-0.5}{W}\right) \]
(FPCore (W)
  :precision binary64
  :pre TRUE
  (fma W 0.5 (/ -0.5 W)))
double code(double W) {
	return fma(W, 0.5, (-0.5 / W));
}
function code(W)
	return fma(W, 0.5, Float64(-0.5 / W))
end
code[W_] := N[(W * 0.5 + N[(-0.5 / W), $MachinePrecision]), $MachinePrecision]
f(W):
	W in [-inf, +inf]
code: THEORY
BEGIN
f(W: real): real =
	(W * (5e-1)) + ((-5e-1) / W)
END code
\mathsf{fma}\left(W, 0.5, \frac{-0.5}{W}\right)
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \left(W - \frac{1}{W}\right) \]
  2. Applied rewrites100.0%

    \[\leadsto \mathsf{fma}\left(W, 0.5, \frac{-0.5}{W}\right) \]
  3. Add Preprocessing

Alternative 2: 97.9% accurate, 0.4× speedup?

\[\mathsf{copysign}\left(1, W\right) \cdot \begin{array}{l} \mathbf{if}\;\left|W\right| - \frac{1}{\left|W\right|} \leq -10000000000:\\ \;\;\;\;\frac{-0.5}{\left|W\right|}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left|W\right|\\ \end{array} \]
(FPCore (W)
  :precision binary64
  :pre TRUE
  (*
 (copysign 1.0 W)
 (if (<= (- (fabs W) (/ 1.0 (fabs W))) -10000000000.0)
   (/ -0.5 (fabs W))
   (* 0.5 (fabs W)))))
double code(double W) {
	double tmp;
	if ((fabs(W) - (1.0 / fabs(W))) <= -10000000000.0) {
		tmp = -0.5 / fabs(W);
	} else {
		tmp = 0.5 * fabs(W);
	}
	return copysign(1.0, W) * tmp;
}
public static double code(double W) {
	double tmp;
	if ((Math.abs(W) - (1.0 / Math.abs(W))) <= -10000000000.0) {
		tmp = -0.5 / Math.abs(W);
	} else {
		tmp = 0.5 * Math.abs(W);
	}
	return Math.copySign(1.0, W) * tmp;
}
def code(W):
	tmp = 0
	if (math.fabs(W) - (1.0 / math.fabs(W))) <= -10000000000.0:
		tmp = -0.5 / math.fabs(W)
	else:
		tmp = 0.5 * math.fabs(W)
	return math.copysign(1.0, W) * tmp
function code(W)
	tmp = 0.0
	if (Float64(abs(W) - Float64(1.0 / abs(W))) <= -10000000000.0)
		tmp = Float64(-0.5 / abs(W));
	else
		tmp = Float64(0.5 * abs(W));
	end
	return Float64(copysign(1.0, W) * tmp)
end
function tmp_2 = code(W)
	tmp = 0.0;
	if ((abs(W) - (1.0 / abs(W))) <= -10000000000.0)
		tmp = -0.5 / abs(W);
	else
		tmp = 0.5 * abs(W);
	end
	tmp_2 = (sign(W) * abs(1.0)) * tmp;
end
code[W_] := N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[W]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * If[LessEqual[N[(N[Abs[W], $MachinePrecision] - N[(1.0 / N[Abs[W], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -10000000000.0], N[(-0.5 / N[Abs[W], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Abs[W], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\mathsf{copysign}\left(1, W\right) \cdot \begin{array}{l}
\mathbf{if}\;\left|W\right| - \frac{1}{\left|W\right|} \leq -10000000000:\\
\;\;\;\;\frac{-0.5}{\left|W\right|}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left|W\right|\\


\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 W (/.f64 #s(literal 1 binary64) W)) < -1e10

    1. Initial program 100.0%

      \[0.5 \cdot \left(W - \frac{1}{W}\right) \]
    2. Taylor expanded in W around 0

      \[\leadsto \frac{\frac{-1}{2}}{W} \]
    3. Applied rewrites49.8%

      \[\leadsto \frac{-0.5}{W} \]

    if -1e10 < (-.f64 W (/.f64 #s(literal 1 binary64) W))

    1. Initial program 100.0%

      \[0.5 \cdot \left(W - \frac{1}{W}\right) \]
    2. Taylor expanded in W around inf

      \[\leadsto \frac{1}{2} \cdot W \]
    3. Applied rewrites50.8%

      \[\leadsto 0.5 \cdot W \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 50.8% accurate, 2.6× speedup?

\[0.5 \cdot W \]
(FPCore (W)
  :precision binary64
  :pre TRUE
  (* 0.5 W))
double code(double W) {
	return 0.5 * W;
}
real(8) function code(w)
use fmin_fmax_functions
    real(8), intent (in) :: w
    code = 0.5d0 * w
end function
public static double code(double W) {
	return 0.5 * W;
}
def code(W):
	return 0.5 * W
function code(W)
	return Float64(0.5 * W)
end
function tmp = code(W)
	tmp = 0.5 * W;
end
code[W_] := N[(0.5 * W), $MachinePrecision]
f(W):
	W in [-inf, +inf]
code: THEORY
BEGIN
f(W: real): real =
	(5e-1) * W
END code
0.5 \cdot W
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \left(W - \frac{1}{W}\right) \]
  2. Taylor expanded in W around inf

    \[\leadsto \frac{1}{2} \cdot W \]
  3. Applied rewrites50.8%

    \[\leadsto 0.5 \cdot W \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 2026050 +o generate:egglog
(FPCore (W)
  :name "forward-half-diff"
  :precision binary64
  (* 0.5 (- W (/ 1.0 W))))