
(FPCore (x) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x) {
return (2.0 / (1.0 + exp((-2.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 = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function tmp = code(x) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0; end
code[x_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x) {
return (2.0 / (1.0 + exp((-2.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 = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function tmp = code(x) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0; end
code[x_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (pow (exp x) -2.0)))
(if (<= x -0.02)
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0)
(if (<= x 0.024)
(fma
(* (* x x) x)
(-
(* (* (fma -0.05396825396825397 (* x x) 0.13333333333333333) x) x)
0.3333333333333333)
x)
(fma
(/ 1.0 (fma (pow (exp -2.0) (* x 2.0)) t_0 1.0))
(fma t_0 (expm1 (* x -2.0)) 1.0)
(expm1 (* (log1p t_0) -1.0)))))))
double code(double x) {
double t_0 = pow(exp(x), -2.0);
double tmp;
if (x <= -0.02) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
} else if (x <= 0.024) {
tmp = fma(((x * x) * x), (((fma(-0.05396825396825397, (x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x);
} else {
tmp = fma((1.0 / fma(pow(exp(-2.0), (x * 2.0)), t_0, 1.0)), fma(t_0, expm1((x * -2.0)), 1.0), expm1((log1p(t_0) * -1.0)));
}
return tmp;
}
function code(x) t_0 = exp(x) ^ -2.0 tmp = 0.0 if (x <= -0.02) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0); elseif (x <= 0.024) tmp = fma(Float64(Float64(x * x) * x), Float64(Float64(Float64(fma(-0.05396825396825397, Float64(x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x); else tmp = fma(Float64(1.0 / fma((exp(-2.0) ^ Float64(x * 2.0)), t_0, 1.0)), fma(t_0, expm1(Float64(x * -2.0)), 1.0), expm1(Float64(log1p(t_0) * -1.0))); end return tmp end
code[x_] := Block[{t$95$0 = N[Power[N[Exp[x], $MachinePrecision], -2.0], $MachinePrecision]}, If[LessEqual[x, -0.02], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision], If[LessEqual[x, 0.024], N[(N[(N[(x * x), $MachinePrecision] * x), $MachinePrecision] * N[(N[(N[(N[(-0.05396825396825397 * N[(x * x), $MachinePrecision] + 0.13333333333333333), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 0.3333333333333333), $MachinePrecision] + x), $MachinePrecision], N[(N[(1.0 / N[(N[Power[N[Exp[-2.0], $MachinePrecision], N[(x * 2.0), $MachinePrecision]], $MachinePrecision] * t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision] * N[(t$95$0 * N[(Exp[N[(x * -2.0), $MachinePrecision]] - 1), $MachinePrecision] + 1.0), $MachinePrecision] + N[(Exp[N[(N[Log[1 + t$95$0], $MachinePrecision] * -1.0), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {\left(e^{x}\right)}^{-2}\\
\mathbf{if}\;x \leq -0.02:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\
\mathbf{elif}\;x \leq 0.024:\\
\;\;\;\;\mathsf{fma}\left(\left(x \cdot x\right) \cdot x, \left(\mathsf{fma}\left(-0.05396825396825397, x \cdot x, 0.13333333333333333\right) \cdot x\right) \cdot x - 0.3333333333333333, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{\mathsf{fma}\left({\left(e^{-2}\right)}^{\left(x \cdot 2\right)}, t\_0, 1\right)}, \mathsf{fma}\left(t\_0, \mathsf{expm1}\left(x \cdot -2\right), 1\right), \mathsf{expm1}\left(\mathsf{log1p}\left(t\_0\right) \cdot -1\right)\right)\\
\end{array}
\end{array}
if x < -0.0200000000000000004Initial program 100.0%
if -0.0200000000000000004 < x < 0.024Initial program 10.6%
Taylor expanded in x around 0
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
unpow2N/A
cube-multN/A
*-rgt-identityN/A
lower-fma.f64N/A
Applied rewrites100.0%
if 0.024 < x Initial program 99.9%
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x)
:precision binary64
(if (or (<= x -0.02) (not (<= x 0.024)))
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0)
(fma
(* (* x x) x)
(-
(* (* (fma -0.05396825396825397 (* x x) 0.13333333333333333) x) x)
0.3333333333333333)
x)))
double code(double x) {
double tmp;
if ((x <= -0.02) || !(x <= 0.024)) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
} else {
tmp = fma(((x * x) * x), (((fma(-0.05396825396825397, (x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x);
}
return tmp;
}
function code(x) tmp = 0.0 if ((x <= -0.02) || !(x <= 0.024)) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0); else tmp = fma(Float64(Float64(x * x) * x), Float64(Float64(Float64(fma(-0.05396825396825397, Float64(x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x); end return tmp end
code[x_] := If[Or[LessEqual[x, -0.02], N[Not[LessEqual[x, 0.024]], $MachinePrecision]], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(N[(x * x), $MachinePrecision] * x), $MachinePrecision] * N[(N[(N[(N[(-0.05396825396825397 * N[(x * x), $MachinePrecision] + 0.13333333333333333), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 0.3333333333333333), $MachinePrecision] + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.02 \lor \neg \left(x \leq 0.024\right):\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(x \cdot x\right) \cdot x, \left(\mathsf{fma}\left(-0.05396825396825397, x \cdot x, 0.13333333333333333\right) \cdot x\right) \cdot x - 0.3333333333333333, x\right)\\
\end{array}
\end{array}
if x < -0.0200000000000000004 or 0.024 < x Initial program 100.0%
if -0.0200000000000000004 < x < 0.024Initial program 10.6%
Taylor expanded in x around 0
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
unpow2N/A
cube-multN/A
*-rgt-identityN/A
lower-fma.f64N/A
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (fma (* (* x x) x) (- (* (* (fma -0.05396825396825397 (* x x) 0.13333333333333333) x) x) 0.3333333333333333) x))
double code(double x) {
return fma(((x * x) * x), (((fma(-0.05396825396825397, (x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x);
}
function code(x) return fma(Float64(Float64(x * x) * x), Float64(Float64(Float64(fma(-0.05396825396825397, Float64(x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x) end
code[x_] := N[(N[(N[(x * x), $MachinePrecision] * x), $MachinePrecision] * N[(N[(N[(N[(-0.05396825396825397 * N[(x * x), $MachinePrecision] + 0.13333333333333333), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision] - 0.3333333333333333), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left(x \cdot x\right) \cdot x, \left(\mathsf{fma}\left(-0.05396825396825397, x \cdot x, 0.13333333333333333\right) \cdot x\right) \cdot x - 0.3333333333333333, x\right)
\end{array}
Initial program 56.0%
Taylor expanded in x around 0
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
unpow2N/A
cube-multN/A
*-rgt-identityN/A
lower-fma.f64N/A
Applied rewrites50.0%
herbie shell --seed 2025065
(FPCore (x)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))