Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jslint bitwise: true */
  2. 'use strict';
  3.  
  4. (function() {
  5.     return {
  6.         abs: Math.abs,
  7.         acos: Math.acos,
  8.         asin: Math.asin,
  9.         atan: Math.atan,
  10.         atan2: Math.atan2,
  11.         ceil: Math.ceil,
  12.         cos: Math.cos,
  13.         cosh: function(x) {
  14.             var y = Math.exp(x);
  15.             return (y + 1 / y) / 2;
  16.         },
  17.         deg: function(x) {
  18.             return x / (Math.PI / 180);
  19.         },
  20.         exp: Math.exp,
  21.         floor: Math.floor,
  22.         fmod: function(x, y) {
  23.             return Number((x - (Math.floor(x / y) * y)).toPrecision(8));
  24.         },
  25.         // Article: http://croquetweak.blogspot.com/2014/08/deconstructing-floats-frexp-and-ldexp.html
  26.         // https://github.com/bertfreudenberg/SqueakJS/commit/e4d60ea16cc1d9868872dde6033e0bbf5166bd6f
  27.         frexp: function(x) {
  28.             if (x === 0.0) return 0;
  29.             var data = new DataView(new ArrayBuffer(8));
  30.             data.setFloat64(0, x);
  31.             var bits = (data.getUint32(0) >>> 20) & 0x7FF;
  32.             if (bits === 0) {
  33.                 data.setFloat64(0, x * Math.pow(2, 64));
  34.                 bits = ((data.getUint32(0) >>> 20) & 0x7FF) - 64;
  35.             }
  36.             var e = bits - 1022;
  37.             var m = this.ldexp(x, -e);
  38.             return [m, e];
  39.         },
  40.         huge: Infinity,
  41.         // Article: http://croquetweak.blogspot.com/2014/08/deconstructing-floats-frexp-and-ldexp.html
  42.         // https://github.com/bertfreudenberg/SqueakJS/commit/e4d60ea16cc1d9868872dde6033e0bbf5166bd6f
  43.         ldexp: function(m, e) {
  44.             return e <= 1023 ?
  45.               m * Math.pow(2, e) :
  46.                 m * Math.pow(2, 1023) * Math.pow(2, e - 1023);
  47.         },
  48.         log: Math.log,
  49.         log10: function(x) {
  50.             return Math.log(x) / Math.LN10;
  51.         },
  52.         max: Math.max,
  53.         min: Math.min,
  54.         modf: function(x) {
  55.             return [Math.floor(x), x % 1];
  56.         },
  57.         pi: Math.PI,
  58.         pow: Math.pow,
  59.         rad: function(x) {
  60.             return x * (Math.PI / 180);
  61.         },
  62.         random: function(m, n) {
  63.             var r = Math.random();
  64.             var l, u;
  65.             if (m && n) {
  66.                             l = m;
  67.               u = n;
  68.               return Math.floor(r * (u - l + 1) + l);
  69.             } else if (m) {
  70.               u = m;
  71.               return Math.floor(r * u) + 1;
  72.             } else {
  73.               return r;
  74.             }
  75.         },
  76.         // Does nothing
  77.         randomseed: function(){},
  78.         sin: Math.sin,
  79.         sinh: function(x) {
  80.             var y = Math.exp(x);
  81.             return (y - 1/y) / 2;
  82.         },
  83.         sqrt: Math.sqrt,
  84.         tan: Math.tan,
  85.         tanh: function(x) {
  86.             if(x === Infinity) {
  87.                 return 1;
  88.             } else if(x === -Infinity) {
  89.                 return -1;
  90.             } else {
  91.                 var y = Math.exp(2 * x);
  92.                 return (y - 1) / (y + 1);
  93.             }
  94.         }
  95.     };
  96. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement