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

Cannot convert Rational to float #227

Closed
Wrzlprmft opened this issue Apr 9, 2018 · 8 comments
Closed

Cannot convert Rational to float #227

Wrzlprmft opened this issue Apr 9, 2018 · 8 comments

Comments

@Wrzlprmft
Copy link

Wrzlprmft commented Apr 9, 2018

from symengine import Rational
x = Rational(2,3)
print(float(x))

raises a TypeError.

The best workaround I found so far is float(x.n().real_part()).

@certik
Copy link
Contributor

certik commented Jan 17, 2019

@Wrzlprmft thank you for the bug report. @isuruf fixed it in #265.

@Wrzlprmft
Copy link
Author

Wrzlprmft commented Feb 13, 2019

Unfortunately the fix (or some other recent change) seems to destroy my work-around (see neurophysik/jitcdde#17). More specifically, symengine.Float(42).real_part does not exist (only symengine.Float(42).real).

Can you either make a release (that allows me to pinpoint the version requirements) or implement a real_part attribute for Floats for backwards compatibility? Since everything is hardcoded, I can not monkey-patch this either.

@isuruf
Copy link
Member

isuruf commented Feb 13, 2019

That fix should be backwards compatible. Which version of symengine are you using?

@Wrzlprmft
Copy link
Author

That fix should be backwards compatible. Which version of symengine are you using?

I am comparing a version I pulled a few minutes ago from GitHub and the latest pre-release from PyPI (0.3.1.dev1). What I need something that converts any of SymEngine’s non-complex number types (Integer, Rational, Float, …) to a Python float and works in both of these versions (or a new release/pre-release).

Suppose x is my SymEngine number (e.g., x=symengine.Rational(2,3)). In the current version, I can just use float(x), but that does not work in the pre-release. In the pre-release I can use float(x.n().real_part()) but that does not work in the current version (“'RealDouble' object has no attribute 'real_part'”); the closest I can get is float(x.n().real).

@certik
Copy link
Contributor

certik commented Feb 13, 2019

@Wrzlprmft is the issue that you are trying to make your code work with multiple versions of symengine?

Could one have a check based on a version of symengine and do what works for each? I understand that's not ideal, but at least it's some way forward.

@isuruf
Copy link
Member

isuruf commented Feb 13, 2019

@Wrzlprmft, use float(x.n(real=True)) which should work in both versions.

@Wrzlprmft
Copy link
Author

@Wrzlprmft is the issue that you are trying to make your code work with multiple versions of symengine?

Correct. Alternatively, I can deal with this using explicitly requiring a version, but that would require a release.

Could one have a check based on a version of symengine and do what works for each? I understand that's not ideal, but at least it's some way forward.

sympy.__version__ returns 0.3.1.dev1 in both cases, so that wouldn’t work. What I could do is to catch the exception, but I see no easy way to avoid catching some real problems due to faulty user input or similar. Anyway:

@Wrzlprmft, use float(x.n(real=True)) which should work in both versions.

Indeed. That solves my problem. Thank you.

@certik
Copy link
Contributor

certik commented Feb 13, 2019

@Wrzlprmft perfect, I am glad it's solved. Sorry about the inconvenience.

Wrzlprmft pushed a commit to neurophysik/jitcdde that referenced this issue Feb 13, 2019
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

3 participants