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

Problem with the integers #3

Closed
timsherwood opened this issue Jul 9, 2023 · 1 comment
Closed

Problem with the integers #3

timsherwood opened this issue Jul 9, 2023 · 1 comment

Comments

@timsherwood
Copy link

Sorry, not sure if this related to the update just pushed, or some other issue, but now there seems to be some problem with the integers? Both with as_tuple and even with normal arithmetic... could be my misunderstanding of the correct behavior though. This is with the most recent version just pushed.

The powers of p have the same tuple representation

>>> PAdic(1,2,4)
1 + O(2^4)
>>> PAdic(2,2,4)
1*2 + O(2^5)
>>> PAdic(4,2,4)
1*2^2 + O(2^6)
>>> PAdic(1,2,4).as_tuple
(1, 0, 0, 0)
>>> PAdic(2,2,4).as_tuple
(1, 0, 0, 0)
>>> PAdic(4,2,4).as_tuple
(1, 0, 0, 0)

Normal arithmetic seems broken

>>> PAdic(2,2,4) + PAdic(2,2,4) == PAdic(4,2,4)
False
>>> PAdic(2,2,4) + PAdic(2,2,4)
1*2^2 + O(2^5)
>>> PAdic(4,2,4)
1*2^2 + O(2^6)
@GDeLaurentis
Copy link
Owner

The idea for .as_tuple is to represent the mantissa only, see Eq. 3.13 of arXiv:2203.04269. More specifically, the entries of the tuple are the $a_i$'s of Eq. 3.13. These are a series of numbers between $0$ and $p-1$, included. Similarly to the usual real/complex floating-point numbers, different numbers may have the same mantissa (a.k.a. significand) but differ in the exponent part. For PAdic the exponent is stored in .n.

>>> PAdic(1,2,4).n
0
>>> PAdic(2,2,4).n
1
>>> PAdic(4,2,4).n
2

As for the weird behavior with equality, this only happens when a number becomes proportional to the prime, and thus the number of significant digits changes. I wouldn't say this is a bug. For instance,

>>> PAdic(1,3,4) + PAdic(1,3,4) == PAdic(2,3,4)
True

works as expected. The reason why

>>> PAdic(2,2,4) + PAdic(2,2,4) == PAdic(4,2,4)

doesn't return True is that the number of significant digits differs on the two sides, as you show in your message one is O(2^5), and the other is O(2^6). PAdic(2,2,4) was instantiated with 4 significant digits, but after the sum only 3 significant digits are left. The equality has no way to know if the $4^{th}$ digit is the same, so it returns False. Using .as_tuple we see clearly the difference:

>>> (PAdic(2,2,4) + PAdic(2,2,4)).as_tuple
(1, 0, 0)
>>> PAdic(4,2,4).as_tuple
(1, 0, 0, 0)

An analogous example with real floating-point numbers could be

>>> 1.00000000000000002 - 1.00000000000000001 == 0.00000000000000002 - 0.00000000000000001
False

because the left-hand side gives the approximate result 0.0, while the right-hand side correctly returns 1e-17 . In general, I would say that it is safer to check if two numbers are close, rather than equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants