5 lines
1.8 KiB
JavaScript
5 lines
1.8 KiB
JavaScript
|
(function(h,g){"object"===typeof exports&&"undefined"!==typeof module?g(exports):"function"===typeof define&&define.amd?define(["exports"],g):(h=h||self,g(h.IEEE754Buffer={}))})(this,function(h){function g(e){var a=Math.floor(e);e-=a;return.5>e?a:.5<e?a+1:a%2?a+1:a}var k=function(e,a){this.ebits=e;this.fbits=a;this.bias=(1<<e-1)-1;this.numBytes=Math.ceil((e+a)/8);this.biasP2=Math.pow(2,this.bias+1);this.ebitsFbits=e+a;this.fbias=Math.pow(2,-(8*this.numBytes-1-e))};k.prototype.pack=function(e,a,f){if("number"!=
|
||
|
typeof a)throw new TypeError;Math.abs(a)>this.biasP2-2*this.ebitsFbits&&(a=0>a?-Infinity:Infinity);var d=0>((a=+a)||1/a)?1:0>a?1:0;a=Math.abs(a);var c=Math.min(Math.floor(Math.log(a)/Math.LN2),1023),b=g(a/Math.pow(2,c)*Math.pow(2,this.fbits));a!==a?(b=Math.pow(2,this.fbits-1),c=(1<<this.ebits)-1):0!==a&&(a>=Math.pow(2,1-this.bias)?(2<=b/Math.pow(2,this.fbits)&&(c+=1,b=1),c>this.bias?(c=(1<<this.ebits)-1,b=0):(c+=this.bias,b=g(b)-Math.pow(2,this.fbits))):(b=g(a/Math.pow(2,1-this.bias-this.fbits)),
|
||
|
c=0));return this.packFloatBits_(e,f,d,c,b)};k.prototype.unpack=function(e,a){for(var f=(1<<this.ebits)-1,d="",c=this.numBytes-1;0<=c;c--){var b=e[c+a].toString(2);d+="00000000".substring(b.length)+b}c="1"==d.charAt(0)?-1:1;d=d.substring(1);b=parseInt(d.substring(0,this.ebits),2);d=d.substring(this.ebits);if(b==f)return 0!==parseInt(d,2)?NaN:Infinity*c;0===b?(b+=1,f=parseInt(d,2)):f=parseInt("1"+d,2);return c*f*this.fbias*Math.pow(2,b-this.bias)};k.prototype.packFloatBits_=function(e,a,f,d,c){var b=
|
||
|
[];b.push(f);for(f=this.ebits;0<f;--f)b[f]=d%2?1:0,d=Math.floor(d/2);d=b.length;for(f=this.fbits;0<f;--f)b[d+f]=c%2?1:0,c=Math.floor(c/2);c=b.join("");b=this.numBytes+a-1;for(d=a;b>=a;)e[b]=parseInt(c.substring(0,8),2),c=c.substring(8),b--,d++;return d};h.IEEE754Buffer=k;Object.defineProperty(h,"__esModule",{value:!0})});
|