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

ceil() of algebraic numbers is buggy #39345

Open
2 tasks done
dimpase opened this issue Jan 18, 2025 · 4 comments
Open
2 tasks done

ceil() of algebraic numbers is buggy #39345

dimpase opened this issue Jan 18, 2025 · 4 comments

Comments

@dimpase
Copy link
Member

dimpase commented Jan 18, 2025

Steps To Reproduce

sage: x = QQbar(sqrt(8))
....: ceil(x-2*QQbar(sqrt(2)))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[41], line 2
      1 x = QQbar(sqrt(Integer(8)))
----> 2 ceil(x-Integer(2)*QQbar(sqrt(Integer(2))))

File /mnt/opt/Sage/sage-clang/src/sage/functions/other.py:433, in Function_ceil.__call__(self, x, **kwds)
    416 def __call__(self, x, **kwds):
    417     """
    418     Allow an object of this class to behave like a function. If
    419     ``ceil`` is an instance of this class, we can do ``ceil(n)`` to get
   (...)
    431         -2
    432     """
--> 433     return _eval_floor_ceil(self, x, "ceil", **kwds)

File /mnt/opt/Sage/sage-clang/src/sage/functions/other.py:297, in _eval_floor_ceil(self, x, method, bits, **kwds)
    293         continue
    295     bits *= 2
--> 297 raise ValueError("cannot compute {}({!r}) using {} bits of precision".format(method, x, RIF.precision()))

ValueError: cannot compute ceil(0.?e-307) using 512 bits of precision

Documentation of ceil() says:

3. If none of the above work, Sage returns a "Expression" object.

This is contradicting next pieces of the docs:

   Small numbers which are extremely close to an integer are hard to
   deal with:

      sage: ceil((33^100 + 1)^(1/100))
      Traceback (most recent call last):
      ...
      ValueError: cannot compute ceil(...) using 256 bits of precision

The latter is not an Expression.

Worse, increasing the precision does not seem to have an effect:

ValueError: cannot compute ceil(0.?e-157825) using 262144 bits of precision

Is this a bug in RIF?

This was reported to me by @Ilia-Smilga

Expected Behavior

it should work

Actual Behavior

it doesn't

Additional Information

Note that in this case, one can immediately see that we're computing ceil of an algebraic number 0:

sage: n=t-2*QQbar(sqrt(2))
sage: n.minpoly()
x

The latter can be used as follows:

sage: f=n.minpoly()
sage: f.variables()[0]==f
True
sage: f.variables()
(x,)
sage: f.variables()[0]==f
True

Environment

current Sage beta

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
@dimpase
Copy link
Member Author

dimpase commented Jan 18, 2025

@dimpase
Copy link
Member Author

dimpase commented Jan 18, 2025

Should we implement ceil for algebraic numbers via the minpoly trick as above ?

@Ilia-Smilga
Copy link

Note that there is already some code written to handle algebraic numbers that appear almost indistinguishable from 0 - namely the functions bool(self) and sign(self) in the QQbar module ( https://github.com/sagemath/sage/blob/develop/src/sage/rings/qqbar.py ). So ceil() should maybe call one of these two functions.

@mezzarobba
Copy link
Member

There is a (slow but) working implementation of ceil() for (real) algebraic numbers:

sage: x = AA(sqrt(8))
sage: x.ceil()
3

The issue here appears to be with the symbolic function ceil.

Related issues: #22079, #25827, #12121, #18333.

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

3 participants