polyfill_Math.js.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/Math.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: polyfill/Math.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
  20. //
  21. // IBM Watson Analytics
  22. //
  23. // (C) Copyright IBM Corp. 2015
  24. //
  25. // US Government Users Restricted Rights - Use, duplication or
  26. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  27. ( function( Math, ObjectPolyfill )
  28. {
  29. "use strict";
  30. // This module polyfills bitwise operations.
  31. /*jshint bitwise:false*/
  32. /**
  33. * Polyfills for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math Math}.
  34. * @class module:barejs/polyfill.Math
  35. */
  36. /** @lends module:barejs/polyfill.Math */
  37. var stat = {};
  38. /*istanbul ignore else: We test with __ES__ set to 3*/
  39. if ( __ES__ &lt; 6 )
  40. {
  41. /**
  42. * The Math.cbrt() function returns the cube root of a number.
  43. * @param {number} _x The number to get the cube root of.
  44. * @returns {number} The cube root of the number
  45. */
  46. stat.cbrt = function cbrt( _x )
  47. {
  48. var y = Math.pow( Math.abs( _x ), 1/3 );
  49. return _x &lt; 0 ? -y : y;
  50. };
  51. /**
  52. * The Math.expm1() function returns e^x - 1, where x is the argument, and e the base of the natural logarithms.
  53. * @param {number} _x The number.
  54. * @returns {number} A number representing e^x - 1, where e is Euler's number and x is the argument.
  55. */
  56. stat.expm1 = function expm1( _x )
  57. {
  58. return Math.exp( _x ) - 1;
  59. };
  60. /**
  61. * The Math.hypot() function returns the square root of the sum of squares of its arguments.
  62. * @returns {number} The square root of the sum of squares of its arguments.
  63. */
  64. stat.hypot = function hypot( _a, _b/*, ...*/ )
  65. {
  66. for ( var i = 0, len = arguments.length, y = 0, v; i &lt; len; ++i )
  67. {
  68. v = Math.abs( arguments[i] );
  69. if ( v === Infinity )
  70. return v;
  71. y += v * v;
  72. }
  73. return Math.sqrt( y );
  74. };
  75. /**
  76. * The Math.imul() function returns the result of the C-like 32-bit multiplication of the two parameters.
  77. * @param {number} _a Operand A.
  78. * @param {number} _b Operand B.
  79. * @returns {number} The result of the multiplication.
  80. */
  81. stat.imul = function imul( _a, _b )
  82. {
  83. var ah = ( _a >>> 16 ) &amp; 0xffff;
  84. var al = _a &amp; 0xffff;
  85. var bh = ( _b >>> 16 ) &amp; 0xffff;
  86. var bl = _b &amp; 0xffff;
  87. // the shift by 0 fixes the sign on the high part
  88. // the final |0 converts the unsigned value into a signed value
  89. return ( ( al * bl ) + ( ( ( ah * bl + al * bh ) &lt;&lt; 16 ) >>> 0 ) | 0 );
  90. };
  91. ( function()
  92. {
  93. var table = [
  94. 32, 31, 0, 16, 0, 30, 3, 0, 15, 0, 0, 0, 29, 10, 2, 0,
  95. 0, 0, 12, 14, 21, 0, 19, 0, 0, 28, 0, 25, 0, 9, 1, 0,
  96. 17, 0, 4, undefined, 0, 0, 11, 0, 13, 22, 20, 0, 26, 0, 0, 18,
  97. 5, 0, 0, 23, 0, 27, 0, 6, 0, 24, 7, 0, 8, 0, 0, 0];
  98. /**
  99. * The Math.clz32() function returns the number of leading zero bits in the 32-bit binary representation of a number.
  100. * "clz32" is short for CountLeadingZeroes32.
  101. * @param {number} _x
  102. * @returns {number}
  103. */
  104. stat.clz32 = function clz32( _x )
  105. {
  106. var v = _x >>> 0; // convert to unsigned integer
  107. v |= v >>> 1;
  108. v |= v >>> 2;
  109. v |= v >>> 4;
  110. v |= v >>> 8;
  111. v |= v >>> 16;
  112. return table[ Math.imul( v, 0x06EB14F9 ) >>> 26 ];
  113. };
  114. }() );
  115. /**
  116. * The Math.log1p() function returns the natural logarithm (base e) of 1 + a number.
  117. * @param {number} _x The value to get the logarithm of.
  118. * @returns {number} The logarithm of 1 + _x.
  119. */
  120. stat.log1p = function log1p( _x )
  121. {
  122. return Math.log( 1 + _x );
  123. };
  124. /**
  125. * The Math.log2() function returns the base 2 logarithm of a number.
  126. * @param {number} _x The value to get the base 2 logarithm of.
  127. * @returns {number} The base 2 logarithm of _x.
  128. */
  129. stat.log2 = function log2( _x )
  130. {
  131. return Math.log( _x ) / Math.LN2;
  132. };
  133. /**
  134. * The Math.log10() function returns the base 10 logarithm of a number.
  135. * Note that this polyfill is subject to rounding errors.
  136. * @param {number} _x The value to get the base 10 logarithm of.
  137. * @returns {number} The base 10 logarithm of _x.
  138. */
  139. stat.log10 = function log10( _x )
  140. {
  141. return Math.log( _x ) / Math.LN10;
  142. };
  143. /**
  144. * The Math.cosh() function returns the hyperbolic cosine of a number.
  145. * @param {number} _x The value to calculate the cosine of.
  146. * @returns {number} The hyperbolic cosine.
  147. */
  148. stat.cosh = function cosh( _x )
  149. {
  150. var y = Math.exp( _x );
  151. return ( y + 1 / y ) / 2;
  152. };
  153. /**
  154. * The Math.acosh() function returns the hyperbolic arc-cosine of a number.
  155. * @param {number} _x The value to calculate the arc-cosine of.
  156. * @returns {number} The hyperbolic arc-cosine.
  157. */
  158. stat.acosh = function acosh( _x )
  159. {
  160. return Math.log( _x + Math.sqrt( _x * _x - 1 ) );
  161. };
  162. /**
  163. * The Math.sinh() function returns the hyperbolic sine of a number.
  164. * @param {number} _x The value to calculate the sine of.
  165. * @returns {number} The hyperbolic sine.
  166. */
  167. stat.sinh = function sinh( _x )
  168. {
  169. var y = Math.exp( _x );
  170. return ( y - 1 / y ) / 2;
  171. };
  172. /**
  173. * The Math.asinh() function returns the hyperbolic arcsine of a number.
  174. * @param {number} _x The value to calculate the arcsine of.
  175. * @returns {number} The hyperbolic arcsine.
  176. */
  177. stat.asinh = function asinh( _x )
  178. {
  179. if ( _x === -Infinity )
  180. return _x;
  181. else
  182. return Math.log( _x + Math.sqrt( _x * _x + 1 ) );
  183. };
  184. /**
  185. * The Math.tanh() function returns the hyperbolic tangent of a number.
  186. * @param {number} _x The value to calculate the tangent of.
  187. * @returns {number} The hyperbolic tangent.
  188. */
  189. stat.tanh = function tanh( _x )
  190. {
  191. var y;
  192. if ( _x === Infinity )
  193. return 1;
  194. else if ( _x === -Infinity )
  195. return -1;
  196. else
  197. return ( ( y = Math.exp( 2 * _x ) ) - 1) / ( y + 1 );
  198. };
  199. /**
  200. * The Math.atanh() function returns the hyperbolic arctangent of a number.
  201. * @param {number} _x The value to calculate the arctangent of.
  202. * @returns {number} The hyperbolic arctangent.
  203. */
  204. stat.atanh = function atanh( _x )
  205. {
  206. return Math.log( ( 1 + _x ) / ( 1 - _x ) ) / 2;
  207. };
  208. /**
  209. * The Math.sign() function returns the sign of a number, indicating whether the number is positive, negative or zero.
  210. * @param {number} _x The number to check
  211. * @returns {number} This function has 5 kinds of return values, 1, -1, 0, -0, NaN, which represent
  212. * "positive number", "negative number", "positive zero", "negative zero" and NaN respectively.
  213. */
  214. stat.sign = function sign( _x )
  215. {
  216. _x = +_x; // convert to a number
  217. if ( _x === 0 || isNaN( _x ) )
  218. return _x;
  219. return _x > 0 ? 1 : -1;
  220. };
  221. /**
  222. * Unlike other three Math methods: Math.floor(), Math.ceil() and Math.round(), the way
  223. * Math.trunc() works is very simple and straightforward, just truncate the dot and the digits
  224. * behind it, no matter whether the argument is a positive number or a negative number.
  225. * @param {number} _x The number to truncate.
  226. * @returns {number} The truncated number
  227. */
  228. stat.trunc = function trunc( _x )
  229. {
  230. return _x &lt; 0 ? Math.ceil( _x ) : Math.floor( _x );
  231. };
  232. // End of ES6 polyfill scope
  233. }
  234. ObjectPolyfill.polyfill( Math, stat, null, exports, "Math" );
  235. // End of module
  236. }( Math, require( "./Object" ) ) );
  237. </code></pre>
  238. </article>
  239. </section>
  240. </div>
  241. <nav>
  242. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
  243. </nav>
  244. <br class="clear">
  245. <footer>
  246. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:34 GMT+0200 (W. Europe Daylight Time)
  247. </footer>
  248. <script> prettyPrint(); </script>
  249. <script src="scripts/linenumber.js"> </script>
  250. </body>
  251. </html>