[New-bugs-announce] [issue36934] C API Function PyLong_AsDouble Returning Wrong Value

Farhan Sajjad report at bugs.python.org
Wed May 15 22:43:21 EDT 2019


New submission from Farhan Sajjad <sajjadfx at outlook.com>:

Found this rather obscure behavior where certain 64 bit numbers are changing (probably losing precision somewhere down the call chain) if converted from PyLong to double using the PyLong_AsDouble C API function.

TO REPRODUCE:

#define __SIZEOF_STRS__ 512

static PyObject*
test_pylong(PyObject* self, PyObject* args)
{
    char rBuffer[__SIZEOF_STRS__];
    char* strValue;
    if (!PyArg_ParseTuple(args, "s", &strValue))
    return NULL;

    {
        printf("%s AS INGRESS\n", strValue);
        double dblValue = PyLong_AsDouble(
                PyLong_FromString(strValue, NULL, 10));

        snprintf(rBuffer, __SIZEOF_STRS__, "%.0f",
                 PyLong_AsDouble(PyLong_FromString(strValue, NULL, 10)));

        printf("CONVERT 1: %.0f\nCONVERT 2: %s\n", dblValue, rBuffer);

    }

    Py_RETURN_NONE;
}

Test:
>>> test_pylong("1639873214337061279")
1639873214337061279 AS INGRESS
CONVERT 1: 1639873214337061376
CONVERT 2: 1639873214337061376

----------
messages: 342619
nosy: sajjadfx
priority: normal
severity: normal
status: open
title: C API Function PyLong_AsDouble Returning Wrong Value
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36934>
_______________________________________


More information about the New-bugs-announce mailing list