Module: Math
- Defined in:
 - opal/opal/corelib/math.rb
 
Constant Summary
- E =
 `Math.E`
- PI =
 `Math.PI`
- DomainError =
 Class.new(StandardError)
Class Method Summary collapse
- .acos(x) ⇒ Object
 - .acosh(x) ⇒ Object
 - .asin(x) ⇒ Object
 - .asinh(x) ⇒ Object
 - .atan(x) ⇒ Object
 - .atan2(y, x) ⇒ Object
 - .atanh(x) ⇒ Object
 - .cbrt(x) ⇒ Object
 - .checked(method, *args) ⇒ Object
 - .cos(x) ⇒ Object
 - .cosh(x) ⇒ Object
 - .erf(x) ⇒ Object
 - .erfc(x) ⇒ Object
 - .exp(x) ⇒ Object
 - .float!(value) ⇒ Object
 - .frexp(x) ⇒ Object
 - .gamma(n) ⇒ Object
 - .hypot(x, y) ⇒ Object
 - .integer!(value) ⇒ Object
 - .ldexp(mantissa, exponent) ⇒ Object
 - .lgamma(n) ⇒ Object
 - .log(x, base = undefined) ⇒ Object
 - .log10(x) ⇒ Object
 - .log2(x) ⇒ Object
 - .sin(x) ⇒ Object
 - .sinh(x) ⇒ Object
 - .sqrt(x) ⇒ Object
 - .tan(x) ⇒ Object
 - .tanh(x) ⇒ Object
 
Class Method Details
.acos(x) ⇒ Object
      37 38 39  | 
    
      # File 'opal/opal/corelib/math.rb', line 37 def acos(x) Math.checked :acos, Math.float!(x) end  | 
  
.acosh(x) ⇒ Object
      49 50 51  | 
    
      # File 'opal/opal/corelib/math.rb', line 49 def acosh(x) Math.checked :acosh, Math.float!(x) end  | 
  
.asin(x) ⇒ Object
      53 54 55  | 
    
      # File 'opal/opal/corelib/math.rb', line 53 def asin(x) Math.checked :asin, Math.float!(x) end  | 
  
.asinh(x) ⇒ Object
      65 66 67  | 
    
      # File 'opal/opal/corelib/math.rb', line 65 def asinh(x) Math.checked :asinh, Math.float!(x) end  | 
  
.atan(x) ⇒ Object
      69 70 71  | 
    
      # File 'opal/opal/corelib/math.rb', line 69 def atan(x) Math.checked :atan, Math.float!(x) end  | 
  
.atan2(y, x) ⇒ Object
      73 74 75  | 
    
      # File 'opal/opal/corelib/math.rb', line 73 def atan2(y, x) Math.checked :atan2, Math.float!(y), Math.float!(x) end  | 
  
.atanh(x) ⇒ Object
      85 86 87  | 
    
      # File 'opal/opal/corelib/math.rb', line 85 def atanh(x) Math.checked :atanh, Math.float!(x) end  | 
  
.cbrt(x) ⇒ Object
      135 136 137  | 
    
      # File 'opal/opal/corelib/math.rb', line 135 def cbrt(x) Math.checked :cbrt, Math.float!(x) end  | 
  
.checked(method, *args) ⇒ Object
      7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  | 
    
      # File 'opal/opal/corelib/math.rb', line 7 def self.checked(method, *args) %x{ if (isNaN(args[0]) || (args.length == 2 && isNaN(args[1]))) { return NaN; } var result = Math[method].apply(null, args); if (isNaN(result)) { #{raise DomainError, "Numerical argument is out of domain - \"#{method}\""}; } return result; } end  | 
  
.cos(x) ⇒ Object
      139 140 141  | 
    
      # File 'opal/opal/corelib/math.rb', line 139 def cos(x) Math.checked :cos, Math.float!(x) end  | 
  
.cosh(x) ⇒ Object
      151 152 153  | 
    
      # File 'opal/opal/corelib/math.rb', line 151 def cosh(x) Math.checked :cosh, Math.float!(x) end  | 
  
.erf(x) ⇒ Object
      181 182 183  | 
    
      # File 'opal/opal/corelib/math.rb', line 181 def erf(x) Math.checked :erf, Math.float!(x) end  | 
  
.erfc(x) ⇒ Object
      214 215 216  | 
    
      # File 'opal/opal/corelib/math.rb', line 214 def erfc(x) Math.checked :erfc, Math.float!(x) end  | 
  
.exp(x) ⇒ Object
      218 219 220  | 
    
      # File 'opal/opal/corelib/math.rb', line 218 def exp(x) Math.checked :exp, Math.float!(x) end  | 
  
.float!(value) ⇒ Object
      23 24 25 26 27  | 
    
      # File 'opal/opal/corelib/math.rb', line 23 def self.float!(value) Float(value) rescue ArgumentError raise Opal.type_error(value, Float) end  | 
  
.frexp(x) ⇒ Object
      222 223 224 225 226 227 228 229 230 231 232 233 234 235  | 
    
      # File 'opal/opal/corelib/math.rb', line 222 def frexp(x) x = Math.float!(x) %x{ if (isNaN(x)) { return [NaN, 0]; } var ex = Math.floor(Math.log(Math.abs(x)) / Math.log(2)) + 1, frac = x / Math.pow(2, ex); return [frac, ex]; } end  | 
  
