26

I need a function similar to Log but it should produce numbers between 0 and 1 Something like:

f(0)=0
f(1)=0.1
f(2)=0.15
f(3)=0.17
f(100)=0.8
f(1000)=0.95
f(1000000000)=0.99999999

I need this in my program that I am programming and I can use only standard functions like log, exp, etc... Any help would be appreciated.

410503
  • 371

3 Answers3

25

Sasha's suggestion of $f(x) = 1-\exp(-x)$ is good, but doesn't fit your example values too well, even if you scale $x$ appropriately. However, some other similar function, such as $f(x) = 1-1/(1+x)$, might work even better.

Jonas Meyer
  • 53,602
  • 19
    In case you're using this, you can generalize the example to $f(x) = x/(a+x)$; this way, you can adjust $a$ to have a slower or faster growing curve. – Gerben Aug 14 '11 at 19:03
  • How would one scale either of those functions to get the value 1 at some predefined x, say x=100? – ed22 Aug 27 '20 at 09:35
  • 1
    @ed22: Both of these functions will asymptotically approach 1 as $x$ grows but will never reach it, like the OP asked for. Of course, you could just define $g(x) = f(x) \mathbin/ f(100)$ so that $g(100) = 1$, with $g(x)$ then approaching an arbitrary asymptote of $1/f(100)$ as $x$ grows further beyond $100$. But depending on how you want $g(x)$ to behave for $x$ between $0$ and $100$, that may not actually do what you want. If you can describe the behavior you want, you could maybe ask a new question about it. – Ilmari Karonen Aug 27 '20 at 10:12
9

I like the hyperbolic tan (and it likes me) $$f(x) = \tanh(x) = \frac{e^x-e^{-x}}{e^x+e^{-x}} = \frac{e^{2x}-1}{e^{2x}+1} = 1 - \frac{2}{e^{2x}+1}$$

$f$ is strictly increasing and satisfies $f'(0) = 1$, $f(0) = 0$, $f(\infty) = 1$.

If you want to map $(-\infty, \infty)$ into $[0, 1)$ (instead of $[0, \infty)$), use $$f(x) = \frac{\tanh(x)+1}{2} = 1 - \frac{1}{e^{2x}+1}.$$

I have seen this called the "logistic" curve, or "s-shaped" curve.

marty cohen
  • 107,799
8

If you want it to involve log, try $f(x) = 1 - \log(a)/\log(a+bx)$ for suitable positive numbers $a$ and $b$.

Did
  • 279,727
Robert Israel
  • 448,999