Module: Math
- Defined in:
- opal/opal/corelib/math.rb
Overview
helpers: type_error
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
39 40 41 |
# File 'opal/opal/corelib/math.rb', line 39 def acos(x) ::Math.checked :acos, ::Math.float!(x) end |
.acosh(x) ⇒ Object
51 52 53 |
# File 'opal/opal/corelib/math.rb', line 51 def acosh(x) ::Math.checked :acosh, ::Math.float!(x) end |
.asin(x) ⇒ Object
55 56 57 |
# File 'opal/opal/corelib/math.rb', line 55 def asin(x) ::Math.checked :asin, ::Math.float!(x) end |
.asinh(x) ⇒ Object
67 68 69 |
# File 'opal/opal/corelib/math.rb', line 67 def asinh(x) ::Math.checked :asinh, ::Math.float!(x) end |
.atan(x) ⇒ Object
71 72 73 |
# File 'opal/opal/corelib/math.rb', line 71 def atan(x) ::Math.checked :atan, ::Math.float!(x) end |
.atan2(y, x) ⇒ Object
75 76 77 |
# File 'opal/opal/corelib/math.rb', line 75 def atan2(y, x) ::Math.checked :atan2, ::Math.float!(y), ::Math.float!(x) end |
.atanh(x) ⇒ Object
87 88 89 |
# File 'opal/opal/corelib/math.rb', line 87 def atanh(x) ::Math.checked :atanh, ::Math.float!(x) end |
.cbrt(x) ⇒ Object
137 138 139 |
# File 'opal/opal/corelib/math.rb', line 137 def cbrt(x) ::Math.checked :cbrt, ::Math.float!(x) end |
.checked(method, *args) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'opal/opal/corelib/math.rb', line 9 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 |
.cos(x) ⇒ Object
141 142 143 |
# File 'opal/opal/corelib/math.rb', line 141 def cos(x) ::Math.checked :cos, ::Math.float!(x) end |
.cosh(x) ⇒ Object
153 154 155 |
# File 'opal/opal/corelib/math.rb', line 153 def cosh(x) ::Math.checked :cosh, ::Math.float!(x) end |
.erf(x) ⇒ Object
183 184 185 |
# File 'opal/opal/corelib/math.rb', line 183 def erf(x) ::Math.checked :erf, ::Math.float!(x) end |
.erfc(x) ⇒ Object
216 217 218 |
# File 'opal/opal/corelib/math.rb', line 216 def erfc(x) ::Math.checked :erfc, ::Math.float!(x) end |
.exp(x) ⇒ Object
220 221 222 |
# File 'opal/opal/corelib/math.rb', line 220 def exp(x) ::Math.checked :exp, ::Math.float!(x) end |
.float!(value) ⇒ Object
25 26 27 28 29 |
# File 'opal/opal/corelib/math.rb', line 25 def self.float!(value) ::Kernel.Float(value) rescue ::ArgumentError ::Kernel.raise `$type_error(value, #{::Float})` end |
.frexp(x) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'opal/opal/corelib/math.rb', line 224 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
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 332 333 |
# File 'opal/opal/corelib/math.rb', line 239 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
343 344 345 |
# File 'opal/opal/corelib/math.rb', line 343 def hypot(x, y) ::Math.checked :hypot, ::Math.float!(x), ::Math.float!(y) end |
.integer!(value) ⇒ Object
31 32 33 34 35 |
# File 'opal/opal/corelib/math.rb', line 31 def self.integer!(value) ::Kernel.Integer(value) rescue ::ArgumentError ::Kernel.raise `$type_error(value, #{::Integer})` end |
.ldexp(mantissa, exponent) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'opal/opal/corelib/math.rb', line 347 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
360 361 362 363 364 365 366 367 368 369 |
# File 'opal/opal/corelib/math.rb', line 360 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
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'opal/opal/corelib/math.rb', line 371 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
395 396 397 398 399 400 401 |
# File 'opal/opal/corelib/math.rb', line 395 def log10(x) if ::String === x ::Kernel.raise `$type_error(x, #{::Float})` end ::Math.checked :log10, ::Math.float!(x) end |
.log2(x) ⇒ Object
411 412 413 414 415 416 417 |
# File 'opal/opal/corelib/math.rb', line 411 def log2(x) if ::String === x ::Kernel.raise `$type_error(x, #{::Float})` end ::Math.checked :log2, ::Math.float!(x) end |
.sin(x) ⇒ Object
419 420 421 |
# File 'opal/opal/corelib/math.rb', line 419 def sin(x) ::Math.checked :sin, ::Math.float!(x) end |
.sinh(x) ⇒ Object
431 432 433 |
# File 'opal/opal/corelib/math.rb', line 431 def sinh(x) ::Math.checked :sinh, ::Math.float!(x) end |
.sqrt(x) ⇒ Object
435 436 437 |
# File 'opal/opal/corelib/math.rb', line 435 def sqrt(x) ::Math.checked :sqrt, ::Math.float!(x) end |