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

Consider adding public PyLong_FlipSign() function #120446

Open
skirpichev opened this issue Jun 13, 2024 · 4 comments
Open

Consider adding public PyLong_FlipSign() function #120446

skirpichev opened this issue Jun 13, 2024 · 4 comments
Labels
topic-C-API type-feature A feature request or enhancement

Comments

@skirpichev
Copy link
Member

skirpichev commented Jun 13, 2024

Feature or enhancement

Proposal:

Currently there is only a slow way (i.e. PyNumber_Negative()) to invert the sign of a big integer. Direct manipulation of the sign value is available only via private function, e.g. _PyLong_SetSignAndDigitCount():
https://github.com/aleaxit/gmpy/blob/1c0d4f38d4f1093df0e9460d82e4331d3d3946b6/src/gmpy2_convert_gmp.c#L153

I propose this interface:

int PyLong_FlipSign(PyObject *obj);

For small integers it will fallback to PyLong_FromLong() (like _PyLong_Negate() does), for the rest - just _PyLong_FlipSign() to change sign in place.

Alternatively, we could just expose a direct wrapper of _PyLong_FlipSign() and warn users to not use this function for small ints (that doesn't make sense for me anyway).

In both variants, on success - flip the sign of obj and return 0. On failure - return -1 with exception set. Shouldn't fail on PyLongObject or it's subtype.

I'll work on a patch if this proposal does make sense.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@skirpichev skirpichev added the type-feature A feature request or enhancement label Jun 13, 2024
@serhiy-storchaka
Copy link
Member

I planned to propose adding PyLong_NegativeInPlace() and PyLong_AbsoluteInPlace().

@skirpichev
Copy link
Member Author

Hmm, it seems that my first variant can't be implemented with same signature as the second. Rather it should be (like _PyLong_Negate()):

int PyLong_FlipSign(PyObject **obj);

@skirpichev
Copy link
Member Author

adding PyLong_NegativeInPlace() and PyLong_AbsoluteInPlace().

I think this naming better fits case, when there will be also other PyNumber_*-like functions. But I'm not sure that we have practical need for that.

FYI, PR: #120489 (as a draft; IIUC, this should be discussed in C-API WG)

@skirpichev
Copy link
Member Author

PR closed in favor of #121339.

Now I don't have a strong motivation for this function. Kept open as @serhiy-storchaka planned a similar proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants