Skip to content

Test failure due to rounding issues #41056

@whoami730

Description

@whoami730

Steps To Reproduce

sage -t --warn-long 5.0 --random-seed=174653573728956284690523116673537031147 src/sage/rings/number_field/number_field_element_quadratic.pyx

Rounding differences lead to assertion error because RealNumber is rounded away from zero whereas rationals seem to round to even number in case of ties.

Encountered in this run

Expected Behavior

src/bin/sage -t --warn-long 5.0 --random-seed=174653573728956284690523116673537031147 src/sage/rings/number_field/number_field_element_quadratic.pyx
**********************************************************************
Error: Failed example:: Exception raised:
Traceback (most recent call last):
  File "/usr/share/miniconda/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 734, in _run
    self.compile_and_execute(example, compiler, test.globs)
  File "/usr/share/miniconda/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1158, in compile_and_execute
    exec(compiled, globs)
  File "<doctest sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round[8]>", line 7, in <module>
    assert round(a+b*sqrt(RealNumber('2.'))) == round(a+b*sqrt2), (a, b)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: (-437/2, 0)

    for _ in range(100):
       a = QQ.random_element(1000,20)
       b = QQ.random_element(1000,20)
       assert a.round() == round(K2(a)), a
       assert a.round() == round(K3(a)), a
       assert a.round() == round(K5(a)), a
       assert round(a+b*sqrt(2.)) == round(a+b*sqrt2), (a, b)
       assert round(a+b*sqrt(3.)) == round(a+b*sqrt3), (a, b)
       assert round(a+b*sqrt(5.)) == round(a+b*sqrt5), (a, b)
Exception raised:
    Traceback (most recent call last):
      File "/usr/share/miniconda/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 734, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/usr/share/miniconda/envs/sage-dev/lib/python3.11/site-packages/sage/doctest/forker.py", line 1158, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round[8]>", line 7, in <module>
        assert round(a+b*sqrt(RealNumber('2.'))) == round(a+b*sqrt2), (a, b)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: (-437/2, 0)
**********************************************************************

Actual Behavior

Expected to succeed

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions