test_trig.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from mpmath import *
  2. from mpmath.libmp import *
  3. def test_trig_misc_hard():
  4. mp.prec = 53
  5. # Worst-case input for an IEEE double, from a paper by Kahan
  6. x = ldexp(6381956970095103,797)
  7. assert cos(x) == mpf('-4.6871659242546277e-19')
  8. assert sin(x) == 1
  9. mp.prec = 150
  10. a = mpf(10**50)
  11. mp.prec = 53
  12. assert sin(a).ae(-0.7896724934293100827)
  13. assert cos(a).ae(-0.6135286082336635622)
  14. # Check relative accuracy close to x = zero
  15. assert sin(1e-100) == 1e-100 # when rounding to nearest
  16. assert sin(1e-6).ae(9.999999999998333e-007, rel_eps=2e-15, abs_eps=0)
  17. assert sin(1e-6j).ae(1.0000000000001666e-006j, rel_eps=2e-15, abs_eps=0)
  18. assert sin(-1e-6j).ae(-1.0000000000001666e-006j, rel_eps=2e-15, abs_eps=0)
  19. assert cos(1e-100) == 1
  20. assert cos(1e-6).ae(0.9999999999995)
  21. assert cos(-1e-6j).ae(1.0000000000005)
  22. assert tan(1e-100) == 1e-100
  23. assert tan(1e-6).ae(1.0000000000003335e-006, rel_eps=2e-15, abs_eps=0)
  24. assert tan(1e-6j).ae(9.9999999999966644e-007j, rel_eps=2e-15, abs_eps=0)
  25. assert tan(-1e-6j).ae(-9.9999999999966644e-007j, rel_eps=2e-15, abs_eps=0)
  26. def test_trig_near_zero():
  27. mp.dps = 15
  28. for r in [round_nearest, round_down, round_up, round_floor, round_ceiling]:
  29. assert sin(0, rounding=r) == 0
  30. assert cos(0, rounding=r) == 1
  31. a = mpf('1e-100')
  32. b = mpf('-1e-100')
  33. assert sin(a, rounding=round_nearest) == a
  34. assert sin(a, rounding=round_down) < a
  35. assert sin(a, rounding=round_floor) < a
  36. assert sin(a, rounding=round_up) >= a
  37. assert sin(a, rounding=round_ceiling) >= a
  38. assert sin(b, rounding=round_nearest) == b
  39. assert sin(b, rounding=round_down) > b
  40. assert sin(b, rounding=round_floor) <= b
  41. assert sin(b, rounding=round_up) <= b
  42. assert sin(b, rounding=round_ceiling) > b
  43. assert cos(a, rounding=round_nearest) == 1
  44. assert cos(a, rounding=round_down) < 1
  45. assert cos(a, rounding=round_floor) < 1
  46. assert cos(a, rounding=round_up) == 1
  47. assert cos(a, rounding=round_ceiling) == 1
  48. assert cos(b, rounding=round_nearest) == 1
  49. assert cos(b, rounding=round_down) < 1
  50. assert cos(b, rounding=round_floor) < 1
  51. assert cos(b, rounding=round_up) == 1
  52. assert cos(b, rounding=round_ceiling) == 1
  53. def test_trig_near_n_pi():
  54. mp.dps = 15
  55. a = [n*pi for n in [1, 2, 6, 11, 100, 1001, 10000, 100001]]
  56. mp.dps = 135
  57. a.append(10**100 * pi)
  58. mp.dps = 15
  59. assert sin(a[0]) == mpf('1.2246467991473531772e-16')
  60. assert sin(a[1]) == mpf('-2.4492935982947063545e-16')
  61. assert sin(a[2]) == mpf('-7.3478807948841190634e-16')
  62. assert sin(a[3]) == mpf('4.8998251578625894243e-15')
  63. assert sin(a[4]) == mpf('1.9643867237284719452e-15')
  64. assert sin(a[5]) == mpf('-8.8632615209684813458e-15')
  65. assert sin(a[6]) == mpf('-4.8568235395684898392e-13')
  66. assert sin(a[7]) == mpf('3.9087342299491231029e-11')
  67. assert sin(a[8]) == mpf('-1.369235466754566993528e-36')
  68. r = round_nearest
  69. assert cos(a[0], rounding=r) == -1
  70. assert cos(a[1], rounding=r) == 1
  71. assert cos(a[2], rounding=r) == 1
  72. assert cos(a[3], rounding=r) == -1
  73. assert cos(a[4], rounding=r) == 1
  74. assert cos(a[5], rounding=r) == -1
  75. assert cos(a[6], rounding=r) == 1
  76. assert cos(a[7], rounding=r) == -1
  77. assert cos(a[8], rounding=r) == 1
  78. r = round_up
  79. assert cos(a[0], rounding=r) == -1
  80. assert cos(a[1], rounding=r) == 1
  81. assert cos(a[2], rounding=r) == 1
  82. assert cos(a[3], rounding=r) == -1
  83. assert cos(a[4], rounding=r) == 1
  84. assert cos(a[5], rounding=r) == -1
  85. assert cos(a[6], rounding=r) == 1
  86. assert cos(a[7], rounding=r) == -1
  87. assert cos(a[8], rounding=r) == 1
  88. r = round_down
  89. assert cos(a[0], rounding=r) > -1
  90. assert cos(a[1], rounding=r) < 1
  91. assert cos(a[2], rounding=r) < 1
  92. assert cos(a[3], rounding=r) > -1
  93. assert cos(a[4], rounding=r) < 1
  94. assert cos(a[5], rounding=r) > -1
  95. assert cos(a[6], rounding=r) < 1
  96. assert cos(a[7], rounding=r) > -1
  97. assert cos(a[8], rounding=r) < 1
  98. r = round_floor
  99. assert cos(a[0], rounding=r) == -1
  100. assert cos(a[1], rounding=r) < 1
  101. assert cos(a[2], rounding=r) < 1
  102. assert cos(a[3], rounding=r) == -1
  103. assert cos(a[4], rounding=r) < 1
  104. assert cos(a[5], rounding=r) == -1
  105. assert cos(a[6], rounding=r) < 1
  106. assert cos(a[7], rounding=r) == -1
  107. assert cos(a[8], rounding=r) < 1
  108. r = round_ceiling
  109. assert cos(a[0], rounding=r) > -1
  110. assert cos(a[1], rounding=r) == 1
  111. assert cos(a[2], rounding=r) == 1
  112. assert cos(a[3], rounding=r) > -1
  113. assert cos(a[4], rounding=r) == 1
  114. assert cos(a[5], rounding=r) > -1
  115. assert cos(a[6], rounding=r) == 1
  116. assert cos(a[7], rounding=r) > -1
  117. assert cos(a[8], rounding=r) == 1
  118. mp.dps = 15