
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x)
use fmin_fmax_functions
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
module fmin_fmax_functions
implicit none
private
public fmax
public fmin
interface fmax
module procedure fmax88
module procedure fmax44
module procedure fmax84
module procedure fmax48
end interface
interface fmin
module procedure fmin88
module procedure fmin44
module procedure fmin84
module procedure fmin48
end interface
contains
real(8) function fmax88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(4) function fmax44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, max(x, y), y /= y), x /= x)
end function
real(8) function fmax84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmax48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
end function
real(8) function fmin88(x, y) result (res)
real(8), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(4) function fmin44(x, y) result (res)
real(4), intent (in) :: x
real(4), intent (in) :: y
res = merge(y, merge(x, min(x, y), y /= y), x /= x)
end function
real(8) function fmin84(x, y) result(res)
real(8), intent (in) :: x
real(4), intent (in) :: y
res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
end function
real(8) function fmin48(x, y) result(res)
real(4), intent (in) :: x
real(8), intent (in) :: y
res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
end function
end module
real(8) function code(x)
use fmin_fmax_functions
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (/ (fma (* (/ (fma (pow x -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) x)))
(if (<= x -13500.0)
(/ t_0 x)
(if (<= x 1700.0)
(- (/ 1.0 (- x -1.0)) (/ 1.0 x))
(/ (fma (* t_0 -1.0) 1.0 -1.0) (* (* (* -1.0 x) x) -1.0))))))
double code(double x) {
double t_0 = fma(((fma(pow(x, -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x;
double tmp;
if (x <= -13500.0) {
tmp = t_0 / x;
} else if (x <= 1700.0) {
tmp = (1.0 / (x - -1.0)) - (1.0 / x);
} else {
tmp = fma((t_0 * -1.0), 1.0, -1.0) / (((-1.0 * x) * x) * -1.0);
}
return tmp;
}
function code(x) t_0 = Float64(fma(Float64(Float64(fma((x ^ -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) tmp = 0.0 if (x <= -13500.0) tmp = Float64(t_0 / x); elseif (x <= 1700.0) tmp = Float64(Float64(1.0 / Float64(x - -1.0)) - Float64(1.0 / x)); else tmp = Float64(fma(Float64(t_0 * -1.0), 1.0, -1.0) / Float64(Float64(Float64(-1.0 * x) * x) * -1.0)); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[(N[(N[(N[Power[x, -1.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision]}, If[LessEqual[x, -13500.0], N[(t$95$0 / x), $MachinePrecision], If[LessEqual[x, 1700.0], N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision], N[(N[(N[(t$95$0 * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * x), $MachinePrecision] * -1.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x}\\
\mathbf{if}\;x \leq -13500:\\
\;\;\;\;\frac{t\_0}{x}\\
\mathbf{elif}\;x \leq 1700:\\
\;\;\;\;\frac{1}{x - -1} - \frac{1}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(t\_0 \cdot -1, 1, -1\right)}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1}\\
\end{array}
\end{array}
if x < -13500Initial program 52.2%
Taylor expanded in x around -inf
unpow2N/A
associate-/r*N/A
lower-/.f64N/A
Applied rewrites99.7%
if -13500 < x < 1700Initial program 100.0%
if 1700 < x Initial program 62.1%
Taylor expanded in x around -inf
frac-2negN/A
lower-/.f64N/A
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ (* -1.0 -1.0) (* (fma (* -1.0 x) 1.0 -1.0) x)))
double code(double x) {
return (-1.0 * -1.0) / (fma((-1.0 * x), 1.0, -1.0) * x);
}
function code(x) return Float64(Float64(-1.0 * -1.0) / Float64(fma(Float64(-1.0 * x), 1.0, -1.0) * x)) end
code[x_] := N[(N[(-1.0 * -1.0), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1 \cdot -1}{\mathsf{fma}\left(-1 \cdot x, 1, -1\right) \cdot x}
\end{array}
Initial program 79.4%
lift--.f64N/A
lift-+.f64N/A
lift-/.f64N/A
frac-2negN/A
metadata-evalN/A
lift-/.f64N/A
frac-2negN/A
metadata-evalN/A
frac-subN/A
lower-/.f64N/A
lower--.f64N/A
lower-*.f64N/A
mul-1-negN/A
lower-*.f64N/A
lower-*.f64N/A
distribute-neg-inN/A
*-lft-identityN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
mul-1-negN/A
lower-*.f64N/A
Applied rewrites79.9%
Taylor expanded in x around 0
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x)
:precision binary64
(let* ((t_0 (* (* -1.0 x) x)))
(if (<= (- (/ 1.0 (- x -1.0)) (/ 1.0 x)) -1.0)
(/ (/ (fma (* (* -1.0 (- x 1.0)) x) 1.0 -1.0) (* t_0 x)) (* -1.0 x))
(/
(fma
(* (/ (fma (* (/ (fma (pow x -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) x) -1.0)
1.0
-1.0)
(* t_0 -1.0)))))
double code(double x) {
double t_0 = (-1.0 * x) * x;
double tmp;
if (((1.0 / (x - -1.0)) - (1.0 / x)) <= -1.0) {
tmp = (fma(((-1.0 * (x - 1.0)) * x), 1.0, -1.0) / (t_0 * x)) / (-1.0 * x);
} else {
tmp = fma(((fma(((fma(pow(x, -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / (t_0 * -1.0);
}
return tmp;
}
function code(x) t_0 = Float64(Float64(-1.0 * x) * x) tmp = 0.0 if (Float64(Float64(1.0 / Float64(x - -1.0)) - Float64(1.0 / x)) <= -1.0) tmp = Float64(Float64(fma(Float64(Float64(-1.0 * Float64(x - 1.0)) * x), 1.0, -1.0) / Float64(t_0 * x)) / Float64(-1.0 * x)); else tmp = Float64(fma(Float64(Float64(fma(Float64(Float64(fma((x ^ -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / Float64(t_0 * -1.0)); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(-1.0 * x), $MachinePrecision] * x), $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[(x - -1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision], -1.0], N[(N[(N[(N[(N[(-1.0 * N[(x - 1.0), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / N[(t$95$0 * x), $MachinePrecision]), $MachinePrecision] / N[(-1.0 * x), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[(N[(N[(N[(N[Power[x, -1.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / N[(t$95$0 * -1.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(-1 \cdot x\right) \cdot x\\
\mathbf{if}\;\frac{1}{x - -1} - \frac{1}{x} \leq -1:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\left(-1 \cdot \left(x - 1\right)\right) \cdot x, 1, -1\right)}{t\_0 \cdot x}}{-1 \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{t\_0 \cdot -1}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < -1Initial program 100.0%
Taylor expanded in x around -inf
unpow2N/A
associate-/r*N/A
lower-/.f64N/A
Applied rewrites6.5%
Applied rewrites6.5%
if -1 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) Initial program 70.6%
Taylor expanded in x around -inf
frac-2negN/A
lower-/.f64N/A
Applied rewrites68.1%
Final simplification49.6%
(FPCore (x) :precision binary64 (/ (/ (fma (* (/ (fma (pow x -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) x) x))
double code(double x) {
return (fma(((fma(pow(x, -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) / x;
}
function code(x) return Float64(Float64(fma(Float64(Float64(fma((x ^ -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) / x) end
code[x_] := N[(N[(N[(N[(N[(N[(N[Power[x, -1.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x}}{x}
\end{array}
Initial program 79.4%
Taylor expanded in x around -inf
unpow2N/A
associate-/r*N/A
lower-/.f64N/A
Applied rewrites49.6%
(FPCore (x) :precision binary64 (/ (fma (* (/ (fma (* (/ (fma (pow x -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) x) -1.0) 1.0 -1.0) (* (* (* -1.0 x) x) -1.0)))
double code(double x) {
return fma(((fma(((fma(pow(x, -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / (((-1.0 * x) * x) * -1.0);
}
function code(x) return Float64(fma(Float64(Float64(fma(Float64(Float64(fma((x ^ -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / x) * -1.0), 1.0, -1.0) / Float64(Float64(Float64(-1.0 * x) * x) * -1.0)) end
code[x_] := N[(N[(N[(N[(N[(N[(N[(N[(N[Power[x, -1.0], $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / x), $MachinePrecision] * -1.0), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * x), $MachinePrecision] * -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left(\frac{\mathsf{fma}\left({x}^{-1}, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{x} \cdot -1, 1, -1\right)}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1}
\end{array}
Initial program 79.4%
Taylor expanded in x around -inf
frac-2negN/A
lower-/.f64N/A
Applied rewrites47.8%
Final simplification47.8%
(FPCore (x) :precision binary64 (/ (/ (fma (* (fma (* (* -1.0 (- x 1.0)) x) 1.0 -1.0) x) 1.0 1.0) (pow x 3.0)) (* (* (* -1.0 x) x) -1.0)))
double code(double x) {
return (fma((fma(((-1.0 * (x - 1.0)) * x), 1.0, -1.0) * x), 1.0, 1.0) / pow(x, 3.0)) / (((-1.0 * x) * x) * -1.0);
}
function code(x) return Float64(Float64(fma(Float64(fma(Float64(Float64(-1.0 * Float64(x - 1.0)) * x), 1.0, -1.0) * x), 1.0, 1.0) / (x ^ 3.0)) / Float64(Float64(Float64(-1.0 * x) * x) * -1.0)) end
code[x_] := N[(N[(N[(N[(N[(N[(N[(-1.0 * N[(x - 1.0), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision] * x), $MachinePrecision] * 1.0 + 1.0), $MachinePrecision] / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] / N[(N[(N[(-1.0 * x), $MachinePrecision] * x), $MachinePrecision] * -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left(\left(-1 \cdot \left(x - 1\right)\right) \cdot x, 1, -1\right) \cdot x, 1, 1\right)}{{x}^{3}}}{\left(\left(-1 \cdot x\right) \cdot x\right) \cdot -1}
\end{array}
Initial program 79.4%
Taylor expanded in x around -inf
frac-2negN/A
lower-/.f64N/A
Applied rewrites47.8%
Applied rewrites13.0%
Final simplification13.0%
herbie shell --seed 2025065
(FPCore (x)
:name "2frac (problem 3.3.1)"
:precision binary64
:alt
(! :herbie-platform c (/ 1 (* x (- -1 x))))
(- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))