Skip to content

Instantly share code, notes, and snippets.

@Yaffle
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yaffle/3c0a9a6d1fb535e24b21 to your computer and use it in GitHub Desktop.
Save Yaffle/3c0a9a6d1fb535e24b21 to your computer and use it in GitHub Desktop.
// (Math.exp(704.6589) / Math.expm1(704.6589) - 1) / Number.EPSILON
var isExpOK = Math.exp(704.6589) >= 1.0702171200481775e+306 * (1 - 16 * Math.pow(2, -52)) &&
Math.exp(704.6589) <= 1.0702171200481775e+306 * (1 + 16 * Math.pow(2, -52));
// (Math.exp(704.6589) / Math.expm1(704.6589) - 1) / Number.EPSILON
var isExpOK = Math.exp(704.6589) >= 1.0702171200481775e+306 * (1 - 16 * Math.pow(2, -52)) &&
Math.exp(704.6589) <= 1.0702171200481775e+306 * (1 + 16 * Math.pow(2, -52));
Math.exp = function (x) {
var hi = 0.6931471805598903;
var lo = 5.497923018708371e-14;
var k = Math.floor(x / hi + 0.5);
var t = Math.pow(2, k);
var k2 = 0;
if (t === 0) {
k2 -= 1;
}
if (t === Infinity) {
k2 += 1;
}
t = Math.pow(2, k - k2);
if (t === 0 || t === Infinity) {
return t;
}
var r = x - k * hi - k * lo;
return (Math.expm1(r) + 1) * t * Math.pow(2, k2);
};
// 709.4758600739439
var test2 = function (v) {
console.log((Math.exp(v)/nativeExp(v) - 1)/Number.EPSILON);
};
var test = function (v) {
test2(v * (1 - Math.floor(4) * Number.EPSILON));
test2(v);
test2(v * (1 + Math.floor(4) * Number.EPSILON));
};
test(708);
test(-708);
Math.sqrt = function (value) {
if (value < 0) {
return NaN;
}
if (value === 0) {
return value;
}
if (value === Infinity) {
return Infinity;
}
var abs = Math.abs(value);
var result = Math.exp(Math.log(abs) / 2);
result = (abs / result + result) / 2;
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment