Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert == on two floating numbers? Really? #15221

Open
mcepl opened this issue May 11, 2023 · 1 comment
Open

assert == on two floating numbers? Really? #15221

mcepl opened this issue May 11, 2023 · 1 comment

Comments

@mcepl
Copy link

mcepl commented May 11, 2023

assert repr(actual) == repr(expected), (

No wonder this immediately fails on i586 while packaging for openSUSE. Add a churn around integer math with CVE-2020-10735 and it is no wonder it doesn’t equal at all. Well, as if those things were needed over the general Programming 101 rule “Don’t use equal for floating point numbers!”.

[ 2168s] _________________________________ testMathOps __________________________________
[ 2168s] [gw0] linux -- Python 3.9.16 /usr/bin/python3.9
[ 2168s] data: /home/abuild/rpmbuild/BUILD/mypy-1.3.0/mypyc/test-data/run-math.test:3:
[ 2168s] Failed: Invalid output (/home/abuild/rpmbuild/BUILD/mypy-1.3.0/mypyc/test-data/run-math.test, line 3)
[ 2168s] ----------------------------- Captured stdout call -----------------------------
[ 2168s] 
[ 2168s] *** Exit status: 1
[ 2168s] ----------------------------- Captured stderr call -----------------------------
[ 2168s] Expected:
[ 2168s] Actual:
[ 2168s]   Traceback (most recent call last):            (diff)
[ 2168s]     File "driver.py", line 43, in <module>      (diff)
[ 2168s]       raise failures[-1][1]                     (diff)
[ 2168s]     File "driver.py", line 19, in <module>      (diff)
[ 2168s]       test_func()                               (diff)
[ 2168s]     File "run-math.test", line 50, in test_sqrt (diff)
[ 2168s]       validate_one_arg(lambda x: math.sqrt(x), pymath.sqrt) (diff)
[ 2168s]     File "run-math.test", line 24, in validate_one_arg (diff)
[ 2168s]       assert repr(actual) == repr(expected), (  (diff)
[ 2168s]   AssertionError: actual for 1.7976931348623157e+308: 1.3407807929942597e+154, expected 1.3407807929942596e+154 (diff)
[ 2168s] 

Complete build log with all details about the packages used and reproduction.

@mcepl
Copy link
Author

mcepl commented May 12, 2023

Couldn’t you at least something as silly as:

---
 mypyc/test-data/run-math.test |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Index: mypy-1.3.0/mypyc/test-data/run-math.test
===================================================================
--- mypy-1.3.0.orig/mypyc/test-data/run-math.test
+++ mypy-1.3.0/mypyc/test-data/run-math.test
@@ -8,6 +8,8 @@ from testutil import assertRaises, float
 
 pymath: Any = math
 
+FLOAT_MIN_DIFF = 0.0001
+
 def validate_one_arg(test: Callable[[float], float], validate: Callable[[float], float]) -> None:
     """Ensure that test and validate behave the same for various float args."""
     for x in float_vals:
@@ -21,9 +23,10 @@ def validate_one_arg(test: Callable[[flo
                 assert repr(e) == repr(e2), f"actual for {x!r}: {e2!r}, expected: {e!r}"
                 continue
         actual = test(x)
-        assert repr(actual) == repr(expected), (
+        assert (abs(actual - expected) / expected) < FLOAT_MIN_DIFF, (
             f"actual for {x!r}: {actual!r}, expected {expected!r}")
 
+
 def validate_two_arg(test: Callable[[float, float], float],
                      validate: Callable[[float, float], float]) -> None:
     """Ensure that test and validate behave the same for various float args."""
@@ -43,7 +46,7 @@ def validate_two_arg(test: Callable[[flo
                 actual = test(x, y)
             except Exception as e:
                 assert False, f"no exception expected for {args}, got {e!r}"
-            assert repr(actual) == repr(expected), (
+            assert (abs(actual - expected) / expected) < FLOAT_MIN_DIFF, (
                 f"actual for {args}: {actual!r}, expected {expected!r}")
 
 def test_sqrt() -> None:

bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this issue May 14, 2023
https://build.opensuse.org/request/show/1086859
by user mcepl + dimstar_suse
- Skip over failing testMathOps (gh#python/mypy#15221).
- Update to 1.3.0:
  - Improve performance of union subtyping
  - Add negative subtype caches
  - Stubtest: Check that the stub is abstract if the runtime is,
    even when the stub is an overloaded method
  - Stubtest: Verify stub methods or properties are decorated
    with @Final if they are decorated with @Final at runtime
  - Stubtest: Fix stubtest false positives with TypedDicts at
    runtime
  - Stubgen: Support @functools.cached_property
  - Improvements to stubgenc
  - Add support for converters with TypeVars on generic attrs
    classes
  - Fix attrs.evolve on bound TypeVar
  - Improve async documentation
  - Improvements to cheat sheet
  - Add documentation for bytes formatting error code
  - Convert insecure links to use H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants