Source: polyfill/Math.js

  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Watson Analytics
  4. //
  5. // (C) Copyright IBM Corp. 2015
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or
  8. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. ( function( Math, ObjectPolyfill )
  10. {
  11. "use strict";
  12. // This module polyfills bitwise operations.
  13. /*jshint bitwise:false*/
  14. /**
  15. * Polyfills for {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math Math}.
  16. * @class module:barejs/polyfill.Math
  17. */
  18. /** @lends module:barejs/polyfill.Math */
  19. var stat = {};
  20. /*istanbul ignore else: We test with __ES__ set to 3*/
  21. if ( __ES__ < 6 )
  22. {
  23. /**
  24. * The Math.cbrt() function returns the cube root of a number.
  25. * @param {number} _x The number to get the cube root of.
  26. * @returns {number} The cube root of the number
  27. */
  28. stat.cbrt = function cbrt( _x )
  29. {
  30. var y = Math.pow( Math.abs( _x ), 1/3 );
  31. return _x < 0 ? -y : y;
  32. };
  33. /**
  34. * The Math.expm1() function returns e^x - 1, where x is the argument, and e the base of the natural logarithms.
  35. * @param {number} _x The number.
  36. * @returns {number} A number representing e^x - 1, where e is Euler's number and x is the argument.
  37. */
  38. stat.expm1 = function expm1( _x )
  39. {
  40. return Math.exp( _x ) - 1;
  41. };
  42. /**
  43. * The Math.hypot() function returns the square root of the sum of squares of its arguments.
  44. * @returns {number} The square root of the sum of squares of its arguments.
  45. */
  46. stat.hypot = function hypot( _a, _b/*, ...*/ )
  47. {
  48. for ( var i = 0, len = arguments.length, y = 0, v; i < len; ++i )
  49. {
  50. v = Math.abs( arguments[i] );
  51. if ( v === Infinity )
  52. return v;
  53. y += v * v;
  54. }
  55. return Math.sqrt( y );
  56. };
  57. /**
  58. * The Math.imul() function returns the result of the C-like 32-bit multiplication of the two parameters.
  59. * @param {number} _a Operand A.
  60. * @param {number} _b Operand B.
  61. * @returns {number} The result of the multiplication.
  62. */
  63. stat.imul = function imul( _a, _b )
  64. {
  65. var ah = ( _a >>> 16 ) & 0xffff;
  66. var al = _a & 0xffff;
  67. var bh = ( _b >>> 16 ) & 0xffff;
  68. var bl = _b & 0xffff;
  69. // the shift by 0 fixes the sign on the high part
  70. // the final |0 converts the unsigned value into a signed value
  71. return ( ( al * bl ) + ( ( ( ah * bl + al * bh ) << 16 ) >>> 0 ) | 0 );
  72. };
  73. ( function()
  74. {
  75. var table = [
  76. 32, 31, 0, 16, 0, 30, 3, 0, 15, 0, 0, 0, 29, 10, 2, 0,
  77. 0, 0, 12, 14, 21, 0, 19, 0, 0, 28, 0, 25, 0, 9, 1, 0,
  78. 17, 0, 4, undefined, 0, 0, 11, 0, 13, 22, 20, 0, 26, 0, 0, 18,
  79. 5, 0, 0, 23, 0, 27, 0, 6, 0, 24, 7, 0, 8, 0, 0, 0];
  80. /**
  81. * The Math.clz32() function returns the number of leading zero bits in the 32-bit binary representation of a number.
  82. * "clz32" is short for CountLeadingZeroes32.
  83. * @param {number} _x
  84. * @returns {number}
  85. */
  86. stat.clz32 = function clz32( _x )
  87. {
  88. var v = _x >>> 0; // convert to unsigned integer
  89. v |= v >>> 1;
  90. v |= v >>> 2;
  91. v |= v >>> 4;
  92. v |= v >>> 8;
  93. v |= v >>> 16;
  94. return table[ Math.imul( v, 0x06EB14F9 ) >>> 26 ];
  95. };
  96. }() );
  97. /**
  98. * The Math.log1p() function returns the natural logarithm (base e) of 1 + a number.
  99. * @param {number} _x The value to get the logarithm of.
  100. * @returns {number} The logarithm of 1 + _x.
  101. */
  102. stat.log1p = function log1p( _x )
  103. {
  104. return Math.log( 1 + _x );
  105. };
  106. /**
  107. * The Math.log2() function returns the base 2 logarithm of a number.
  108. * @param {number} _x The value to get the base 2 logarithm of.
  109. * @returns {number} The base 2 logarithm of _x.
  110. */
  111. stat.log2 = function log2( _x )
  112. {
  113. return Math.log( _x ) / Math.LN2;
  114. };
  115. /**
  116. * The Math.log10() function returns the base 10 logarithm of a number.
  117. * Note that this polyfill is subject to rounding errors.
  118. * @param {number} _x The value to get the base 10 logarithm of.
  119. * @returns {number} The base 10 logarithm of _x.
  120. */
  121. stat.log10 = function log10( _x )
  122. {
  123. return Math.log( _x ) / Math.LN10;
  124. };
  125. /**
  126. * The Math.cosh() function returns the hyperbolic cosine of a number.
  127. * @param {number} _x The value to calculate the cosine of.
  128. * @returns {number} The hyperbolic cosine.
  129. */
  130. stat.cosh = function cosh( _x )
  131. {
  132. var y = Math.exp( _x );
  133. return ( y + 1 / y ) / 2;
  134. };
  135. /**
  136. * The Math.acosh() function returns the hyperbolic arc-cosine of a number.
  137. * @param {number} _x The value to calculate the arc-cosine of.
  138. * @returns {number} The hyperbolic arc-cosine.
  139. */
  140. stat.acosh = function acosh( _x )
  141. {
  142. return Math.log( _x + Math.sqrt( _x * _x - 1 ) );
  143. };
  144. /**
  145. * The Math.sinh() function returns the hyperbolic sine of a number.
  146. * @param {number} _x The value to calculate the sine of.
  147. * @returns {number} The hyperbolic sine.
  148. */
  149. stat.sinh = function sinh( _x )
  150. {
  151. var y = Math.exp( _x );
  152. return ( y - 1 / y ) / 2;
  153. };
  154. /**
  155. * The Math.asinh() function returns the hyperbolic arcsine of a number.
  156. * @param {number} _x The value to calculate the arcsine of.
  157. * @returns {number} The hyperbolic arcsine.
  158. */
  159. stat.asinh = function asinh( _x )
  160. {
  161. if ( _x === -Infinity )
  162. return _x;
  163. else
  164. return Math.log( _x + Math.sqrt( _x * _x + 1 ) );
  165. };
  166. /**
  167. * The Math.tanh() function returns the hyperbolic tangent of a number.
  168. * @param {number} _x The value to calculate the tangent of.
  169. * @returns {number} The hyperbolic tangent.
  170. */
  171. stat.tanh = function tanh( _x )
  172. {
  173. var y;
  174. if ( _x === Infinity )
  175. return 1;
  176. else if ( _x === -Infinity )
  177. return -1;
  178. else
  179. return ( ( y = Math.exp( 2 * _x ) ) - 1) / ( y + 1 );
  180. };
  181. /**
  182. * The Math.atanh() function returns the hyperbolic arctangent of a number.
  183. * @param {number} _x The value to calculate the arctangent of.
  184. * @returns {number} The hyperbolic arctangent.
  185. */
  186. stat.atanh = function atanh( _x )
  187. {
  188. return Math.log( ( 1 + _x ) / ( 1 - _x ) ) / 2;
  189. };
  190. /**
  191. * The Math.sign() function returns the sign of a number, indicating whether the number is positive, negative or zero.
  192. * @param {number} _x The number to check
  193. * @returns {number} This function has 5 kinds of return values, 1, -1, 0, -0, NaN, which represent
  194. * "positive number", "negative number", "positive zero", "negative zero" and NaN respectively.
  195. */
  196. stat.sign = function sign( _x )
  197. {
  198. _x = +_x; // convert to a number
  199. if ( _x === 0 || isNaN( _x ) )
  200. return _x;
  201. return _x > 0 ? 1 : -1;
  202. };
  203. /**
  204. * Unlike other three Math methods: Math.floor(), Math.ceil() and Math.round(), the way
  205. * Math.trunc() works is very simple and straightforward, just truncate the dot and the digits
  206. * behind it, no matter whether the argument is a positive number or a negative number.
  207. * @param {number} _x The number to truncate.
  208. * @returns {number} The truncated number
  209. */
  210. stat.trunc = function trunc( _x )
  211. {
  212. return _x < 0 ? Math.ceil( _x ) : Math.floor( _x );
  213. };
  214. // End of ES6 polyfill scope
  215. }
  216. ObjectPolyfill.polyfill( Math, stat, null, exports, "Math" );
  217. // End of module
  218. }( Math, require( "./Object" ) ) );