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

Floating point equality comparison fails in tests #41

Closed
AMDmi3 opened this issue Apr 26, 2022 · 3 comments
Closed

Floating point equality comparison fails in tests #41

AMDmi3 opened this issue Apr 26, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@AMDmi3
Copy link

AMDmi3 commented Apr 26, 2022

FreeBSD 13.0, elementpath 2.5.0, some tests fail:

============================= test session starts ==============================
platform freebsd13 -- Python 3.8.13, pytest-7.1.1, pluggy-0.13.1
rootdir: /work/usr/ports/textproc/py-elementpath/work-py38/elementpath-2.5.0
collected 1953 items

tests/test_datatypes.py ................................................ [  2%]
.................................................................        [  5%]
tests/test_exceptions.py ..                                              [  5%]
tests/test_helpers.py ......                                             [  6%]
tests/test_namespaces.py ....                                            [  6%]
tests/test_package.py ..                                                 [  6%]
tests/test_regex.py .................................................... [  9%]
......                                                                   [  9%]
tests/test_schema_context.py sssssss                                     [  9%]
tests/test_schema_proxy.py ssssssssssssssssssssssssssssssssssssss        [ 11%]
tests/test_selectors.py ....                                             [ 11%]
tests/test_tdop_parser.py ...................                            [ 12%]
tests/test_typing.py s                                                   [ 13%]
tests/test_xpath1_parser.py ............................................ [ 15%]
........................................................................ [ 18%]
...................................                                      [ 20%]
tests/test_xpath2_constructors.py ...................................... [ 22%]
..................................                                       [ 24%]
tests/test_xpath2_functions.py .............s........................... [ 26%]
..........ss............................................s............... [ 30%]
......................ss...............................                  [ 33%]
tests/test_xpath2_parser.py ..............s............................. [ 35%]
........s............................................................... [ 38%]
...............................s.....................................s.. [ 42%]
........................................................................ [ 46%]
......                                                                   [ 46%]
tests/test_xpath30.py ....................s............................. [ 49%]
.......F.................s.............................................. [ 52%]
........................................................................ [ 56%]
....s....................................F.................s............ [ 60%]
........................................................................ [ 63%]
...............................s.....................................ss. [ 67%]
...........................................s............................ [ 71%]
.........ss............................................................. [ 75%]
..............................................................s......... [ 78%]
...........................F.................s.......................... [ 82%]
........................................................................ [ 86%]
........................s....................................F.......... [ 89%]
.......s................................................................ [ 93%]
......................................                                   [ 95%]
tests/test_xpath_context.py ....................                         [ 96%]
tests/test_xpath_nodes.py .....................                          [ 97%]
tests/test_xpath_token.py .....................ss.........ss..s....s.... [ 99%]
...                                                                      [100%]

=================================== FAILURES ===================================
___________________ XPath30ParserTest.test_exp_math_function ___________________

self = <tests.test_xpath30.XPath30ParserTest testMethod=test_exp_math_function>

    def test_exp_math_function(self):
        token = self.parser.parse('math:exp(())')
        self.assertIsNone(token.evaluate())
        self.assertEqual(self.parser.parse('math:exp(0)').evaluate(), 1.0)
>       self.assertEqual(self.parser.parse('math:exp(1)').evaluate(), 2.718281828459045)
E       AssertionError: 2.7182818284590455 != 2.718281828459045

tests/test_xpath30.py:133: AssertionError
_________________ LxmlXPath3ParserTest.test_exp_math_function __________________

self = <tests.test_xpath30.LxmlXPath3ParserTest testMethod=test_exp_math_function>

    def test_exp_math_function(self):
        token = self.parser.parse('math:exp(())')
        self.assertIsNone(token.evaluate())
        self.assertEqual(self.parser.parse('math:exp(0)').evaluate(), 1.0)
>       self.assertEqual(self.parser.parse('math:exp(1)').evaluate(), 2.718281828459045)
E       AssertionError: 2.7182818284590455 != 2.718281828459045

tests/test_xpath30.py:133: AssertionError
___________________ XPath31ParserTest.test_exp_math_function ___________________

self = <tests.test_xpath30.XPath31ParserTest testMethod=test_exp_math_function>

    def test_exp_math_function(self):
        token = self.parser.parse('math:exp(())')
        self.assertIsNone(token.evaluate())
        self.assertEqual(self.parser.parse('math:exp(0)').evaluate(), 1.0)
>       self.assertEqual(self.parser.parse('math:exp(1)').evaluate(), 2.718281828459045)
E       AssertionError: 2.7182818284590455 != 2.718281828459045

tests/test_xpath30.py:133: AssertionError
_________________ LxmlXPath31ParserTest.test_exp_math_function _________________

self = <tests.test_xpath30.LxmlXPath31ParserTest testMethod=test_exp_math_function>

    def test_exp_math_function(self):
        token = self.parser.parse('math:exp(())')
        self.assertIsNone(token.evaluate())
        self.assertEqual(self.parser.parse('math:exp(0)').evaluate(), 1.0)
>       self.assertEqual(self.parser.parse('math:exp(1)').evaluate(), 2.718281828459045)
E       AssertionError: 2.7182818284590455 != 2.718281828459045

tests/test_xpath30.py:133: AssertionError
=========================== short test summary info ============================
FAILED tests/test_xpath30.py::XPath30ParserTest::test_exp_math_function - Ass...
FAILED tests/test_xpath30.py::LxmlXPath3ParserTest::test_exp_math_function - ...
FAILED tests/test_xpath30.py::XPath31ParserTest::test_exp_math_function - Ass...
FAILED tests/test_xpath30.py::LxmlXPath31ParserTest::test_exp_math_function
============ 4 failed, 1873 passed, 76 skipped in 82.21s (0:01:22) =============

It is well known that floating point values cannot be tested for equality, these should probably be replaced with epsilon-comparison, such as https://docs.pytest.org/en/4.6.x/reference.html#pytest-approx

@brunato
Copy link
Member

brunato commented Apr 27, 2022

Hi,
yes, it's my superficiality on some tests.
I will use math.isclose() for fixing faulty tests, that is already used on others.

Thanks

brunato added a commit that referenced this issue Apr 28, 2022
  - Use assertAlmostEqual() for comparing float values
@brunato
Copy link
Member

brunato commented Apr 28, 2022

Used assertAlmostEqual() of the unittest library.
Please check v2.5.1 with FreeBSD and, if the tests are good, close this issue.

@AMDmi3
Copy link
Author

AMDmi3 commented Apr 28, 2022

Fix confirmed

@AMDmi3 AMDmi3 closed this as completed Apr 28, 2022
@brunato brunato added the bug Something isn't working label Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants