test_special.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from mpmath import *
  2. def test_special():
  3. assert inf == inf
  4. assert inf != -inf
  5. assert -inf == -inf
  6. assert inf != nan
  7. assert nan != nan
  8. assert isnan(nan)
  9. assert --inf == inf
  10. assert abs(inf) == inf
  11. assert abs(-inf) == inf
  12. assert abs(nan) != abs(nan)
  13. assert isnan(inf - inf)
  14. assert isnan(inf + (-inf))
  15. assert isnan(-inf - (-inf))
  16. assert isnan(inf + nan)
  17. assert isnan(-inf + nan)
  18. assert mpf(2) + inf == inf
  19. assert 2 + inf == inf
  20. assert mpf(2) - inf == -inf
  21. assert 2 - inf == -inf
  22. assert inf > 3
  23. assert 3 < inf
  24. assert 3 > -inf
  25. assert -inf < 3
  26. assert inf > mpf(3)
  27. assert mpf(3) < inf
  28. assert mpf(3) > -inf
  29. assert -inf < mpf(3)
  30. assert not (nan < 3)
  31. assert not (nan > 3)
  32. assert isnan(inf * 0)
  33. assert isnan(-inf * 0)
  34. assert inf * 3 == inf
  35. assert inf * -3 == -inf
  36. assert -inf * 3 == -inf
  37. assert -inf * -3 == inf
  38. assert inf * inf == inf
  39. assert -inf * -inf == inf
  40. assert isnan(nan / 3)
  41. assert inf / -3 == -inf
  42. assert inf / 3 == inf
  43. assert 3 / inf == 0
  44. assert -3 / inf == 0
  45. assert 0 / inf == 0
  46. assert isnan(inf / inf)
  47. assert isnan(inf / -inf)
  48. assert isnan(inf / nan)
  49. assert mpf('inf') == mpf('+inf') == inf
  50. assert mpf('-inf') == -inf
  51. assert isnan(mpf('nan'))
  52. assert isinf(inf)
  53. assert isinf(-inf)
  54. assert not isinf(mpf(0))
  55. assert not isinf(nan)
  56. def test_special_powers():
  57. assert inf**3 == inf
  58. assert isnan(inf**0)
  59. assert inf**-3 == 0
  60. assert (-inf)**2 == inf
  61. assert (-inf)**3 == -inf
  62. assert isnan((-inf)**0)
  63. assert (-inf)**-2 == 0
  64. assert (-inf)**-3 == 0
  65. assert isnan(nan**5)
  66. assert isnan(nan**0)
  67. def test_functions_special():
  68. assert exp(inf) == inf
  69. assert exp(-inf) == 0
  70. assert isnan(exp(nan))
  71. assert log(inf) == inf
  72. assert isnan(log(nan))
  73. assert isnan(sin(inf))
  74. assert isnan(sin(nan))
  75. assert atan(inf).ae(pi/2)
  76. assert atan(-inf).ae(-pi/2)
  77. assert isnan(sqrt(nan))
  78. assert sqrt(inf) == inf
  79. def test_convert_special():
  80. float_inf = 1e300 * 1e300
  81. float_ninf = -float_inf
  82. float_nan = float_inf/float_ninf
  83. assert mpf(3) * float_inf == inf
  84. assert mpf(3) * float_ninf == -inf
  85. assert isnan(mpf(3) * float_nan)
  86. assert not (mpf(3) < float_nan)
  87. assert not (mpf(3) > float_nan)
  88. assert not (mpf(3) <= float_nan)
  89. assert not (mpf(3) >= float_nan)
  90. assert float(mpf('1e1000')) == float_inf
  91. assert float(mpf('-1e1000')) == float_ninf
  92. assert float(mpf('1e100000000000000000')) == float_inf
  93. assert float(mpf('-1e100000000000000000')) == float_ninf
  94. assert float(mpf('1e-100000000000000000')) == 0.0
  95. def test_div_bug():
  96. assert isnan(nan/1)
  97. assert isnan(nan/2)
  98. assert inf/2 == inf
  99. assert (-inf)/2 == -inf