
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
return log((1.0 + exp(x))) - (x * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y): return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y) return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)) end
function tmp = code(x, y) tmp = log((1.0 + exp(x))) - (x * y); end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + e^{x}\right) - x \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
return log((1.0 + exp(x))) - (x * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y): return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y) return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)) end
function tmp = code(x, y) tmp = log((1.0 + exp(x))) - (x * y); end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + e^{x}\right) - x \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma (- y) x (log1p (exp x))))
double code(double x, double y) {
return fma(-y, x, log1p(exp(x)));
}
function code(x, y) return fma(Float64(-y), x, log1p(exp(x))) end
code[x_, y_] := N[((-y) * x + N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-y, x, \mathsf{log1p}\left(e^{x}\right)\right)
\end{array}
Initial program 99.6%
sub-neg99.6%
+-commutative99.6%
*-commutative99.6%
distribute-lft-neg-in99.6%
fma-define99.6%
log1p-define100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (- (log1p (exp x)) (* y x)))
double code(double x, double y) {
return log1p(exp(x)) - (y * x);
}
public static double code(double x, double y) {
return Math.log1p(Math.exp(x)) - (y * x);
}
def code(x, y): return math.log1p(math.exp(x)) - (y * x)
function code(x, y) return Float64(log1p(exp(x)) - Float64(y * x)) end
code[x_, y_] := N[(N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision] - N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(e^{x}\right) - y \cdot x
\end{array}
Initial program 99.6%
*-un-lft-identity99.6%
*-commutative99.6%
log-prod99.6%
log1p-define100.0%
metadata-eval100.0%
Applied egg-rr100.0%
+-rgt-identity100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= x -2.75e-61)
(* (- y) x)
(if (<= x 5.2e-71)
(log (+ x 2.0))
(if (<= x 6.5e-34) (* x (- 0.5 y)) (+ (log 2.0) (* x 0.5))))))
double code(double x, double y) {
double tmp;
if (x <= -2.75e-61) {
tmp = -y * x;
} else if (x <= 5.2e-71) {
tmp = log((x + 2.0));
} else if (x <= 6.5e-34) {
tmp = x * (0.5 - y);
} else {
tmp = log(2.0) + (x * 0.5);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2.75d-61)) then
tmp = -y * x
else if (x <= 5.2d-71) then
tmp = log((x + 2.0d0))
else if (x <= 6.5d-34) then
tmp = x * (0.5d0 - y)
else
tmp = log(2.0d0) + (x * 0.5d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2.75e-61) {
tmp = -y * x;
} else if (x <= 5.2e-71) {
tmp = Math.log((x + 2.0));
} else if (x <= 6.5e-34) {
tmp = x * (0.5 - y);
} else {
tmp = Math.log(2.0) + (x * 0.5);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.75e-61: tmp = -y * x elif x <= 5.2e-71: tmp = math.log((x + 2.0)) elif x <= 6.5e-34: tmp = x * (0.5 - y) else: tmp = math.log(2.0) + (x * 0.5) return tmp
function code(x, y) tmp = 0.0 if (x <= -2.75e-61) tmp = Float64(Float64(-y) * x); elseif (x <= 5.2e-71) tmp = log(Float64(x + 2.0)); elseif (x <= 6.5e-34) tmp = Float64(x * Float64(0.5 - y)); else tmp = Float64(log(2.0) + Float64(x * 0.5)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2.75e-61) tmp = -y * x; elseif (x <= 5.2e-71) tmp = log((x + 2.0)); elseif (x <= 6.5e-34) tmp = x * (0.5 - y); else tmp = log(2.0) + (x * 0.5); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.75e-61], N[((-y) * x), $MachinePrecision], If[LessEqual[x, 5.2e-71], N[Log[N[(x + 2.0), $MachinePrecision]], $MachinePrecision], If[LessEqual[x, 6.5e-34], N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.75 \cdot 10^{-61}:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{elif}\;x \leq 5.2 \cdot 10^{-71}:\\
\;\;\;\;\log \left(x + 2\right)\\
\mathbf{elif}\;x \leq 6.5 \cdot 10^{-34}:\\
\;\;\;\;x \cdot \left(0.5 - y\right)\\
\mathbf{else}:\\
\;\;\;\;\log 2 + x \cdot 0.5\\
\end{array}
\end{array}
if x < -2.7499999999999998e-61Initial program 99.1%
Taylor expanded in x around inf 93.2%
associate-*r*93.2%
neg-mul-193.2%
Simplified93.2%
if -2.7499999999999998e-61 < x < 5.1999999999999997e-71Initial program 100.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in y around 0 85.0%
if 5.1999999999999997e-71 < x < 6.49999999999999985e-34Initial program 100.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around inf 89.6%
if 6.49999999999999985e-34 < x Initial program 99.0%
Taylor expanded in x around 0 89.4%
Taylor expanded in y around 0 69.3%
Final simplification88.3%
(FPCore (x y)
:precision binary64
(if (<= x -5.4e-62)
(* (- y) x)
(if (or (<= x 5.6e-71) (not (<= x 1.9e-27)))
(log (+ x 2.0))
(* x (- 0.5 y)))))
double code(double x, double y) {
double tmp;
if (x <= -5.4e-62) {
tmp = -y * x;
} else if ((x <= 5.6e-71) || !(x <= 1.9e-27)) {
tmp = log((x + 2.0));
} else {
tmp = x * (0.5 - y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-5.4d-62)) then
tmp = -y * x
else if ((x <= 5.6d-71) .or. (.not. (x <= 1.9d-27))) then
tmp = log((x + 2.0d0))
else
tmp = x * (0.5d0 - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -5.4e-62) {
tmp = -y * x;
} else if ((x <= 5.6e-71) || !(x <= 1.9e-27)) {
tmp = Math.log((x + 2.0));
} else {
tmp = x * (0.5 - y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -5.4e-62: tmp = -y * x elif (x <= 5.6e-71) or not (x <= 1.9e-27): tmp = math.log((x + 2.0)) else: tmp = x * (0.5 - y) return tmp
function code(x, y) tmp = 0.0 if (x <= -5.4e-62) tmp = Float64(Float64(-y) * x); elseif ((x <= 5.6e-71) || !(x <= 1.9e-27)) tmp = log(Float64(x + 2.0)); else tmp = Float64(x * Float64(0.5 - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -5.4e-62) tmp = -y * x; elseif ((x <= 5.6e-71) || ~((x <= 1.9e-27))) tmp = log((x + 2.0)); else tmp = x * (0.5 - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -5.4e-62], N[((-y) * x), $MachinePrecision], If[Or[LessEqual[x, 5.6e-71], N[Not[LessEqual[x, 1.9e-27]], $MachinePrecision]], N[Log[N[(x + 2.0), $MachinePrecision]], $MachinePrecision], N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5.4 \cdot 10^{-62}:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{elif}\;x \leq 5.6 \cdot 10^{-71} \lor \neg \left(x \leq 1.9 \cdot 10^{-27}\right):\\
\;\;\;\;\log \left(x + 2\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(0.5 - y\right)\\
\end{array}
\end{array}
if x < -5.40000000000000039e-62Initial program 99.1%
Taylor expanded in x around inf 93.2%
associate-*r*93.2%
neg-mul-193.2%
Simplified93.2%
if -5.40000000000000039e-62 < x < 5.60000000000000001e-71 or 1.9e-27 < x Initial program 99.9%
Taylor expanded in x around 0 99.2%
Taylor expanded in y around 0 83.9%
if 5.60000000000000001e-71 < x < 1.9e-27Initial program 100.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around inf 89.6%
Final simplification88.3%
(FPCore (x y)
:precision binary64
(if (<= x -2.6e-61)
(* (- y) x)
(if (<= x 2.65e-70)
(log 2.0)
(if (<= x 1.08e-35) (* x (- 0.5 y)) (log 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= -2.6e-61) {
tmp = -y * x;
} else if (x <= 2.65e-70) {
tmp = log(2.0);
} else if (x <= 1.08e-35) {
tmp = x * (0.5 - y);
} else {
tmp = log(2.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2.6d-61)) then
tmp = -y * x
else if (x <= 2.65d-70) then
tmp = log(2.0d0)
else if (x <= 1.08d-35) then
tmp = x * (0.5d0 - y)
else
tmp = log(2.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2.6e-61) {
tmp = -y * x;
} else if (x <= 2.65e-70) {
tmp = Math.log(2.0);
} else if (x <= 1.08e-35) {
tmp = x * (0.5 - y);
} else {
tmp = Math.log(2.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.6e-61: tmp = -y * x elif x <= 2.65e-70: tmp = math.log(2.0) elif x <= 1.08e-35: tmp = x * (0.5 - y) else: tmp = math.log(2.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -2.6e-61) tmp = Float64(Float64(-y) * x); elseif (x <= 2.65e-70) tmp = log(2.0); elseif (x <= 1.08e-35) tmp = Float64(x * Float64(0.5 - y)); else tmp = log(2.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2.6e-61) tmp = -y * x; elseif (x <= 2.65e-70) tmp = log(2.0); elseif (x <= 1.08e-35) tmp = x * (0.5 - y); else tmp = log(2.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.6e-61], N[((-y) * x), $MachinePrecision], If[LessEqual[x, 2.65e-70], N[Log[2.0], $MachinePrecision], If[LessEqual[x, 1.08e-35], N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision], N[Log[2.0], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.6 \cdot 10^{-61}:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{elif}\;x \leq 2.65 \cdot 10^{-70}:\\
\;\;\;\;\log 2\\
\mathbf{elif}\;x \leq 1.08 \cdot 10^{-35}:\\
\;\;\;\;x \cdot \left(0.5 - y\right)\\
\mathbf{else}:\\
\;\;\;\;\log 2\\
\end{array}
\end{array}
if x < -2.6000000000000001e-61Initial program 99.1%
Taylor expanded in x around inf 93.2%
associate-*r*93.2%
neg-mul-193.2%
Simplified93.2%
if -2.6000000000000001e-61 < x < 2.64999999999999992e-70 or 1.08000000000000003e-35 < x Initial program 99.9%
sub-neg99.9%
+-commutative99.9%
*-commutative99.9%
distribute-lft-neg-in99.9%
fma-define99.9%
log1p-define100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 98.7%
Taylor expanded in y around 0 83.4%
if 2.64999999999999992e-70 < x < 1.08000000000000003e-35Initial program 100.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in x around inf 89.6%
Final simplification88.0%
(FPCore (x y) :precision binary64 (if (<= x -370.0) (* (- y) x) (+ (log 2.0) (* x (+ (* x 0.125) (- 0.5 y))))))
double code(double x, double y) {
double tmp;
if (x <= -370.0) {
tmp = -y * x;
} else {
tmp = log(2.0) + (x * ((x * 0.125) + (0.5 - y)));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-370.0d0)) then
tmp = -y * x
else
tmp = log(2.0d0) + (x * ((x * 0.125d0) + (0.5d0 - y)))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -370.0) {
tmp = -y * x;
} else {
tmp = Math.log(2.0) + (x * ((x * 0.125) + (0.5 - y)));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -370.0: tmp = -y * x else: tmp = math.log(2.0) + (x * ((x * 0.125) + (0.5 - y))) return tmp
function code(x, y) tmp = 0.0 if (x <= -370.0) tmp = Float64(Float64(-y) * x); else tmp = Float64(log(2.0) + Float64(x * Float64(Float64(x * 0.125) + Float64(0.5 - y)))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -370.0) tmp = -y * x; else tmp = log(2.0) + (x * ((x * 0.125) + (0.5 - y))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -370.0], N[((-y) * x), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + N[(x * N[(N[(x * 0.125), $MachinePrecision] + N[(0.5 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -370:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\log 2 + x \cdot \left(x \cdot 0.125 + \left(0.5 - y\right)\right)\\
\end{array}
\end{array}
if x < -370Initial program 100.0%
Taylor expanded in x around inf 100.0%
associate-*r*100.0%
neg-mul-1100.0%
Simplified100.0%
if -370 < x Initial program 99.3%
Taylor expanded in x around 0 99.0%
*-commutative99.0%
unpow299.0%
associate-*l*99.0%
distribute-lft-out99.0%
Simplified99.0%
Final simplification99.4%
(FPCore (x y) :precision binary64 (if (<= x -1.4) (* (- y) x) (+ (log 2.0) (* x (- 0.5 y)))))
double code(double x, double y) {
double tmp;
if (x <= -1.4) {
tmp = -y * x;
} else {
tmp = log(2.0) + (x * (0.5 - y));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.4d0)) then
tmp = -y * x
else
tmp = log(2.0d0) + (x * (0.5d0 - y))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.4) {
tmp = -y * x;
} else {
tmp = Math.log(2.0) + (x * (0.5 - y));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.4: tmp = -y * x else: tmp = math.log(2.0) + (x * (0.5 - y)) return tmp
function code(x, y) tmp = 0.0 if (x <= -1.4) tmp = Float64(Float64(-y) * x); else tmp = Float64(log(2.0) + Float64(x * Float64(0.5 - y))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.4) tmp = -y * x; else tmp = log(2.0) + (x * (0.5 - y)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.4], N[((-y) * x), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + N[(x * N[(0.5 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.4:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\log 2 + x \cdot \left(0.5 - y\right)\\
\end{array}
\end{array}
if x < -1.3999999999999999Initial program 99.0%
Taylor expanded in x around inf 99.0%
associate-*r*99.0%
neg-mul-199.0%
Simplified99.0%
if -1.3999999999999999 < x Initial program 99.9%
Taylor expanded in x around 0 99.1%
Final simplification99.0%
(FPCore (x y) :precision binary64 (if (<= x -370.0) (* (- y) x) (- (log 2.0) (* y x))))
double code(double x, double y) {
double tmp;
if (x <= -370.0) {
tmp = -y * x;
} else {
tmp = log(2.0) - (y * x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-370.0d0)) then
tmp = -y * x
else
tmp = log(2.0d0) - (y * x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -370.0) {
tmp = -y * x;
} else {
tmp = Math.log(2.0) - (y * x);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -370.0: tmp = -y * x else: tmp = math.log(2.0) - (y * x) return tmp
function code(x, y) tmp = 0.0 if (x <= -370.0) tmp = Float64(Float64(-y) * x); else tmp = Float64(log(2.0) - Float64(y * x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -370.0) tmp = -y * x; else tmp = log(2.0) - (y * x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -370.0], N[((-y) * x), $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] - N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -370:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\log 2 - y \cdot x\\
\end{array}
\end{array}
if x < -370Initial program 100.0%
Taylor expanded in x around inf 100.0%
associate-*r*100.0%
neg-mul-1100.0%
Simplified100.0%
if -370 < x Initial program 99.3%
Taylor expanded in x around 0 97.8%
Final simplification98.7%
(FPCore (x y) :precision binary64 (* (- y) x))
double code(double x, double y) {
return -y * x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -y * x
end function
public static double code(double x, double y) {
return -y * x;
}
def code(x, y): return -y * x
function code(x, y) return Float64(Float64(-y) * x) end
function tmp = code(x, y) tmp = -y * x; end
code[x_, y_] := N[((-y) * x), $MachinePrecision]
\begin{array}{l}
\\
\left(-y\right) \cdot x
\end{array}
Initial program 99.6%
Taylor expanded in x around inf 53.6%
associate-*r*53.6%
neg-mul-153.6%
Simplified53.6%
Final simplification53.6%
(FPCore (x y) :precision binary64 (* x 0.5))
double code(double x, double y) {
return x * 0.5;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * 0.5d0
end function
public static double code(double x, double y) {
return x * 0.5;
}
def code(x, y): return x * 0.5
function code(x, y) return Float64(x * 0.5) end
function tmp = code(x, y) tmp = x * 0.5; end
code[x_, y_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
Initial program 99.6%
Taylor expanded in x around 0 80.1%
Taylor expanded in y around 0 48.1%
Taylor expanded in x around inf 3.6%
*-commutative3.6%
Simplified3.6%
Final simplification3.6%
(FPCore (x y) :precision binary64 (if (<= x 0.0) (- (log (+ 1.0 (exp x))) (* x y)) (- (log (+ 1.0 (exp (- x)))) (* (- x) (- 1.0 y)))))
double code(double x, double y) {
double tmp;
if (x <= 0.0) {
tmp = log((1.0 + exp(x))) - (x * y);
} else {
tmp = log((1.0 + exp(-x))) - (-x * (1.0 - y));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 0.0d0) then
tmp = log((1.0d0 + exp(x))) - (x * y)
else
tmp = log((1.0d0 + exp(-x))) - (-x * (1.0d0 - y))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 0.0) {
tmp = Math.log((1.0 + Math.exp(x))) - (x * y);
} else {
tmp = Math.log((1.0 + Math.exp(-x))) - (-x * (1.0 - y));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 0.0: tmp = math.log((1.0 + math.exp(x))) - (x * y) else: tmp = math.log((1.0 + math.exp(-x))) - (-x * (1.0 - y)) return tmp
function code(x, y) tmp = 0.0 if (x <= 0.0) tmp = Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)); else tmp = Float64(log(Float64(1.0 + exp(Float64(-x)))) - Float64(Float64(-x) * Float64(1.0 - y))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 0.0) tmp = log((1.0 + exp(x))) - (x * y); else tmp = log((1.0 + exp(-x))) - (-x * (1.0 - y)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 0.0], N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], N[(N[Log[N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[((-x) * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0:\\
\;\;\;\;\log \left(1 + e^{x}\right) - x \cdot y\\
\mathbf{else}:\\
\;\;\;\;\log \left(1 + e^{-x}\right) - \left(-x\right) \cdot \left(1 - y\right)\\
\end{array}
\end{array}
herbie shell --seed 2024048
(FPCore (x y)
:name "Logistic regression 2"
:precision binary64
:alt
(if (<= x 0.0) (- (log (+ 1.0 (exp x))) (* x y)) (- (log (+ 1.0 (exp (- x)))) (* (- x) (- 1.0 y))))
(- (log (+ 1.0 (exp x))) (* x y)))