double_const.py 1.2 KB

123456789101112131415161718192021222324252627282930
  1. from test.test_support import TestFailed
  2. # A test for SF bug 422177: manifest float constants varied way too much in
  3. # precision depending on whether Python was loading a module for the first
  4. # time, or reloading it from a precompiled .pyc. The "expected" failure
  5. # mode is that when test_import imports this after all .pyc files have been
  6. # erased, it passes, but when test_import imports this from
  7. # double_const.pyc, it fails. This indicates a woeful loss of precision in
  8. # the marshal format for doubles. It's also possible that repr() doesn't
  9. # produce enough digits to get reasonable precision for this box.
  10. PI = 3.14159265358979324
  11. TWOPI = 6.28318530717958648
  12. PI_str = "3.14159265358979324"
  13. TWOPI_str = "6.28318530717958648"
  14. # Verify that the double x is within a few bits of eval(x_str).
  15. def check_ok(x, x_str):
  16. assert x > 0.0
  17. x2 = eval(x_str)
  18. assert x2 > 0.0
  19. diff = abs(x - x2)
  20. # If diff is no larger than 3 ULP (wrt x2), then diff/8 is no larger
  21. # than 0.375 ULP, so adding diff/8 to x2 should have no effect.
  22. if x2 + (diff / 8.) != x2:
  23. raise TestFailed("Manifest const %s lost too much precision " % x_str)
  24. check_ok(PI, PI_str)
  25. check_ok(TWOPI, TWOPI_str)