PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/math.c

https://github.com/nazy/ruby
C | 822 lines | 386 code | 99 blank | 337 comment | 44 complexity | 717e14aca54e62b6a90fab7010f4113a MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD, Unlicense
  1. /**********************************************************************
  2. math.c -
  3. $Author$
  4. created at: Tue Jan 25 14:12:56 JST 1994
  5. Copyright (C) 1993-2007 Yukihiro Matsumoto
  6. **********************************************************************/
  7. #include "ruby/ruby.h"
  8. #include <math.h>
  9. #include <errno.h>
  10. #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
  11. VALUE rb_mMath;
  12. VALUE rb_eMathDomainError;
  13. extern VALUE rb_to_float(VALUE val);
  14. #define Need_Float(x) do {if (TYPE(x) != T_FLOAT) {(x) = rb_to_float(x);}} while(0)
  15. #define Need_Float2(x,y) do {\
  16. Need_Float(x);\
  17. Need_Float(y);\
  18. } while (0)
  19. #define domain_error(msg) \
  20. rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg);
  21. /*
  22. * call-seq:
  23. * Math.atan2(y, x) -> float
  24. *
  25. * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns
  26. * -PI..PI.
  27. *
  28. * Math.atan2(-0.0, -1.0) #=> -3.141592653589793
  29. * Math.atan2(-1.0, -1.0) #=> -2.356194490192345
  30. * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966
  31. * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483
  32. * Math.atan2(-0.0, 1.0) #=> -0.0
  33. * Math.atan2(0.0, 1.0) #=> 0.0
  34. * Math.atan2(1.0, 1.0) #=> 0.7853981633974483
  35. * Math.atan2(1.0, 0.0) #=> 1.5707963267948966
  36. * Math.atan2(1.0, -1.0) #=> 2.356194490192345
  37. * Math.atan2(0.0, -1.0) #=> 3.141592653589793
  38. *
  39. */
  40. static VALUE
  41. math_atan2(VALUE obj, VALUE y, VALUE x)
  42. {
  43. #ifndef M_PI
  44. # define M_PI 3.14159265358979323846
  45. #endif
  46. double dx, dy;
  47. Need_Float2(y, x);
  48. dx = RFLOAT_VALUE(x);
  49. dy = RFLOAT_VALUE(y);
  50. if (dx == 0.0 && dy == 0.0) {
  51. if (!signbit(dx))
  52. return DBL2NUM(dy);
  53. if (!signbit(dy))
  54. return DBL2NUM(M_PI);
  55. return DBL2NUM(-M_PI);
  56. }
  57. if (isinf(dx) && isinf(dy)) domain_error("atan2");
  58. return DBL2NUM(atan2(dy, dx));
  59. }
  60. /*
  61. * call-seq:
  62. * Math.cos(x) -> float
  63. *
  64. * Computes the cosine of <i>x</i> (expressed in radians). Returns
  65. * -1..1.
  66. */
  67. static VALUE
  68. math_cos(VALUE obj, VALUE x)
  69. {
  70. Need_Float(x);
  71. return DBL2NUM(cos(RFLOAT_VALUE(x)));
  72. }
  73. /*
  74. * call-seq:
  75. * Math.sin(x) -> float
  76. *
  77. * Computes the sine of <i>x</i> (expressed in radians). Returns
  78. * -1..1.
  79. */
  80. static VALUE
  81. math_sin(VALUE obj, VALUE x)
  82. {
  83. Need_Float(x);
  84. return DBL2NUM(sin(RFLOAT_VALUE(x)));
  85. }
  86. /*
  87. * call-seq:
  88. * Math.tan(x) -> float
  89. *
  90. * Returns the tangent of <i>x</i> (expressed in radians).
  91. */
  92. static VALUE
  93. math_tan(VALUE obj, VALUE x)
  94. {
  95. Need_Float(x);
  96. return DBL2NUM(tan(RFLOAT_VALUE(x)));
  97. }
  98. /*
  99. * call-seq:
  100. * Math.acos(x) -> float
  101. *
  102. * Computes the arc cosine of <i>x</i>. Returns 0..PI.
  103. */
  104. static VALUE
  105. math_acos(VALUE obj, VALUE x)
  106. {
  107. double d0, d;
  108. Need_Float(x);
  109. d0 = RFLOAT_VALUE(x);
  110. /* check for domain error */
  111. if (d0 < -1.0 || 1.0 < d0) domain_error("acos");
  112. d = acos(d0);
  113. return DBL2NUM(d);
  114. }
  115. /*
  116. * call-seq:
  117. * Math.asin(x) -> float
  118. *
  119. * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}.
  120. */
  121. static VALUE
  122. math_asin(VALUE obj, VALUE x)
  123. {
  124. double d0, d;
  125. Need_Float(x);
  126. d0 = RFLOAT_VALUE(x);
  127. /* check for domain error */
  128. if (d0 < -1.0 || 1.0 < d0) domain_error("asin");
  129. d = asin(d0);
  130. return DBL2NUM(d);
  131. }
  132. /*
  133. * call-seq:
  134. * Math.atan(x) -> float
  135. *
  136. * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}.
  137. */
  138. static VALUE
  139. math_atan(VALUE obj, VALUE x)
  140. {
  141. Need_Float(x);
  142. return DBL2NUM(atan(RFLOAT_VALUE(x)));
  143. }
  144. #ifndef HAVE_COSH
  145. double
  146. cosh(double x)
  147. {
  148. return (exp(x) + exp(-x)) / 2;
  149. }
  150. #endif
  151. /*
  152. * call-seq:
  153. * Math.cosh(x) -> float
  154. *
  155. * Computes the hyperbolic cosine of <i>x</i> (expressed in radians).
  156. */
  157. static VALUE
  158. math_cosh(VALUE obj, VALUE x)
  159. {
  160. Need_Float(x);
  161. return DBL2NUM(cosh(RFLOAT_VALUE(x)));
  162. }
  163. #ifndef HAVE_SINH
  164. double
  165. sinh(double x)
  166. {
  167. return (exp(x) - exp(-x)) / 2;
  168. }
  169. #endif
  170. /*
  171. * call-seq:
  172. * Math.sinh(x) -> float
  173. *
  174. * Computes the hyperbolic sine of <i>x</i> (expressed in
  175. * radians).
  176. */
  177. static VALUE
  178. math_sinh(VALUE obj, VALUE x)
  179. {
  180. Need_Float(x);
  181. return DBL2NUM(sinh(RFLOAT_VALUE(x)));
  182. }
  183. #ifndef HAVE_TANH
  184. double
  185. tanh(double x)
  186. {
  187. return sinh(x) / cosh(x);
  188. }
  189. #endif
  190. /*
  191. * call-seq:
  192. * Math.tanh() -> float
  193. *
  194. * Computes the hyperbolic tangent of <i>x</i> (expressed in
  195. * radians).
  196. */
  197. static VALUE
  198. math_tanh(VALUE obj, VALUE x)
  199. {
  200. Need_Float(x);
  201. return DBL2NUM(tanh(RFLOAT_VALUE(x)));
  202. }
  203. /*
  204. * call-seq:
  205. * Math.acosh(x) -> float
  206. *
  207. * Computes the inverse hyperbolic cosine of <i>x</i>.
  208. */
  209. static VALUE
  210. math_acosh(VALUE obj, VALUE x)
  211. {
  212. double d0, d;
  213. Need_Float(x);
  214. d0 = RFLOAT_VALUE(x);
  215. /* check for domain error */
  216. if (d0 < 1.0) domain_error("acosh");
  217. d = acosh(d0);
  218. return DBL2NUM(d);
  219. }
  220. /*
  221. * call-seq:
  222. * Math.asinh(x) -> float
  223. *
  224. * Computes the inverse hyperbolic sine of <i>x</i>.
  225. */
  226. static VALUE
  227. math_asinh(VALUE obj, VALUE x)
  228. {
  229. Need_Float(x);
  230. return DBL2NUM(asinh(RFLOAT_VALUE(x)));
  231. }
  232. /*
  233. * call-seq:
  234. * Math.atanh(x) -> float
  235. *
  236. * Computes the inverse hyperbolic tangent of <i>x</i>.
  237. */
  238. static VALUE
  239. math_atanh(VALUE obj, VALUE x)
  240. {
  241. double d0, d;
  242. Need_Float(x);
  243. d0 = RFLOAT_VALUE(x);
  244. /* check for domain error */
  245. if (d0 < -1.0 || +1.0 < d0) domain_error("atanh");
  246. /* check for pole error */
  247. if (d0 == -1.0) return DBL2NUM(-INFINITY);
  248. if (d0 == +1.0) return DBL2NUM(+INFINITY);
  249. d = atanh(d0);
  250. return DBL2NUM(d);
  251. }
  252. /*
  253. * call-seq:
  254. * Math.exp(x) -> float
  255. *
  256. * Returns e**x.
  257. *
  258. * Math.exp(0) #=> 1.0
  259. * Math.exp(1) #=> 2.718281828459045
  260. * Math.exp(1.5) #=> 4.4816890703380645
  261. *
  262. */
  263. static VALUE
  264. math_exp(VALUE obj, VALUE x)
  265. {
  266. Need_Float(x);
  267. return DBL2NUM(exp(RFLOAT_VALUE(x)));
  268. }
  269. #if defined __CYGWIN__
  270. # include <cygwin/version.h>
  271. # if CYGWIN_VERSION_DLL_MAJOR < 1005
  272. # define nan(x) nan()
  273. # endif
  274. # define log(x) ((x) < 0.0 ? nan("") : log(x))
  275. # define log10(x) ((x) < 0.0 ? nan("") : log10(x))
  276. #endif
  277. /*
  278. * call-seq:
  279. * Math.log(numeric) -> float
  280. * Math.log(num,base) -> float
  281. *
  282. * Returns the natural logarithm of <i>numeric</i>.
  283. * If additional second argument is given, it will be the base
  284. * of logarithm.
  285. *
  286. * Math.log(1) #=> 0.0
  287. * Math.log(Math::E) #=> 1.0
  288. * Math.log(Math::E**3) #=> 3.0
  289. * Math.log(12,3) #=> 2.2618595071429146
  290. *
  291. */
  292. static VALUE
  293. math_log(int argc, VALUE *argv)
  294. {
  295. VALUE x, base;
  296. double d0, d;
  297. rb_scan_args(argc, argv, "11", &x, &base);
  298. Need_Float(x);
  299. d0 = RFLOAT_VALUE(x);
  300. /* check for domain error */
  301. if (d0 < 0.0) domain_error("log");
  302. /* check for pole error */
  303. if (d0 == 0.0) return DBL2NUM(-INFINITY);
  304. d = log(d0);
  305. if (argc == 2) {
  306. Need_Float(base);
  307. d /= log(RFLOAT_VALUE(base));
  308. }
  309. return DBL2NUM(d);
  310. }
  311. #ifndef log2
  312. #ifndef HAVE_LOG2
  313. double
  314. log2(double x)
  315. {
  316. return log10(x)/log10(2.0);
  317. }
  318. #else
  319. extern double log2(double);
  320. #endif
  321. #endif
  322. /*
  323. * call-seq:
  324. * Math.log2(numeric) -> float
  325. *
  326. * Returns the base 2 logarithm of <i>numeric</i>.
  327. *
  328. * Math.log2(1) #=> 0.0
  329. * Math.log2(2) #=> 1.0
  330. * Math.log2(32768) #=> 15.0
  331. * Math.log2(65536) #=> 16.0
  332. *
  333. */
  334. static VALUE
  335. math_log2(VALUE obj, VALUE x)
  336. {
  337. double d0, d;
  338. Need_Float(x);
  339. d0 = RFLOAT_VALUE(x);
  340. /* check for domain error */
  341. if (d0 < 0.0) domain_error("log2");
  342. /* check for pole error */
  343. if (d0 == 0.0) return DBL2NUM(-INFINITY);
  344. d = log2(d0);
  345. return DBL2NUM(d);
  346. }
  347. /*
  348. * call-seq:
  349. * Math.log10(numeric) -> float
  350. *
  351. * Returns the base 10 logarithm of <i>numeric</i>.
  352. *
  353. * Math.log10(1) #=> 0.0
  354. * Math.log10(10) #=> 1.0
  355. * Math.log10(10**100) #=> 100.0
  356. *
  357. */
  358. static VALUE
  359. math_log10(VALUE obj, VALUE x)
  360. {
  361. double d0, d;
  362. Need_Float(x);
  363. d0 = RFLOAT_VALUE(x);
  364. /* check for domain error */
  365. if (d0 < 0.0) domain_error("log10");
  366. /* check for pole error */
  367. if (d0 == 0.0) return DBL2NUM(-INFINITY);
  368. d = log10(d0);
  369. return DBL2NUM(d);
  370. }
  371. /*
  372. * call-seq:
  373. * Math.sqrt(numeric) -> float
  374. *
  375. * Returns the non-negative square root of <i>numeric</i>.
  376. *
  377. * 0.upto(10) {|x|
  378. * p [x, Math.sqrt(x), Math.sqrt(x)**2]
  379. * }
  380. * #=>
  381. * [0, 0.0, 0.0]
  382. * [1, 1.0, 1.0]
  383. * [2, 1.4142135623731, 2.0]
  384. * [3, 1.73205080756888, 3.0]
  385. * [4, 2.0, 4.0]
  386. * [5, 2.23606797749979, 5.0]
  387. * [6, 2.44948974278318, 6.0]
  388. * [7, 2.64575131106459, 7.0]
  389. * [8, 2.82842712474619, 8.0]
  390. * [9, 3.0, 9.0]
  391. * [10, 3.16227766016838, 10.0]
  392. *
  393. */
  394. static VALUE
  395. math_sqrt(VALUE obj, VALUE x)
  396. {
  397. double d0, d;
  398. Need_Float(x);
  399. d0 = RFLOAT_VALUE(x);
  400. /* check for domain error */
  401. if (d0 < 0.0) domain_error("sqrt");
  402. if (d0 == 0.0) return DBL2NUM(0.0);
  403. d = sqrt(d0);
  404. return DBL2NUM(d);
  405. }
  406. /*
  407. * call-seq:
  408. * Math.cbrt(numeric) -> float
  409. *
  410. * Returns the cube root of <i>numeric</i>.
  411. *
  412. * -9.upto(9) {|x|
  413. * p [x, Math.cbrt(x), Math.cbrt(x)**3]
  414. * }
  415. * #=>
  416. * [-9, -2.0800838230519, -9.0]
  417. * [-8, -2.0, -8.0]
  418. * [-7, -1.91293118277239, -7.0]
  419. * [-6, -1.81712059283214, -6.0]
  420. * [-5, -1.7099759466767, -5.0]
  421. * [-4, -1.5874010519682, -4.0]
  422. * [-3, -1.44224957030741, -3.0]
  423. * [-2, -1.25992104989487, -2.0]
  424. * [-1, -1.0, -1.0]
  425. * [0, 0.0, 0.0]
  426. * [1, 1.0, 1.0]
  427. * [2, 1.25992104989487, 2.0]
  428. * [3, 1.44224957030741, 3.0]
  429. * [4, 1.5874010519682, 4.0]
  430. * [5, 1.7099759466767, 5.0]
  431. * [6, 1.81712059283214, 6.0]
  432. * [7, 1.91293118277239, 7.0]
  433. * [8, 2.0, 8.0]
  434. * [9, 2.0800838230519, 9.0]
  435. *
  436. */
  437. static VALUE
  438. math_cbrt(VALUE obj, VALUE x)
  439. {
  440. Need_Float(x);
  441. return DBL2NUM(cbrt(RFLOAT_VALUE(x)));
  442. }
  443. /*
  444. * call-seq:
  445. * Math.frexp(numeric) -> [ fraction, exponent ]
  446. *
  447. * Returns a two-element array containing the normalized fraction (a
  448. * <code>Float</code>) and exponent (a <code>Fixnum</code>) of
  449. * <i>numeric</i>.
  450. *
  451. * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
  452. * fraction * 2**exponent #=> 1234.0
  453. */
  454. static VALUE
  455. math_frexp(VALUE obj, VALUE x)
  456. {
  457. double d;
  458. int exp;
  459. Need_Float(x);
  460. d = frexp(RFLOAT_VALUE(x), &exp);
  461. return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
  462. }
  463. /*
  464. * call-seq:
  465. * Math.ldexp(flt, int) -> float
  466. *
  467. * Returns the value of <i>flt</i>*(2**<i>int</i>).
  468. *
  469. * fraction, exponent = Math.frexp(1234)
  470. * Math.ldexp(fraction, exponent) #=> 1234.0
  471. */
  472. static VALUE
  473. math_ldexp(VALUE obj, VALUE x, VALUE n)
  474. {
  475. Need_Float(x);
  476. return DBL2NUM(ldexp(RFLOAT_VALUE(x), NUM2INT(n)));
  477. }
  478. /*
  479. * call-seq:
  480. * Math.hypot(x, y) -> float
  481. *
  482. * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle
  483. * with sides <i>x</i> and <i>y</i>.
  484. *
  485. * Math.hypot(3, 4) #=> 5.0
  486. */
  487. static VALUE
  488. math_hypot(VALUE obj, VALUE x, VALUE y)
  489. {
  490. Need_Float2(x, y);
  491. return DBL2NUM(hypot(RFLOAT_VALUE(x), RFLOAT_VALUE(y)));
  492. }
  493. /*
  494. * call-seq:
  495. * Math.erf(x) -> float
  496. *
  497. * Calculates the error function of x.
  498. */
  499. static VALUE
  500. math_erf(VALUE obj, VALUE x)
  501. {
  502. Need_Float(x);
  503. return DBL2NUM(erf(RFLOAT_VALUE(x)));
  504. }
  505. /*
  506. * call-seq:
  507. * Math.erfc(x) -> float
  508. *
  509. * Calculates the complementary error function of x.
  510. */
  511. static VALUE
  512. math_erfc(VALUE obj, VALUE x)
  513. {
  514. Need_Float(x);
  515. return DBL2NUM(erfc(RFLOAT_VALUE(x)));
  516. }
  517. /*
  518. * call-seq:
  519. * Math.gamma(x) -> float
  520. *
  521. * Calculates the gamma function of x.
  522. *
  523. * Note that gamma(n) is same as fact(n-1) for integer n > 0.
  524. * However gamma(n) returns float and can be an approximation.
  525. *
  526. * def fact(n) (1..n).inject(1) {|r,i| r*i } end
  527. * 1.upto(26) {|i| p [i, Math.gamma(i), fact(i-1)] }
  528. * #=> [1, 1.0, 1]
  529. * # [2, 1.0, 1]
  530. * # [3, 2.0, 2]
  531. * # [4, 6.0, 6]
  532. * # [5, 24.0, 24]
  533. * # [6, 120.0, 120]
  534. * # [7, 720.0, 720]
  535. * # [8, 5040.0, 5040]
  536. * # [9, 40320.0, 40320]
  537. * # [10, 362880.0, 362880]
  538. * # [11, 3628800.0, 3628800]
  539. * # [12, 39916800.0, 39916800]
  540. * # [13, 479001600.0, 479001600]
  541. * # [14, 6227020800.0, 6227020800]
  542. * # [15, 87178291200.0, 87178291200]
  543. * # [16, 1307674368000.0, 1307674368000]
  544. * # [17, 20922789888000.0, 20922789888000]
  545. * # [18, 355687428096000.0, 355687428096000]
  546. * # [19, 6.402373705728e+15, 6402373705728000]
  547. * # [20, 1.21645100408832e+17, 121645100408832000]
  548. * # [21, 2.43290200817664e+18, 2432902008176640000]
  549. * # [22, 5.109094217170944e+19, 51090942171709440000]
  550. * # [23, 1.1240007277776077e+21, 1124000727777607680000]
  551. * # [24, 2.5852016738885062e+22, 25852016738884976640000]
  552. * # [25, 6.204484017332391e+23, 620448401733239439360000]
  553. * # [26, 1.5511210043330954e+25, 15511210043330985984000000]
  554. *
  555. */
  556. static VALUE
  557. math_gamma(VALUE obj, VALUE x)
  558. {
  559. static const double fact_table[] = {
  560. /* fact(0) */ 1.0,
  561. /* fact(1) */ 1.0,
  562. /* fact(2) */ 2.0,
  563. /* fact(3) */ 6.0,
  564. /* fact(4) */ 24.0,
  565. /* fact(5) */ 120.0,
  566. /* fact(6) */ 720.0,
  567. /* fact(7) */ 5040.0,
  568. /* fact(8) */ 40320.0,
  569. /* fact(9) */ 362880.0,
  570. /* fact(10) */ 3628800.0,
  571. /* fact(11) */ 39916800.0,
  572. /* fact(12) */ 479001600.0,
  573. /* fact(13) */ 6227020800.0,
  574. /* fact(14) */ 87178291200.0,
  575. /* fact(15) */ 1307674368000.0,
  576. /* fact(16) */ 20922789888000.0,
  577. /* fact(17) */ 355687428096000.0,
  578. /* fact(18) */ 6402373705728000.0,
  579. /* fact(19) */ 121645100408832000.0,
  580. /* fact(20) */ 2432902008176640000.0,
  581. /* fact(21) */ 51090942171709440000.0,
  582. /* fact(22) */ 1124000727777607680000.0,
  583. /* fact(23)=25852016738884976640000 needs 56bit mantissa which is
  584. * impossible to represent exactly in IEEE 754 double which have
  585. * 53bit mantissa. */
  586. };
  587. double d0, d;
  588. double intpart, fracpart;
  589. Need_Float(x);
  590. d0 = RFLOAT_VALUE(x);
  591. /* check for domain error */
  592. if (isinf(d0) && signbit(d0)) domain_error("gamma");
  593. fracpart = modf(d0, &intpart);
  594. if (fracpart == 0.0) {
  595. if (intpart < 0) domain_error("gamma");
  596. if (0 < intpart &&
  597. intpart - 1 < (double)numberof(fact_table)) {
  598. return DBL2NUM(fact_table[(int)intpart - 1]);
  599. }
  600. }
  601. d = tgamma(d0);
  602. return DBL2NUM(d);
  603. }
  604. /*
  605. * call-seq:
  606. * Math.lgamma(x) -> [float, -1 or 1]
  607. *
  608. * Calculates the logarithmic gamma of x and
  609. * the sign of gamma of x.
  610. *
  611. * Math.lgamma(x) is same as
  612. * [Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
  613. * but avoid overflow by Math.gamma(x) for large x.
  614. */
  615. static VALUE
  616. math_lgamma(VALUE obj, VALUE x)
  617. {
  618. double d0, d;
  619. int sign=1;
  620. VALUE v;
  621. Need_Float(x);
  622. d0 = RFLOAT_VALUE(x);
  623. /* check for domain error */
  624. if (isinf(d0)) {
  625. if (signbit(d0)) domain_error("lgamma");
  626. return rb_assoc_new(DBL2NUM(INFINITY), INT2FIX(1));
  627. }
  628. d = lgamma_r(d0, &sign);
  629. v = DBL2NUM(d);
  630. return rb_assoc_new(v, INT2FIX(sign));
  631. }
  632. #define exp1(n) \
  633. VALUE \
  634. rb_math_##n(VALUE x)\
  635. {\
  636. return math_##n(rb_mMath, x);\
  637. }
  638. #define exp2(n) \
  639. VALUE \
  640. rb_math_##n(VALUE x, VALUE y)\
  641. {\
  642. return math_##n(rb_mMath, x, y);\
  643. }
  644. exp2(atan2)
  645. exp1(cos)
  646. exp1(cosh)
  647. exp1(exp)
  648. exp2(hypot)
  649. VALUE
  650. rb_math_log(int argc, VALUE *argv)
  651. {
  652. return math_log(argc, argv);
  653. }
  654. exp1(sin)
  655. exp1(sinh)
  656. exp1(sqrt)
  657. /*
  658. * Document-class: Math::DomainError
  659. *
  660. * Raised when a mathematical function is evaluated outside of its
  661. * domain of definition.
  662. *
  663. * For example, since +cos+ returns values in the range -1..1,
  664. * its inverse function +acos+ is only defined on that interval:
  665. *
  666. * Math.acos(42)
  667. *
  668. * <em>produces:</em>
  669. *
  670. * Math::DomainError: Numerical argument is out of domain - "acos"
  671. */
  672. /*
  673. * The <code>Math</code> module contains module functions for basic
  674. * trigonometric and transcendental functions. See class
  675. * <code>Float</code> for a list of constants that
  676. * define Ruby's floating point accuracy.
  677. */
  678. void
  679. Init_Math(void)
  680. {
  681. rb_mMath = rb_define_module("Math");
  682. rb_eMathDomainError = rb_define_class_under(rb_mMath, "DomainError", rb_eStandardError);
  683. #ifdef M_PI
  684. rb_define_const(rb_mMath, "PI", DBL2NUM(M_PI));
  685. #else
  686. rb_define_const(rb_mMath, "PI", DBL2NUM(atan(1.0)*4.0));
  687. #endif
  688. #ifdef M_E
  689. rb_define_const(rb_mMath, "E", DBL2NUM(M_E));
  690. #else
  691. rb_define_const(rb_mMath, "E", DBL2NUM(exp(1.0)));
  692. #endif
  693. rb_define_module_function(rb_mMath, "atan2", math_atan2, 2);
  694. rb_define_module_function(rb_mMath, "cos", math_cos, 1);
  695. rb_define_module_function(rb_mMath, "sin", math_sin, 1);
  696. rb_define_module_function(rb_mMath, "tan", math_tan, 1);
  697. rb_define_module_function(rb_mMath, "acos", math_acos, 1);
  698. rb_define_module_function(rb_mMath, "asin", math_asin, 1);
  699. rb_define_module_function(rb_mMath, "atan", math_atan, 1);
  700. rb_define_module_function(rb_mMath, "cosh", math_cosh, 1);
  701. rb_define_module_function(rb_mMath, "sinh", math_sinh, 1);
  702. rb_define_module_function(rb_mMath, "tanh", math_tanh, 1);
  703. rb_define_module_function(rb_mMath, "acosh", math_acosh, 1);
  704. rb_define_module_function(rb_mMath, "asinh", math_asinh, 1);
  705. rb_define_module_function(rb_mMath, "atanh", math_atanh, 1);
  706. rb_define_module_function(rb_mMath, "exp", math_exp, 1);
  707. rb_define_module_function(rb_mMath, "log", math_log, -1);
  708. rb_define_module_function(rb_mMath, "log2", math_log2, 1);
  709. rb_define_module_function(rb_mMath, "log10", math_log10, 1);
  710. rb_define_module_function(rb_mMath, "sqrt", math_sqrt, 1);
  711. rb_define_module_function(rb_mMath, "cbrt", math_cbrt, 1);
  712. rb_define_module_function(rb_mMath, "frexp", math_frexp, 1);
  713. rb_define_module_function(rb_mMath, "ldexp", math_ldexp, 2);
  714. rb_define_module_function(rb_mMath, "hypot", math_hypot, 2);
  715. rb_define_module_function(rb_mMath, "erf", math_erf, 1);
  716. rb_define_module_function(rb_mMath, "erfc", math_erfc, 1);
  717. rb_define_module_function(rb_mMath, "gamma", math_gamma, 1);
  718. rb_define_module_function(rb_mMath, "lgamma", math_lgamma, 1);
  719. }