| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -3,6 +3,7 @@ from flint.utils.typecheck cimport typecheck | |
| from flint.utils.conversion cimport chars_from_str | ||
| from flint.utils.conversion cimport str_from_chars, _str_trunc | ||
| cimport libc.stdlib | ||
| from libc.math cimport nextafter | ||
|
|
||
| from flint.flintlib.types.flint cimport FMPZ_REF, FMPZ_TMP, FMPZ_UNKNOWN, COEFF_IS_MPZ | ||
| from flint.flintlib.functions.flint cimport flint_free | ||
| Expand Down Expand Up | @@ -105,6 +106,41 @@ cdef class fmpz(flint_scalar): | |
| return fmpz_get_intlong(self.val) | ||
|
|
||
| def __float__(self): | ||
| if not COEFF_IS_MPZ(self.val[0]): | ||
| return <double>(<slong>self.val[0]) | ||
| cdef slong bits = fmpz_bits(self.val) | ||
| cdef long tz | ||
| cdef double d | ||
| cdef int roundup = 0 | ||
| if bits <= 1023: | ||
|
Comment thread
Copy link
Copy Markdown
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityShouldn't you allow maximal exponent (1024) too? BTW, you might rely on sys.float_info parameters, instead of assuming IEEE doubles.
Sorry, something went wrong.
All reactions
|
||
| # Known to be representable by a IEEE-754 double | ||
| d = fmpz_get_d(self.val) | ||
| # fmpz_get_d always rounds towards zero | ||
| # Sometimes we need to round away from zero: | ||
| # - if the 54-th most significant bit is 1 | ||
| # - if further bits are not zero | ||
| # - or if all further bits are zero but the 53-th bit is 1 (round-to-even convention) | ||
| if fmpz_sgn(self.val) == -1: | ||
| # tstbit behaves as if self was represented as 2's complement | ||
| # We look for patterns 0.10...0 or x.0xxxxxx | ||
| tz = fmpz_val2(self.val) | ||
| if bits >= 54: | ||
| if fmpz_tstbit(self.val, bits - 54) == 0: | ||
| if tz < bits - 54: | ||
| roundup = 1 | ||
| else: | ||
| if tz == bits - 54 and fmpz_tstbit(self.val, bits - 53) == 0: | ||
| roundup = 1 | ||
| else: | ||
| if bits >= 54 and fmpz_tstbit(self.val, bits - 54) == 1: | ||
| tz = fmpz_val2(self.val) | ||
| if tz < bits - 54 or (tz == bits - 54 and fmpz_tstbit(self.val, bits - 53) == 1): | ||
| roundup = 1 | ||
| if roundup: | ||
| # increase the mantissa of d by 1 | ||
| d = nextafter(d, 2 * d) | ||
| return d | ||
|
|
||
| return float(fmpz_get_intlong(self.val)) | ||
|
|
||
| def __floor__(self): | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityLooking at the C23 standard text:
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf
In 6.3.1.4 it says:
In our case here I think that in a 32 bit build this is fine since every 32 bit integer can go directly to a double. For a 64 bit slong values greater than 2^53 here cannot be represented exactly and then the result would be implementation-defined so it's kind of unsafe even if it seems to work under testing right now.
I think it is best to do here what FLINT's fmpz_get_d does which is to check if the result is in the range [-2^53, 2^53] before casting to double and otherwise fall back on the other path.
That check is unnecessary in the 32-bit case though. We probably should define a function for this using preprocessor defines somewhere near here:
python-flint/src/flint/flintlib/types/flint.pxd
Lines 95 to 108 in 0a59f53
I think we want a macro something like
I guess checking sizeof(slong) is what should really be used for pylong_as_slong as well.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityActually it probably makes more sense to put the macro in fmpz.pyx where it is used. If other code wants to use it in future then we can consider putting it somewhere more central.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.