Skip to content

sum float precision issue #126181

@anton3s

Description

@anton3s

Bug report

Bug description:

I am encountering some strange results in Python 3.12.3 when summing up numbers using the sum function.

It seems that there are some precision issues, which I wouldn't expect with these numbers:

>>> 2.5392 + 0.4608 - 3.0
0.0
>>> sum([2.5392,0.4608,-3.0], 0.0)
1.1102230246251565e-16
>>> import numpy
>>> sum([2.5392,0.4608,-3.0], numpy.float64(0.0))
0.0

I tested on the official docker images of Python 3.11 - 3.13 and version 3.13 also seem affected (3.11 works as expected).

$ docker run -it  python:3.11 python -c "print(sum([2.5392,0.4608,-3.0], 0.0))"
0.0
$ docker run -it  python:3.12 python -c "print(sum([2.5392,0.4608,-3.0], 0.0))"
1.1102230246251565e-16
$ docker run -it  python:3.13 python -c "print(sum([2.5392,0.4608,-3.0], 0.0))"
1.1102230246251565e-16

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux, macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions