
(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 1 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
(if (<= x -0.0245)
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0)
(if (<= x 0.023)
(fma
(pow x 3.0)
(-
(* (* (fma -0.05396825396825397 (* x x) 0.13333333333333333) x) x)
0.3333333333333333)
x)
(fma
(/ 2.0 (+ (pow (exp -2.0) (* x 3.0)) 1.0))
(fma (pow (exp x) -2.0) (expm1 (* x -2.0)) 1.0)
-1.0))))
double code(double x) {
double tmp;
if (x <= -0.0245) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
} else if (x <= 0.023) {
tmp = fma(pow(x, 3.0), (((fma(-0.05396825396825397, (x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x);
} else {
tmp = fma((2.0 / (pow(exp(-2.0), (x * 3.0)) + 1.0)), fma(pow(exp(x), -2.0), expm1((x * -2.0)), 1.0), -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= -0.0245) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0); elseif (x <= 0.023) tmp = fma((x ^ 3.0), Float64(Float64(Float64(fma(-0.05396825396825397, Float64(x * x), 0.13333333333333333) * x) * x) - 0.3333333333333333), x); else tmp = fma(Float64(2.0 / Float64((exp(-2.0) ^ Float64(x * 3.0)) + 1.0)), fma((exp(x) ^ -2.0), expm1(Float64(x * -2.0)), 1.0), -1.0); end return tmp end
code[x_] := If[LessEqual[x, -0.0245], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision], If[LessEqual[x, 0.023], N[(N[Power[x, 3.0], $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[(2.0 / N[(N[Power[N[Exp[-2.0], $MachinePrecision], N[(x * 3.0), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] * N[(N[Power[N[Exp[x], $MachinePrecision], -2.0], $MachinePrecision] * N[(Exp[N[(x * -2.0), $MachinePrecision]] - 1), $MachinePrecision] + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.0245:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} - 1\\
\mathbf{elif}\;x \leq 0.023:\\
\;\;\;\;\mathsf{fma}\left({x}^{3}, \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{2}{{\left(e^{-2}\right)}^{\left(x \cdot 3\right)} + 1}, \mathsf{fma}\left({\left(e^{x}\right)}^{-2}, \mathsf{expm1}\left(x \cdot -2\right), 1\right), -1\right)\\
\end{array}
\end{array}
if x < -0.024500000000000001Initial program 100.0%
if -0.024500000000000001 < x < 0.023Initial program 8.8%
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.023 < x Initial program 99.9%
lift--.f64N/A
lift-/.f64N/A
lift-+.f64N/A
lift-*.f64N/A
lift-exp.f64N/A
metadata-evalN/A
fp-cancel-sub-sign-invN/A
flip3-+N/A
associate-/r/N/A
metadata-evalN/A
metadata-evalN/A
lower-fma.f64N/A
Applied rewrites100.0%
herbie shell --seed 2025059
(FPCore (x)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))