Skip to content

relative error is too big for Math.expm1 and Math.log1p #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Yaffle opened this issue Jan 13, 2015 · 6 comments
Closed

relative error is too big for Math.expm1 and Math.log1p #314

Yaffle opened this issue Jan 13, 2015 · 6 comments

Comments

@Yaffle
Copy link
Contributor

Yaffle commented Jan 13, 2015

Math.expm1(-2e-17)

Math.log1p(-2e-17)

@ljharb
Copy link
Collaborator

ljharb commented Jan 13, 2015

The spec doesn't tend to define precision very well for its Math operations. Can you link to something in the spec that's incorrect here? What values do you expect? In what browser(s) are you getting which values that you don't expect?

@Yaffle
Copy link
Contributor Author

Yaffle commented Jan 13, 2015

@ljharb , so why not to use Math.exp(x) - 1 or Math.log(1 + x) ?
You can define Math.expm1 = function (){} according to the spec, but does it make your shim good?

compat-table/compat-table#392

@ljharb
Copy link
Collaborator

ljharb commented Jan 13, 2015

I'm still not understanding what this issue is about. All three of the variables referenced in the compat-table issue return true for me in Chrome with or without the es6-shim (since they're native). In which browsers are you seeing any of these return false?

Also, if the spec does not explicitly say that your error criteria must be applied, then they need not be applied by this shim or by any browsers. I'm definitely open to making them more precise, but the shims are "good" so long as they provide any guarantees the spec requires of them, nothing more.

@Yaffle Yaffle closed this as completed Jan 13, 2015
@ljharb
Copy link
Collaborator

ljharb commented Jan 16, 2015

Thanks for your examples - I'm sorry we continue to have misunderstandings in these issues, I hope we can chalk it up to language barriers.

I've added failing tests for log1p, and corrected them, in the es6-shim. While our number 1 priority is to comply with the spec, number 2 is, when it doesn't conflict with the spec, to comply with the majority of implementations - in this case, both of these math functions aren't returning the same values as their native counterparts.

I can't locate a polyfill for expm1 that's not exp(1) - 1, and that polyfill has the error you described. Do you have a suggested implementation?

@ljharb ljharb reopened this Jan 16, 2015
@ljharb ljharb closed this as completed in 3ea64bf Jan 16, 2015
@ljharb ljharb self-assigned this Jan 16, 2015
@Yaffle
Copy link
Contributor Author

Yaffle commented Jan 16, 2015

I have only this(using Taylor series expansion):

Math.expm1 = function (x) {
  if (x !== x || x === 0) {
    return x;
  }
  if (Math.abs(x) > 1 / 2) {
    return Math.exp(x) - 1;
  }
  var t = x;
  var s = 0;
  var n = 1;
  while (s + t !== s) {
    s += t;
    n += 1;
    t *= x;
    t /= n;
  }
  return s;
};

@ljharb
Copy link
Collaborator

ljharb commented Jan 16, 2015

@Yaffle Thanks, that seems to do it! Can you suggest more meaningful variable names than "t", "s", and "n"?

ljharb added a commit that referenced this issue Jan 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants