---
layout: page
title: "JavaScript log1p function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/log1p:783
- /functions/view/log1p
- /functions/view/783
- /functions/log1p:783
- /functions/783
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's log1p

{% codeblock math/log1p.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/log1p.js raw on github %}
function log1p (x) {
  // From: http://phpjs.org/functions
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %          note 1: Precision 'n' can be adjusted as desired
  // *     example 1: log1p(1e-15);
  // *     returns 1: 9.999999999999995e-16

  var ret = 0,
    n = 50; // degree of precision
  if (x <= -1) {
    return '-INF'; // JavaScript style would be to return Number.NEGATIVE_INFINITY
  }
  if (x < 0 || x > 1) {
    return Math.log(1 + x);
  }
  for (var i = 1; i < n; i++) {
    if ((i % 2) === 0) {
      ret -= Math.pow(x, i) / i;
    } else {
      ret += Math.pow(x, i) / i;
    }
  }
  return ret;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/log1p.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/log1p.js)

### Example 1
This code
{% codeblock lang:js example %}
log1p(1e-15);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
9.999999999999995e-16
{% endcodeblock %}


### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}
