Skip to content

Conversation

@ganesh-k13
Copy link
Member

@ganesh-k13 ganesh-k13 commented May 28, 2025

Changes

Add float.from_number for floating types

Testing

In [3]: np.float16.from_number(7), type(np.float16.from_number(7))
Out[3]: (np.float16(7.0), numpy.float16)

In [4]: np.float16.from_number(100000000000000000)
Out[4]: np.float16(inf)

In [5]: np.float16.from_number('a')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: must be real number, not str

Related

part of #13375

Todo

Float:

  • release notes
  • UT
  • typing
  • docs

Complex

  • release notes
  • UT
  • typing
  • docs

@ganesh-k13 ganesh-k13 self-assigned this May 28, 2025
@ganesh-k13 ganesh-k13 added 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. labels May 28, 2025
@ganesh-k13 ganesh-k13 requested a review from jorenham June 9, 2025 08:52
@ganesh-k13 ganesh-k13 marked this pull request as ready for review June 9, 2025 09:16
@ganesh-k13 ganesh-k13 removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jun 9, 2025
Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's also a complex.from_number in Python π, maybe it would make sense to also include complexfloating, e.g. by adding np.inexact.from_number?

@mattip mattip requested a review from seberg October 16, 2025 07:49
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, sorry we never pushed this. I think it really just needs some minor tweaks. (I wonder a bit if the tests can be more concise, but probably not!)

static PyObject *
@name@_from_number(PyObject *cls, PyObject *arg)
{
npy_double val = PyFloat_AsDouble(arg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry that I never looked at this. I think this is actually very nice to add and we should totally do that.

In a sense, this is not quite correct, because we should support np.longdouble.from_number(np.longdouble(2).sqrt()) to round-trip. That said, we don't actually care too much about long-double and it is the only one with this problem.
So maybe we can just special case that one even if we might not care much?

(Or if we dislike things a lot, we just omit it for longdouble.)

def test_from_number_exceptions(self, code):
cls = np.dtype(code).type
with pytest.raises(TypeError):
cls.from_number("numpy")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test should use a valid float "2.3" and maybe we should also test NumPy strings/string arrays.

Can we also check for only __index__ to match Python?

@ganesh-k13
Copy link
Member Author

My notifications got missed! I will address the comments, thanks a lot for the review!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

01 - Enhancement 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants