
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
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 - 1.0d0))
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0))
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / (x - 1.0)); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
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 - 1.0d0))
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0))
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / (x - 1.0)); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x - 1}
\end{array}
x_m = (fabs.f64 x)
(FPCore (x_m)
:precision binary64
(let* ((t_0 (* (* -1.0 x_m) x_m)) (t_1 (pow (* -1.0 t_0) 2.0)))
(if (<= x_m 200000000.0)
(/ (fma (- x_m 1.0) 1.0 (fma (* -1.0 x_m) 1.0 -1.0)) (fma t_0 -1.0 -1.0))
(/
(/
(fma (fma (* (pow t_1 -0.5) (- -2.0)) -1.0 (/ -2.0 t_1)) 1.0 -2.0)
x_m)
x_m))))x_m = fabs(x);
double code(double x_m) {
double t_0 = (-1.0 * x_m) * x_m;
double t_1 = pow((-1.0 * t_0), 2.0);
double tmp;
if (x_m <= 200000000.0) {
tmp = fma((x_m - 1.0), 1.0, fma((-1.0 * x_m), 1.0, -1.0)) / fma(t_0, -1.0, -1.0);
} else {
tmp = (fma(fma((pow(t_1, -0.5) * -(-2.0)), -1.0, (-2.0 / t_1)), 1.0, -2.0) / x_m) / x_m;
}
return tmp;
}
x_m = abs(x) function code(x_m) t_0 = Float64(Float64(-1.0 * x_m) * x_m) t_1 = Float64(-1.0 * t_0) ^ 2.0 tmp = 0.0 if (x_m <= 200000000.0) tmp = Float64(fma(Float64(x_m - 1.0), 1.0, fma(Float64(-1.0 * x_m), 1.0, -1.0)) / fma(t_0, -1.0, -1.0)); else tmp = Float64(Float64(fma(fma(Float64((t_1 ^ -0.5) * Float64(-(-2.0))), -1.0, Float64(-2.0 / t_1)), 1.0, -2.0) / x_m) / x_m); end return tmp end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := Block[{t$95$0 = N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]}, Block[{t$95$1 = N[Power[N[(-1.0 * t$95$0), $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[x$95$m, 200000000.0], N[(N[(N[(x$95$m - 1.0), $MachinePrecision] * 1.0 + N[(N[(-1.0 * x$95$m), $MachinePrecision] * 1.0 + -1.0), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * -1.0 + -1.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[(N[Power[t$95$1, -0.5], $MachinePrecision] * (--2.0)), $MachinePrecision] * -1.0 + N[(-2.0 / t$95$1), $MachinePrecision]), $MachinePrecision] * 1.0 + -2.0), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := \left(-1 \cdot x\_m\right) \cdot x\_m\\
t_1 := {\left(-1 \cdot t\_0\right)}^{2}\\
\mathbf{if}\;x\_m \leq 200000000:\\
\;\;\;\;\frac{\mathsf{fma}\left(x\_m - 1, 1, \mathsf{fma}\left(-1 \cdot x\_m, 1, -1\right)\right)}{\mathsf{fma}\left(t\_0, -1, -1\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_1}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_1}\right), 1, -2\right)}{x\_m}}{x\_m}\\
\end{array}
\end{array}
if x < 2e8Initial program 86.1%
lift--.f64N/A
lift-+.f64N/A
lift-/.f64N/A
lift--.f64N/A
lift-/.f64N/A
frac-subN/A
difference-of-squares-revN/A
lower-/.f64N/A
fp-cancel-sub-sign-invN/A
*-commutativeN/A
lower-fma.f64N/A
lift--.f64N/A
lower-*.f64N/A
distribute-neg-inN/A
metadata-evalN/A
*-rgt-identityN/A
lower-fma.f64N/A
mul-1-negN/A
lower-*.f64N/A
difference-of-squares-revN/A
difference-of-sqr--1-revN/A
Applied rewrites86.8%
if 2e8 < x Initial program 49.6%
Taylor expanded in x around inf
div-subN/A
frac-subN/A
pow-prod-upN/A
metadata-evalN/A
lower-/.f64N/A
Applied rewrites35.3%
Applied rewrites99.9%
Final simplification90.4%
x_m = (fabs.f64 x)
(FPCore (x_m)
:precision binary64
(let* ((t_0 (pow (* -1.0 (* (* -1.0 x_m) x_m)) 2.0)))
(if (<= x_m 230.0)
(- (/ 1.0 (- x_m -1.0)) (/ 1.0 (- x_m 1.0)))
(/
(/
(fma (fma (* (pow t_0 -0.5) (- -2.0)) -1.0 (/ -2.0 t_0)) 1.0 -2.0)
x_m)
x_m))))x_m = fabs(x);
double code(double x_m) {
double t_0 = pow((-1.0 * ((-1.0 * x_m) * x_m)), 2.0);
double tmp;
if (x_m <= 230.0) {
tmp = (1.0 / (x_m - -1.0)) - (1.0 / (x_m - 1.0));
} else {
tmp = (fma(fma((pow(t_0, -0.5) * -(-2.0)), -1.0, (-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m;
}
return tmp;
}
x_m = abs(x) function code(x_m) t_0 = Float64(-1.0 * Float64(Float64(-1.0 * x_m) * x_m)) ^ 2.0 tmp = 0.0 if (x_m <= 230.0) tmp = Float64(Float64(1.0 / Float64(x_m - -1.0)) - Float64(1.0 / Float64(x_m - 1.0))); else tmp = Float64(Float64(fma(fma(Float64((t_0 ^ -0.5) * Float64(-(-2.0))), -1.0, Float64(-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m); end return tmp end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := Block[{t$95$0 = N[Power[N[(-1.0 * N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[x$95$m, 230.0], N[(N[(1.0 / N[(x$95$m - -1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x$95$m - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(N[(N[Power[t$95$0, -0.5], $MachinePrecision] * (--2.0)), $MachinePrecision] * -1.0 + N[(-2.0 / t$95$0), $MachinePrecision]), $MachinePrecision] * 1.0 + -2.0), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := {\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{2}\\
\mathbf{if}\;x\_m \leq 230:\\
\;\;\;\;\frac{1}{x\_m - -1} - \frac{1}{x\_m - 1}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_0}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_0}\right), 1, -2\right)}{x\_m}}{x\_m}\\
\end{array}
\end{array}
if x < 230Initial program 86.3%
if 230 < x Initial program 50.1%
Taylor expanded in x around inf
div-subN/A
frac-subN/A
pow-prod-upN/A
metadata-evalN/A
lower-/.f64N/A
Applied rewrites37.1%
Applied rewrites99.9%
Final simplification90.2%
x_m = (fabs.f64 x)
(FPCore (x_m)
:precision binary64
(let* ((t_0 (pow (* -1.0 (* (* -1.0 x_m) x_m)) 2.0)))
(/
(/ (fma (fma (* (pow t_0 -0.5) (- -2.0)) -1.0 (/ -2.0 t_0)) 1.0 -2.0) x_m)
x_m)))x_m = fabs(x);
double code(double x_m) {
double t_0 = pow((-1.0 * ((-1.0 * x_m) * x_m)), 2.0);
return (fma(fma((pow(t_0, -0.5) * -(-2.0)), -1.0, (-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m;
}
x_m = abs(x) function code(x_m) t_0 = Float64(-1.0 * Float64(Float64(-1.0 * x_m) * x_m)) ^ 2.0 return Float64(Float64(fma(fma(Float64((t_0 ^ -0.5) * Float64(-(-2.0))), -1.0, Float64(-2.0 / t_0)), 1.0, -2.0) / x_m) / x_m) end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_] := Block[{t$95$0 = N[Power[N[(-1.0 * N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]}, N[(N[(N[(N[(N[(N[Power[t$95$0, -0.5], $MachinePrecision] * (--2.0)), $MachinePrecision] * -1.0 + N[(-2.0 / t$95$0), $MachinePrecision]), $MachinePrecision] * 1.0 + -2.0), $MachinePrecision] / x$95$m), $MachinePrecision] / x$95$m), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
t_0 := {\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{2}\\
\frac{\frac{\mathsf{fma}\left(\mathsf{fma}\left({t\_0}^{-0.5} \cdot \left(--2\right), -1, \frac{-2}{t\_0}\right), 1, -2\right)}{x\_m}}{x\_m}
\end{array}
\end{array}
Initial program 76.0%
Taylor expanded in x around inf
div-subN/A
frac-subN/A
pow-prod-upN/A
metadata-evalN/A
lower-/.f64N/A
Applied rewrites17.0%
Applied rewrites52.4%
Final simplification52.4%
x_m = (fabs.f64 x) (FPCore (x_m) :precision binary64 (* (/ -1.0 x_m) (/ (fma (* (pow (* -1.0 (* (* -1.0 x_m) x_m)) -1.0) 2.0) 1.0 2.0) x_m)))
x_m = fabs(x);
double code(double x_m) {
return (-1.0 / x_m) * (fma((pow((-1.0 * ((-1.0 * x_m) * x_m)), -1.0) * 2.0), 1.0, 2.0) / x_m);
}
x_m = abs(x) function code(x_m) return Float64(Float64(-1.0 / x_m) * Float64(fma(Float64((Float64(-1.0 * Float64(Float64(-1.0 * x_m) * x_m)) ^ -1.0) * 2.0), 1.0, 2.0) / x_m)) end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_] := N[(N[(-1.0 / x$95$m), $MachinePrecision] * N[(N[(N[(N[Power[N[(-1.0 * N[(N[(-1.0 * x$95$m), $MachinePrecision] * x$95$m), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision] * 2.0), $MachinePrecision] * 1.0 + 2.0), $MachinePrecision] / x$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
\frac{-1}{x\_m} \cdot \frac{\mathsf{fma}\left({\left(-1 \cdot \left(\left(-1 \cdot x\_m\right) \cdot x\_m\right)\right)}^{-1} \cdot 2, 1, 2\right)}{x\_m}
\end{array}
Initial program 76.0%
Taylor expanded in x around inf
associate-*r/N/A
unpow2N/A
times-fracN/A
lower-*.f64N/A
lower-/.f64N/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
metadata-evalN/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
inv-powN/A
lower-pow.f64N/A
unpow2N/A
lower-*.f6452.2
Applied rewrites52.2%
Final simplification52.2%
herbie shell --seed 2025065
(FPCore (x)
:name "Asymptote A"
:precision binary64
(- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))