Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float patch for inf and nan on Windows (and other platforms) #45976

Closed
tiran opened this issue Dec 15, 2007 · 12 comments
Closed

Float patch for inf and nan on Windows (and other platforms) #45976

tiran opened this issue Dec 15, 2007 · 12 comments
Assignees

Comments

@tiran
Copy link
Member

tiran commented Dec 15, 2007

BPO 1635
Nosy @gvanrossum, @tim-one, @tiran
Superseder
  • bpo-1640: Enhancements for mathmodule
  • Files
  • trunk_float_inf_nan.patch
  • trunk_float_inf_nan2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/tiran'
    closed_at = <Date 2007-12-18.23:26:07.917>
    created_at = <Date 2007-12-15.10:23:24.457>
    labels = []
    title = 'Float patch for inf and nan on Windows (and other platforms)'
    updated_at = <Date 2008-01-06.22:29:44.389>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2008-01-06.22:29:44.389>
    actor = 'admin'
    assignee = 'christian.heimes'
    closed = True
    closed_date = <Date 2007-12-18.23:26:07.917>
    closer = 'christian.heimes'
    components = []
    creation = <Date 2007-12-15.10:23:24.457>
    creator = 'christian.heimes'
    dependencies = []
    files = ['8961', '8962']
    hgrepos = []
    issue_num = 1635
    keywords = ['patch']
    message_count = 12.0
    messages = ['58661', '58664', '58697', '58703', '58704', '58759', '58768', '58771', '58776', '58780', '58781', '58785']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'tim.peters', 'Rhamphoryncus', 'christian.heimes']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = '1640'
    type = None
    url = 'https://bugs.python.org/issue1635'
    versions = ['Python 2.6', 'Python 3.0']

    @tiran
    Copy link
    Member Author

    tiran commented Dec 15, 2007

    The patch unifies the creation and representation of "inf", "-inf" and
    "nan" on all platforms.

    >>> float("inf")
    inf
    >>> float("-inf")
    -inf
    >>> float("nan")
    nan
    >>> repr(1e300 * 1e300)
    'inf'
    >>> repr(1e300 * 1e300 * 0)
    'nan'
    >>> repr(1e300 * 1e300 * -1)
    '-inf'

    @tiran tiran self-assigned this Dec 15, 2007
    @tiran
    Copy link
    Member Author

    tiran commented Dec 15, 2007

    Update:

    I've added a platform independent implementation of stricmp and strnicmp
    (case insensitive compare). It could be useful for other parts of the
    code as well.

    @Rhamphoryncus
    Copy link
    Mannequin

    Rhamphoryncus mannequin commented Dec 17, 2007

    You have:
    #define Py_NAN Py_HUGE_VAL * 0
    I think this would be safer as:
    #define Py_NAN (Py_HUGE_VAL * 0)

    For instance, in code that may do "a / Py_NAN".

    Those manual string copies (*cp++ = 'n';) are ugly. Can't you use
    strcpy() instead?

    @tiran
    Copy link
    Member Author

    tiran commented Dec 17, 2007

    Adam Olsen wrote:

    You have:
    #define Py_NAN Py_HUGE_VAL * 0
    I think this would be safer as:
    #define Py_NAN (Py_HUGE_VAL * 0)

    For instance, in code that may do "a / Py_NAN".

    You are right! Fixed

    Those manual string copies (*cp++ = 'n';) are ugly. Can't you use
    strcpy() instead?

    Done

    Christian

    @tiran
    Copy link
    Member Author

    tiran commented Dec 17, 2007

    I'm posting a combined patch for all features at bpo-1640.

    @gvanrossum
    Copy link
    Member

    Mostly looks good. Here are some nits.

    (1) You shouldn't have to add pystrcmp.c to the VC project files since
    on Windows it isn't used, right?

    (2) Will the Windows input routine still accept the *old*
    representations for INF and NAN? IMO that's important (a) so as to be
    able to read old pickles or marshalled data, (b) so as to be able to
    read data files written by C programs.

    (3) Shouldn't you be using Py_HUGE_VAL instead of HUGE_VAL in the chunk
    starting at line 187 in floatobject.c?

    @tim-one
    Copy link
    Member

    tim-one commented Dec 18, 2007

    [Guido]

    ...
    (2) Will the Windows input routine still accept the *old*
    representations for INF and NAN? IMO that's important (a) so as to be
    able to read old pickles or marshalled data, (b) so as to be able to
    read data files written by C programs.

    Ha! You're such an optimist ;-) The remarkable truth is that Windows
    has never been able to read its own representations for INF and NAN:

    '1.#INF'
    >>> float(_)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for float(): 1.#INF
    >>> repr(nan)
    '-1.#IND'
    >>> float(_)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for float(): -1.#IND

    This has nothing to do with Python -- same thing from C, etc.

    @tiran
    Copy link
    Member Author

    tiran commented Dec 18, 2007

    Guido van Rossum wrote:

    (1) You shouldn't have to add pystrcmp.c to the VC project files since
    on Windows it isn't used, right?

    It was the easiest way to test the functions in pystrcmp. Linux doesn't
    have stricmp but it also doesn't require the additional code to mangle
    1.#INF into inf.

    (2) Will the Windows input routine still accept the *old*
    representations for INF and NAN? IMO that's important (a) so as to be
    able to read old pickles or marshalled data, (b) so as to be able to
    read data files written by C programs.

    See Tim's answer.
    Pickles and other C programs aren't an issue. Internally NaNs and INFs
    are represented with a special bit pattern (all bits of the exponent are
    set). As long as users don't use str() or repr() on floats it still
    works. The pickle module uses struct.

    (3) Shouldn't you be using Py_HUGE_VAL instead of HUGE_VAL in the chunk
    starting at line 187 in floatobject.c?

    I missed the spot, thanks.

    Christian

    @tim-one
    Copy link
    Member

    tim-one commented Dec 18, 2007

    Historical note: Guido is probably thinking of "the old" pickle and
    marshal here, which did have problems with inf and NaN on Windows (as in
    they didn't work at all). Michael Hudson changed them to use special
    bit patterns instead, IIRC for Python 2.5.

    @gvanrossum
    Copy link
    Member

    Historical note: Guido is probably thinking of "the old" pickle and
    marshal here, which did have problems with inf and NaN on Windows (as in
    they didn't work at all). Michael Hudson changed them to use special
    bit patterns instead, IIRC for Python 2.5.

    In pickle.py, protocol 0 (still the default in 2.6) uses repr(x) to
    write a float and float(s) to convert that back to input. Maybe you're
    thinking of marshal, which is more sophisticated.

    @gvanrossum
    Copy link
    Member

    I suggest you check this in (with the Py_HUGE_VAL fix) and then see how
    much of bpo-1640 we still need.

    @tiran
    Copy link
    Member Author

    tiran commented Dec 18, 2007

    Applied in r59558 to the trunk

    @tiran tiran closed this as completed Dec 18, 2007
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants