This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: How to use `long double` as a PyObject?
Type: enhancement Stage: resolved
Components: C API Versions: Python 3.10
Status: closed Resolution: postponed
Dependencies: Superseder:
Assigned To: Nosy List: ganesh3597, mark.dickinson, terry.reedy
Priority: normal Keywords:

Created on 2021-02-09 08:40 by ganesh3597, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg386698 - (view) Author: Ganesh Kathiresan (ganesh3597) Date: 2021-02-09 08:40
I wanted to know if there is a way to parse and store c `long double`. I only found `PyFloat_FromDouble` and in the same page, `PyFloat_GetMax` returns: 
> Return the maximum representable finite float DBL_MAX as C double.

Meaning, there is no scope of `long double`. Can you please tell me how I can do something like `PyFloat_FromLongDouble`
msg386714 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-02-09 14:12
You're correct that there's nothing in the C-API to handle a C `long double`. (Similarly, there's very little for C `float` values.)

You've added "ctypes" as a component, but in the message you're talking about the C-API. Can you clarify what you're doing? Are you writing C extensions for Python, or are you using ctypes?
msg386715 - (view) Author: Ganesh Kathiresan (ganesh3597) Date: 2021-02-09 14:15
Ohh ok, thanks for the info Mark. Any plans to add it in the future?

My bad, sorry, will change to C-API.
msg386718 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-02-09 14:33
> Any plans to add it in the future?

It depends - it's not really clear to me what you're asking for.

If you just want to get a Python `float` from a C long double, then cast to double in the C code and then use `PyFloat_FromDouble`. Obviously that loses any extra precision that the C long double had.

If you're asking for Python to grow a new type of "float" with long double precision, that's a *much* bigger ask, and realistically it's not going to happen in the forseeable future.
msg386719 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-02-09 14:34
I'm setting the Python version to 3.10 here: this is a request for a new feature, and as such it can't go into already-released Python versions.
msg386723 - (view) Author: Ganesh Kathiresan (ganesh3597) Date: 2021-02-09 14:39
Oh I see. Yeah my ask is particularly around support for long double precision in python. What I am trying to do is essentially attempt to speed up the comparison of NumPy scalars: 

We have support for float128 in NumPy, but when I was trying to cast it to a C variable, was having a hard time doing so without losing precision.

Please feel free to close the issue, hopefully will see long double support sometime in the future.
msg386724 - (view) Author: Ganesh Kathiresan (ganesh3597) Date: 2021-02-09 14:43
Oh apologies for changing the version, makes sense for 3.10. I had it opened before the change and sort of conflicted into taking my old preset 3.7 on the page
msg386893 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-02-13 00:25
You immediate questions are answered.  Adding 128 bit floats to Python itself need a discussion on python-ideas and very likely a PEP.  Maybe a decade?
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87343
2021-02-13 00:25:21terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg386893

resolution: postponed
stage: resolved
2021-02-09 14:43:00ganesh3597setmessages: + msg386724
2021-02-09 14:39:24ganesh3597setmessages: + msg386723
2021-02-09 14:34:28mark.dickinsonsetmessages: + msg386719
versions: + Python 3.10, - Python 3.7
2021-02-09 14:33:47mark.dickinsonsetmessages: + msg386718
2021-02-09 14:15:00ganesh3597setmessages: + msg386715
components: + C API, - ctypes
versions: + Python 3.7, - Python 3.10
2021-02-09 14:14:31mark.dickinsonsetversions: + Python 3.10, - Python 3.7
2021-02-09 14:12:03mark.dickinsonsetnosy: + mark.dickinson
messages: + msg386714
2021-02-09 08:40:00ganesh3597create