Skip to content

Decide the fate of missing C99 functions in math #135853

Open
@picnixz

Description

@picnixz

Feature or enhancement

Proposal:

I will first post here because I don't want to flood DPO with a controversial thread. I didn't dig much in the issues so it might have already been discussed a bit more in detail elsewhere.

There are some functions in math.h that we don't export in math as some are useless for Python or just forgotten. We recently added isnormal and issubnormal but the following are missing:

scalbn

It's only useful if FLT_RADIX != 2. Python only supports FLT_RADIX = 2 or 16 but I don't know how many systems have FLT_RADIX = 16. So I don't think we need it for now. logb also depends on FLT_RADIX but I think we can ignore it if we don't add scalbn.

DECISION: let's not add it as numpy doesn't have it and it has a confusing name (at least to me).

fdim

This one could be useful to handle possible signed zeros when doing fp artihmetic. Similarly, math currently lacks fmax and fmin which could useful if we support IEC 60559 because max(nan, inf) == nan and max(inf, nan) == inf which is incorrect (in both cases fmax would have returned inf on such systems).

DECISION: numpy has fmax and fmin and treat the NaNs correctly, but not fdim. For now, let's not add it.

remquo

This one is useful for implementing $t$-periodic functions for $t$ exactly representable as an fp-value. I don't know if users are expected to actually work in Python for such functions, but I think we can expose it.

DECISION: numpy doesn't have it and while it could be fine, it's actually really niche.

fpclassify

Now that we have normal and subnormal detection, I suggest we also add this function. This would however require new constants which may or may not be adequate as this would make the module a bit heavier. It was asked in #132908 (comment) but we didn't follow up with that later.

DECISION: I would personally like it but it would more for completeness rather than usefulness, and numpy doesn't have it so I won't push for it.

isunordered

Can be useful to detect if either operand is a NaN. It can save some math.isnan(x) or math.isnan(y) expression.

DECISION: it is useful but a better named helper in pure Python would do the job.

signbit

Useful for getting the sign of signed zeroes, infinities and NaN.

DECISION: I was wrong in my original statement as Mark pointed out but this one seems the most useful. So I'll work on it tomorrow.


I omitted some functions such as isless as they are directly implemented in Python and I don't think we should do something here. I also omitted rounding functions that depend on the rounding mode as those should be handled by Decimal instead.

Finally, it's unfortunate that we named math.gamma instead of math.tgamma as math.gamma could have been confused with Euler-Mascheroni's constant.

cc @skirpichev @tim-one

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

Metadata

Metadata

Assignees

Labels

extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions