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

isprime should error on floats #26489

Open
BorneNeedsHelp opened this issue Apr 10, 2024 · 11 comments
Open

isprime should error on floats #26489

BorneNeedsHelp opened this issue Apr 10, 2024 · 11 comments
Labels

Comments

@BorneNeedsHelp
Copy link

BorneNeedsHelp commented Apr 10, 2024

I was making a calculator and ran into an issue. 2 in not prime?? What!? I looked up tutorials, but still. 2 = false or not prime please help!

@BorneNeedsHelp BorneNeedsHelp changed the title 2 is not prime on IsPrime() ??? 2 is not prime on IsPrime()??? Apr 10, 2024
@asmeurer
Copy link
Member

What SymPy code are you using that gives you this? SymPy does not have a function called IsPrime. It does have a function called isprime, but it does treat 2 as prime:

>>> isprime(2)
True

@BorneNeedsHelp
Copy link
Author

BorneNeedsHelp commented Apr 11, 2024

Yup! I was using floats, not integers! Thanks!

@BorneNeedsHelp BorneNeedsHelp changed the title 2 is not prime on IsPrime()??? 2 is not prime on isprime()??? Apr 11, 2024
@asmeurer
Copy link
Member

I was about to suggest just that. It might be better if isprime just errored if the input is a float. Returning False for an integral float is confusing.

@asmeurer
Copy link
Member

This would be trivial to change, by the way. Just delete these lines

except ValueError:
return False
.

Looks like this comes from #9670. It's good for the assumptions to return False here, but the isprime() function would be better raising an exception.

@asmeurer asmeurer reopened this Apr 11, 2024
@asmeurer asmeurer changed the title 2 is not prime on isprime()??? isprime should error on floats Apr 11, 2024
@haru-44
Copy link
Member

haru-44 commented Apr 11, 2024

I agree with the change.

@Abhishekjsr283204
Copy link
Contributor

@haru-44 sir we need to delete those lines suggested by @asmeurer and can i make a PR should we need to add the test cases.?

@asmeurer
Copy link
Member

You'll also need to update the code in Q.isprime to catch the exception and continue to give False for floats.

@shishir-11
Copy link
Contributor

Would it be better if instead of an error we internally converted them to integers(if the value was integral) or is there any particular reason to be raising an error on those values as well?

@asmeurer
Copy link
Member

You can see the discussion on the old PR I linked to on why integral floats aren't considered prime.

@shishir-11
Copy link
Contributor

I tried the method given in the documentation since i wanted to take a look at the working

SYMPY_DEBUG=True bin/isympy

but in the sympy interactive terminal I still just get the output

In [1]: ask(Q.prime(2))
Out[1]: True

Can someone please inform me about what I am doing wrong.

@asmeurer
Copy link
Member

There's just not any debug output configured for ask. You'll need to just dig into the source code to see where isprime is being used.

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

No branches or pull requests

5 participants