[Python] Numpy longdouble arithmetic does not seem to be in long double with conversion

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have been playing C99's quad precision long double. It is my understanding that (platform specific) numpy supports long double and 128bit floats.

    I have run across something I cannot explain however.

    Given:

    >>> import numpy as np


    Calculate a number that will require more than 64 bits but less than 128 bits to represent as an integer:

    >>> 2**64+2
    18446744073709551618 # note the '8' at the end
    >>> int(2**64+2)
    18446744073709551618 # same obviously


    If I calculate the same number in C99 128 bit long double, I get 18446744073709551618.000000

    Now, if I use numpy long double:

    >>> a=np.longdouble(2)
    >>> b=np.longdouble(64)
    >>> a**b+a
    18446744073709551618.0 # all good...


    What about these incorrect results:

    >>> np.longdouble(2**64+2)
    18446744073709551616.0 # Note '6'; appears 2**64 not done in long double
    >>> np.longdouble(int(2**64+2))
    18446744073709551616.0 # can't force the use of a Python long
    >>> n=int(2**64+2)
    >>> np.longdouble(n)
    18446744073709551616.0
    >>> np.longdouble(18446744073709551618)
    18446744073709551616.0 # It really does not want to do '8' at the end


    But, this works:

    >>> np.longdouble(2**64)+2
    18446744073709551618.0


    Question: Does numpy have issues converting values correctly into long doubles? Is there something I am doing incorrect?

    Continue reading...

Compartilhe esta Página