Module: Math

Extended by:
Math
Included in:
Math
Defined in:
opal/stdlib/math.rb

Defined Under Namespace

Classes: DomainError

Constant Summary

E =
`Math.E`
PI =
`Math.PI`

Instance Method Summary collapse

Instance Method Details

#acos(x) ⇒ Object



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

def acos(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    x = #{x.to_f};

    if (x < -1 || x > 1) {
      #{raise DomainError, :acos};
    }

    return Math.acos(x);
  }
end

#acosh(x) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'opal/stdlib/math.rb', line 35

def acosh(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.acosh(#{x.to_f});
  }
end

#asin(x) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'opal/stdlib/math.rb', line 45

def asin(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    x = #{x.to_f};

    if (x < -1 || x > 1) {
      #{raise DomainError, :asin};
    }

    return Math.asin(x);
  }
end

#asinh(x) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'opal/stdlib/math.rb', line 69

def asinh(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.asinh(#{x.to_f});
  }
end

#atan(x) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'opal/stdlib/math.rb', line 79

def atan(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.atan(#{x.to_f});
  }
end

#atan2(x, y) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'opal/stdlib/math.rb', line 89

def atan2(x, y)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    if (!#{Numeric === y}) {
      #{raise Opal.type_error(y, Float)};
    }

    return Math.atan2(#{x.to_f}, #{y.to_f});
  }
end

#atanh(x) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'opal/stdlib/math.rb', line 111

def atanh(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    x = #{x.to_f};

    if (x < -1 || x > 1) {
      #{raise DomainError, :atanh};
    }

    return Math.atanh(x);
  }
end

#cbrt(x) ⇒ Object

TODO: reimplement this when unavailable



128
129
130
# File 'opal/stdlib/math.rb', line 128

def cbrt(x)
  `Math.cbrt(x)`
end

#cos(x) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'opal/stdlib/math.rb', line 132

def cos(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.cos(#{x.to_f});
  }
end

#cosh(x) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'opal/stdlib/math.rb', line 150

def cosh(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.cosh(#{x.to_f});
  }
end

#erf(x) ⇒ Object

Raises:

  • (NotImplementedError)


160
161
162
# File 'opal/stdlib/math.rb', line 160

def erf(x)
  raise NotImplementedError
end

#erfc(x) ⇒ Object

Raises:

  • (NotImplementedError)


164
165
166
# File 'opal/stdlib/math.rb', line 164

def erfc(x)
  raise NotImplementedError
end

#exp(x) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'opal/stdlib/math.rb', line 168

def exp(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.exp(#{x.to_f});
  }
end

#frexp(x) ⇒ Object

Raises:

  • (NotImplementedError)


178
179
180
# File 'opal/stdlib/math.rb', line 178

def frexp(x)
  raise NotImplementedError
end

#gamma(x) ⇒ Object

Raises:

  • (NotImplementedError)


182
183
184
# File 'opal/stdlib/math.rb', line 182

def gamma(x)
  raise NotImplementedError
end

#hypot(x, y) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'opal/stdlib/math.rb', line 194

def hypot(x, y)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    if (!#{Numeric === y}) {
      #{raise Opal.type_error(y, Float)};
    }

    return Math.hypot(#{x.to_f}, #{y.to_f});
  }
end

#ldexp(flt, int) ⇒ Object

Raises:

  • (NotImplementedError)


208
209
210
# File 'opal/stdlib/math.rb', line 208

def ldexp(flt, int)
  raise NotImplementedError
end

#lgamma(x) ⇒ Object

Raises:

  • (NotImplementedError)


212
213
214
# File 'opal/stdlib/math.rb', line 212

def lgamma(x)
  raise NotImplementedError
end

#log(num, base = E, method = nil) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'opal/stdlib/math.rb', line 216

def log(num, base = E, method = nil)
  %x{
    if (!#{Numeric === num}) {
      #{raise Opal.type_error(num, Float)};
    }

    if (!#{Numeric === base}) {
      #{raise Opal.type_error(base, Float)};
    }

    num  = #{num.to_f};
    base = #{base.to_f};

    if (num < 0) {
      #{raise DomainError, method || :log};
    }

    num = Math.log(num);

    if (base != Math.E) {
      num /= Math.log(base);
    }

    return num
  }
end

#log10(num) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'opal/stdlib/math.rb', line 244

def log10(num)
  %x{
    if (!#{Numeric === num}) {
      #{raise Opal.type_error(num, Float)};
    }

    num = #{num.to_f};

    if (num < 0) {
      #{raise DomainError, :log2};
    }

    return Math.log10(num);
  }
end

#log2(num) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'opal/stdlib/math.rb', line 266

def log2(num)
  %x{
    if (!#{Numeric === num}) {
      #{raise Opal.type_error(num, Float)};
    }

    num = #{num.to_f};

    if (num < 0) {
      #{raise DomainError, :log2};
    }

    return Math.log2(num);
  }
end

#sin(x) ⇒ Object



287
288
289
290
291
292
293
294
295
# File 'opal/stdlib/math.rb', line 287

def sin(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.sin(#{x.to_f});
  }
end

#sinh(x) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'opal/stdlib/math.rb', line 305

def sinh(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.sinh(#{x.to_f});
  }
end

#sqrt(x) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'opal/stdlib/math.rb', line 315

def sqrt(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    x = #{x.to_f};

    if (x < 0) {
      #{raise DomainError, :log2};
    }

    return Math.sqrt(x);
  }
end

#tan(x) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'opal/stdlib/math.rb', line 331

def tan(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.tan(#{x.to_f});
  }
end

#tanh(x) ⇒ Object



357
358
359
360
361
362
363
364
365
# File 'opal/stdlib/math.rb', line 357

def tanh(x)
  %x{
    if (!#{Numeric === x}) {
      #{raise Opal.type_error(x, Float)};
    }

    return Math.tanh(#{x.to_f});
  }
end