Problem · Math
Handwritten Sigmoid
Learn this problemProblem statement
Given a finite double x, return its logistic sigmoid:
sigmoid(x) = 1 / (1 + exp(-x))
Implement the computation directly with standard scalar arithmetic and the standard exponential function. Do not call a machine-learning or numerical-array library.
Function
sigmoid(x: double) → doubleExamples
Example 1
x = 0.0return = 0.5At x = 0, exp(0) = 1, so the result is 1 / 2.
Example 2
x = 2.0return = 0.8807970779778823Substituting 2 into the logistic formula gives approximately 0.8807970779778823.
Example 3
x = -2.0return = 0.11920292202211755The logistic values at opposite inputs sum to 1, so this is 1 - sigmoid(2).
Constraints
-1000 <= x <= 1000xis finite: it is not NaN or infinity.- The result is compared with absolute tolerance
1e-12.