.gamma(n) ⇒ Object
      237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331  | 
    
      # File 'opal/opal/corelib/math.rb', line 237 def gamma(n) n = Math.float!(n) %x{ var i, t, x, value, result, twoN, threeN, fourN, fiveN; var G = 4.7421875; var P = [ 0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, 0.33994649984811888699e-4, 0.46523628927048575665e-4, -0.98374475304879564677e-4, 0.15808870322491248884e-3, -0.21026444172410488319e-3, 0.21743961811521264320e-3, -0.16431810653676389022e-3, 0.84418223983852743293e-4, -0.26190838401581408670e-4, 0.36899182659531622704e-5 ]; if (isNaN(n)) { return NaN; } if (n === 0 && 1 / n < 0) { return -Infinity; } if (n === -1 || n === -Infinity) { #{raise DomainError, 'Numerical argument is out of domain - "gamma"'}; } if (#{Integer === n}) { if (n <= 0) { return isFinite(n) ? Infinity : NaN; } if (n > 171) { return Infinity; } value = n - 2; result = n - 1; while (value > 1) { result *= value; value--; } if (result == 0) { result = 1; } return result; } if (n < 0.5) { return Math.PI / (Math.sin(Math.PI * n) * #{Math.gamma(1 - n)}); } if (n >= 171.35) { return Infinity; } if (n > 85.0) { twoN = n * n; threeN = twoN * n; fourN = threeN * n; fiveN = fourN * n; return Math.sqrt(2 * Math.PI / n) * Math.pow((n / Math.E), n) * (1 + 1 / (12 * n) + 1 / (288 * twoN) - 139 / (51840 * threeN) - 571 / (2488320 * fourN) + 163879 / (209018880 * fiveN) + 5246819 / (75246796800 * fiveN * n)); } n -= 1; x = P[0]; for (i = 1; i < P.length; ++i) { x += P[i] / (n + i); } t = n + G + 0.5; return Math.sqrt(2 * Math.PI) * Math.pow(t, n + 0.5) * Math.exp(-t) * x; } end  | 
  
.hypot(x, y) ⇒ Object
      341 342 343  | 
    
      # File 'opal/opal/corelib/math.rb', line 341 def hypot(x, y) Math.checked :hypot, Math.float!(x), Math.float!(y) end  | 
  
.integer!(value) ⇒ Object
      29 30 31 32 33  | 
    
      # File 'opal/opal/corelib/math.rb', line 29 def self.integer!(value) Integer(value) rescue ArgumentError raise Opal.type_error(value, Integer) end  | 
  
.ldexp(mantissa, exponent) ⇒ Object
      345 346 347 348 349 350 351 352 353 354 355 356  | 
    
      # File 'opal/opal/corelib/math.rb', line 345 def ldexp(mantissa, exponent) mantissa = Math.float!(mantissa) exponent = Math.integer!(exponent) %x{ if (isNaN(exponent)) { #{raise RangeError, 'float NaN out of range of integer'}; } return mantissa * Math.pow(2, exponent); } end  | 
  
.lgamma(n) ⇒ Object
      358 359 360 361 362 363 364 365 366 367  | 
    
      # File 'opal/opal/corelib/math.rb', line 358 def lgamma(n) %x{ if (n == -1) { return [Infinity, 1]; } else { return [Math.log(Math.abs(#{Math.gamma(n)})), #{Math.gamma(n)} < 0 ? -1 : 1]; } } end  | 
  
.log(x, base = undefined) ⇒ Object
      369 370 371 372 373 374 375 376 377 378 379 380 381 382 383  | 
    
      # File 'opal/opal/corelib/math.rb', line 369 def log(x, base = undefined) if String === x raise Opal.type_error(x, Float) end if `base == null` Math.checked :log, Math.float!(x) else if String === base raise Opal.type_error(base, Float) end Math.checked(:log, Math.float!(x)) / Math.checked(:log, Math.float!(base)) end end  | 
  
.log10(x) ⇒ Object
      393 394 395 396 397 398 399  | 
    
      # File 'opal/opal/corelib/math.rb', line 393 def log10(x) if String === x raise Opal.type_error(x, Float) end Math.checked :log10, Math.float!(x) end  | 
  
.log2(x) ⇒ Object
      409 410 411 412 413 414 415  | 
    
      # File 'opal/opal/corelib/math.rb', line 409 def log2(x) if String === x raise Opal.type_error(x, Float) end Math.checked :log2, Math.float!(x) end  | 
  
.sin(x) ⇒ Object
      417 418 419  | 
    
      # File 'opal/opal/corelib/math.rb', line 417 def sin(x) Math.checked :sin, Math.float!(x) end  | 
  
.sinh(x) ⇒ Object
      429 430 431  | 
    
      # File 'opal/opal/corelib/math.rb', line 429 def sinh(x) Math.checked :sinh, Math.float!(x) end  | 
  
.sqrt(x) ⇒ Object
      433 434 435  | 
    
      # File 'opal/opal/corelib/math.rb', line 433 def sqrt(x) Math.checked :sqrt, Math.float!(x) end  |