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

Class Method Details

.acos(x) ⇒ Object



41
42
43
# File 'opal/opal/corelib/math.rb', line 41

def acos(x)
  Math.checked :acos, Math.float!(x)
end

.acosh(x) ⇒ Object



53
54
55
# File 'opal/opal/corelib/math.rb', line 53

def acosh(x)
  Math.checked :acosh, Math.float!(x)
end

.asin(x) ⇒ Object



57
58
59
# File 'opal/opal/corelib/math.rb', line 57

def asin(x)
  Math.checked :asin, Math.float!(x)
end

.asinh(x) ⇒ Object



69
70
71
# File 'opal/opal/corelib/math.rb', line 69

def asinh(x)
  Math.checked :asinh, Math.float!(x)
end

.atan(x) ⇒ Object



73
74
75
# File 'opal/opal/corelib/math.rb', line 73

def atan(x)
  Math.checked :atan, Math.float!(x)
end

.atan2(y, x) ⇒ Object



77
78
79
# File 'opal/opal/corelib/math.rb', line 77

def atan2(y, x)
  Math.checked :atan2, Math.float!(y), Math.float!(x)
end

.atanh(x) ⇒ Object



89
90
91
# File 'opal/opal/corelib/math.rb', line 89

def atanh(x)
  Math.checked :atanh, Math.float!(x)
end

.cbrt(x) ⇒ Object



139
140
141
# File 'opal/opal/corelib/math.rb', line 139

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



143
144
145
# File 'opal/opal/corelib/math.rb', line 143

def cos(x)
  Math.checked :cos, Math.float!(x)
end

.cosh(x) ⇒ Object



155
156
157
# File 'opal/opal/corelib/math.rb', line 155

def cosh(x)
  Math.checked :cosh, Math.float!(x)
end

.erf(x) ⇒ Object



185
186
187
# File 'opal/opal/corelib/math.rb', line 185

def erf(x)
  Math.checked :erf, Math.float!(x)
end

.erfc(x) ⇒ Object



218
219
220
# File 'opal/opal/corelib/math.rb', line 218

def erfc(x)
  Math.checked :erfc, Math.float!(x)
end

.exp(x) ⇒ Object



222
223
224
# File 'opal/opal/corelib/math.rb', line 222

def exp(x)
  Math.checked :exp, Math.float!(x)
end

.float!(value) ⇒ Object



23
24
25
26
27
28
29
# File 'opal/opal/corelib/math.rb', line 23

def self.float!(value)
  begin
    Float(value)
  rescue ArgumentError
    raise Opal.type_error(value, Float)
  end
end

.frexp(x) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'opal/opal/corelib/math.rb', line 226

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



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
332
333
334
335
# File 'opal/opal/corelib/math.rb', line 241

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



345
346
347
# File 'opal/opal/corelib/math.rb', line 345

def hypot(x, y)
  Math.checked :hypot, Math.float!(x), Math.float!(y)
end

.integer!(value) ⇒ Object



31
32
33
34
35
36
37
# File 'opal/opal/corelib/math.rb', line 31

def self.integer!(value)
  begin
    Integer(value)
  rescue ArgumentError
    raise Opal.type_error(value, Integer)
  end
end

.ldexp(mantissa, exponent) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
# File 'opal/opal/corelib/math.rb', line 349

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



362
363
364
365
366
367
368
369
370
371
# File 'opal/opal/corelib/math.rb', line 362

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



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'opal/opal/corelib/math.rb', line 373

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



397
398
399
400
401
402
403
# File 'opal/opal/corelib/math.rb', line 397

def log10(x)
  if String === x
    raise Opal.type_error(x, Float)
  end

  Math.checked :log10, Math.float!(x)
end

.log2(x) ⇒ Object



413
414
415
416
417
418
419
# File 'opal/opal/corelib/math.rb', line 413

def log2(x)
  if String === x
    raise Opal.type_error(x, Float)
  end

  Math.checked :log2, Math.float!(x)
end

.sin(x) ⇒ Object



421
422
423
# File 'opal/opal/corelib/math.rb', line 421

def sin(x)
  Math.checked :sin, Math.float!(x)
end

.sinh(x) ⇒ Object



433
434
435
# File 'opal/opal/corelib/math.rb', line 433

def sinh(x)
  Math.checked :sinh, Math.float!(x)
end

.sqrt(x) ⇒ Object



437
438
439
# File 'opal/opal/corelib/math.rb', line 437

def sqrt(x)
  Math.checked :sqrt, Math.float!(x)
end

.tan(x) ⇒ Object



441
442
443
444
445
446
447
448
449
# File 'opal/opal/corelib/math.rb', line 441

def tan(x)
  x = Math.float!(x)

  if x.infinite?
    return Float::NAN
  end

  Math.checked :tan, Math.float!(x)
end

.tanh(x) ⇒ Object



467
468
469
# File 'opal/opal/corelib/math.rb', line 467

def tanh(x)
  Math.checked :tanh, Math.float!(x)
end