
(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)
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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)
(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)
Initial program 100.0%
Applied rewrites100.0%
(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}
if (-.f64 W (/.f64 #s(literal 1 binary64) W)) < -1e10Initial program 100.0%
Taylor expanded in W around 0
Applied rewrites49.8%
if -1e10 < (-.f64 W (/.f64 #s(literal 1 binary64) W)) Initial program 100.0%
Taylor expanded in W around inf
Applied rewrites50.8%
(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
Initial program 100.0%
Taylor expanded in W around inf
Applied rewrites50.8%
herbie shell --seed 2026050 +o generate:egglog
(FPCore (W)
:name "forward-half-diff"
:precision binary64
(* 0.5 (- W (/ 1.0 W))))