1
|
{"version":3,"sources":["../../../src/c/math/frexp.js"],"names":["module","exports","frexp","arg","Number","result","isFinite","absArg","Math","abs","log2","n","log","LOG2E","exp","max","floor","x","pow"],"mappings":";;AAAAA,OAAOC,OAAP,GAAiB,SAASC,KAAT,CAAgBC,GAAhB,EAAqB;AACpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEAA,QAAMC,OAAOD,GAAP,CAAN;;AAEA,MAAME,SAAS,CAACF,GAAD,EAAM,CAAN,CAAf;;AAEA,MAAIA,QAAQ,CAAR,IAAaC,OAAOE,QAAP,CAAgBH,GAAhB,CAAjB,EAAuC;AACrC,QAAMI,SAASC,KAAKC,GAAL,CAASN,GAAT,CAAf;AACA;AACA,QAAMO,OAAOF,KAAKE,IAAL,IAAa,SAASA,IAAT,CAAeC,CAAf,EAAkB;AAAE,aAAOH,KAAKI,GAAL,CAASD,CAAT,IAAcH,KAAKK,KAA1B;AAAiC,KAA/E;AACA,QAAIC,MAAMN,KAAKO,GAAL,CAAS,CAAC,IAAV,EAAgBP,KAAKQ,KAAL,CAAWN,KAAKH,MAAL,CAAX,IAA2B,CAA3C,CAAV;AACA,QAAIU,IAAIV,SAASC,KAAKU,GAAL,CAAS,CAAT,EAAY,CAACJ,GAAb,CAAjB;;AAEA;AACA;AACA,WAAOG,IAAI,GAAX,EAAgB;AACdA,WAAK,CAAL;AACAH;AACD;AACD,WAAOG,KAAK,CAAZ,EAAe;AACbA,WAAK,GAAL;AACAH;AACD;;AAED,QAAIX,MAAM,CAAV,EAAa;AACXc,UAAI,CAACA,CAAL;AACD;AACDZ,WAAO,CAAP,IAAYY,CAAZ;AACAZ,WAAO,CAAP,IAAYS,GAAZ;AACD;AACD,SAAOT,MAAP;AACD,CArED","file":"frexp.js","sourcesContent":["module.exports = function frexp (arg) {\n // discuss at: http://locutus.io/c/frexp/\n // original by: Oskar Larsson Högfeldt (http://oskar-lh.name/)\n // note 1: Instead of\n // note 1: double frexp( double arg, int* exp );\n // note 1: this is built as\n // note 1: [double, int] frexp( double arg );\n // note 1: due to the lack of pointers in JavaScript.\n // note 1: See code comments for further information.\n // example 1: frexp(1)\n // returns 1: [0.5, 1]\n // example 2: frexp(1.5)\n // returns 2: [0.75, 1]\n // example 3: frexp(3 * Math.pow(2, 500))\n // returns 3: [0.75, 502]\n // example 4: frexp(-4)\n // returns 4: [-0.5, 3]\n // example 5: frexp(Number.MAX_VALUE)\n // returns 5: [0.9999999999999999, 1024]\n // example 6: frexp(Number.MIN_VALUE)\n // returns 6: [0.5, -1073]\n // example 7: frexp(-Infinity)\n // returns 7: [-Infinity, 0]\n // example 8: frexp(-0)\n // returns 8: [-0, 0]\n // example 9: frexp(NaN)\n // returns 9: [NaN, 0]\n\n // Potential issue with this implementation:\n // the precisions of Math.pow and the ** operator are undefined in the ECMAScript standard,\n // however, sane implementations should give the same results for Math.pow(2, <integer>) operations\n\n // Like frexp of C and std::frexp of C++,\n // but returns an array instead of using a pointer argument for passing the exponent result.\n // Object.is(n, frexp(n)[0] * 2 ** frexp(n)[1]) for all number values of n except when Math.isFinite(n) && Math.abs(n) > 2**1023\n // Object.is(n, (2 * frexp(n)[0]) * 2 ** (frexp(n)[1] - 1)) for all number values of n\n // Object.is(n, frexp(n)[0]) for these values of n: 0, -0, NaN, Infinity, -Infinity\n // Math.abs(frexp(n)[0]) is >= 0.5 and < 1.0 for any other number-type value of n\n // See http://en.cppreference.com/w/c/numeric/math/frexp for a more detailed description\n\n arg = Number(arg)\n\n const result = [arg, 0]\n\n if (arg !== 0 && Number.isFinite(arg)) {\n const absArg = Math.abs(arg)\n // Math.log2 was introduced in ES2015, use it when available\n const log2 = Math.log2 || function log2 (n) { return Math.log(n) * Math.LOG2E }\n let exp = Math.max(-1023, Math.floor(log2(absArg)) + 1)\n let x = absArg * Math.pow(2, -exp)\n\n // These while loops compensate for rounding errors that sometimes occur because of ECMAScript's Math.log2's undefined precision\n // and also works around the issue of Math.pow(2, -exp) === Infinity when exp <= -1024\n while (x < 0.5) {\n x *= 2\n exp--\n }\n while (x >= 1) {\n x *= 0.5\n exp++\n }\n\n if (arg < 0) {\n x = -x\n }\n result[0] = x\n result[1] = exp\n }\n return result\n}\n"]}
|