Module: Math

Defined in:
opal/opal/corelib/math.rb

Overview

helpers: type_error backtick_javascript: true use_strict: true

Class Method Summary collapse

Class Method Details

.atan2(y, x) ⇒ Object



106
107
108
# File 'opal/opal/corelib/math.rb', line 106

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

.checked(method, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/opal/corelib/math.rb', line 11

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)) {
      #{::Kernel.raise DomainError, "Numerical argument is out of domain - \"#{method}\""};
    }

    return result;
  }
end

.float!(value) ⇒ Object



27
28
29
30
31
# File 'opal/opal/corelib/math.rb', line 27

def self.float!(value)
  ::Kernel.Float(value)
rescue ::ArgumentError
  ::Kernel.raise `$type_error(value, #{::Float})`
end

.frexp(x) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'opal/opal/corelib/math.rb', line 114

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'opal/opal/corelib/math.rb', line 129

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) {
      #{::Kernel.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



110
111
112
# File 'opal/opal/corelib/math.rb', line 110

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

.integer!(value) ⇒ Object



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

def self.integer!(value)
  ::Kernel.Integer(value)
rescue ::ArgumentError
  ::Kernel.raise `$type_error(value, #{::Integer})`
end

.ldexp(mantissa, exponent) ⇒ Object



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

def ldexp(mantissa, exponent)
  mantissa = Math.float!(mantissa)
  exponent = Math.integer!(exponent)

  %x{
    if (isNaN(exponent)) {
      #{::Kernel.raise ::RangeError, 'float NaN out of range of integer'};
    }

    return mantissa * Math.pow(2, exponent);
  }
end

.lgamma(n) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'opal/opal/corelib/math.rb', line 238

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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'opal/opal/corelib/math.rb', line 249

def log(x, base = undefined)
  if ::String === x
    ::Kernel.raise `$type_error(x, #{::Float})`
  end

  if `base == null`
    ::Math.checked :log, ::Math.float!(x)
  else
    if ::String === base
      ::Kernel.raise `$type_error(base, #{::Float})`
    end

    ::Math.checked(:log, ::Math.float!(x)) / ::Math.checked(:log, ::Math.float!(base))
  end
end

.log10(x) ⇒ Object



265
266
267
268
269
270
271
# File 'opal/opal/corelib/math.rb', line 265

def log10(x)
  if ::String === x
    ::Kernel.raise `$type_error(x, #{::Float})`
  end

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

.log2(x) ⇒ Object



273
274
275
276
277
278
279
# File 'opal/opal/corelib/math.rb', line 273

def log2(x)
  if ::String === x
    ::Kernel.raise `$type_error(x, #{::Float})`
  end

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

.tan(x) ⇒ Object



281
282
283
284
285
286
287
288
289
# File 'opal/opal/corelib/math.rb', line 281

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

  if x.infinite?
    return ::Float::NAN
  end

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