-
Notifications
You must be signed in to change notification settings - Fork 34
More efficient conversion of fmpz to float #323
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
Open
remyoudompheng
wants to merge
6
commits into
flintlib:main
Choose a base branch
from
remyoudompheng:doubleconv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4da6b11
More efficient conversion of fmpz to float
remyoudompheng b3656ca
Add fast path for float conversion of numbers below 1023 bits
remyoudompheng 9382a42
Implement round-to-even compliant rounding
remyoudompheng 91dc079
Additional fmpz to float conversion tests
remyoudompheng 5896ada
Improve conversion of small or negative fmpz to float
remyoudompheng 6a5370c
Update release notes
remyoudompheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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: | ||
|
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. Shouldn't you allow maximal exponent (1024) too? BTW, you might rely on sys.float_info parameters, instead of assuming IEEE doubles. |
||
| # 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): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Looking 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^53here 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_ddoes which is to check if the result is in the range[-2^53, 2^53]before casting todoubleand 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 forpylong_as_slongas well.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.
Actually 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.