Unexpected behaviour of math.floor, round and int functions (rounding)

Peter J. Holzer hjp-python at hjp.at
Sun Nov 21 13:23:33 EST 2021


On 2021-11-19 12:43:07 +0100, ast wrote:
> Le 19/11/2021 à 03:51, MRAB a écrit :
> > On 2021-11-19 02:40, 2QdxY4RzWzUUiLuE at potatochowder.com wrote:
> > > On 2021-11-18 at 23:16:32 -0300,
> > > René Silva Valdés <rene.silva.valdes at gmail.com> wrote:
> > > > Hello, I would like to report the following issue:
> > > > 
> > > > Working with floats i noticed that:
> > > > 
> > > > int(23.99999999999999/12) returns 1, and
> > > > int(23.999999999999999/12) returns 2
> > > > 
> > > > This implies that int() function is rounding ...
[...]
> > Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929
> > 64 bit (AMD64)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
> >  >>> 23.99999999999999 == 24
> > False
> >  >>> 23.999999999999999 == 24
> > True
> 
> >>> 0.3 + 0.3 + 0.3 == 0.9
> False

Fascinating. The OP ran into the fact that FP numbers have a limited
number of digits in the mantissa (completely independent of the base).
Someone else mentions 0.3 and everybody takes off on that tangent.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20211121/41a195b3/attachment.sig>


More information about the Python-list mailing list