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

dn PythonList at DancesWithMice.info
Fri Nov 19 15:38:01 EST 2021


On 20/11/2021 09.17, Chris Angelico wrote:
> On Sat, Nov 20, 2021 at 5:08 AM ast <ast at invalid> 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:
>>>>> Working with floats i noticed that:
>>>>> int(23.99999999999999/12) returns 1, and
>>>>> int(23.999999999999999/12) returns 2


Has the OP (now) realised that the observation is a "feature" not a
"bug"? It is one of the difficulties of representing small numbers or
numerical-components in binary - there are many decimal values which
cannot be accurately-expressed in binary - exactly as noted.

...
>>  >>> 0.3 + 0.3 + 0.3 == 0.9
>> False
> 
> That's because 0.3 is not 3/10. It's not because floats are
> "unreliable" or "inaccurate". It's because the ones you're entering
> are not what you think they are.
> 
> When will people understand this?
> (Probably never. Sigh.)


Am not aware of any institution which teaches the inner-workings of a
CPU/ALU/FPU/GPU in a general programming class, ie "Programming" and
particularly "Coding", have diverged from "Computer Science" - in at
least this respect.

As well as the approximations involved in trying to maintain
decimal-numbers (floats/floating-point numbers), and particularly values
to the 'right' of a decimal-point; we had to study?suffer classes in
"Numerical Analysis" and be able to gauge the declining accuracy and
precision of sundry calculations. A skill disappearing as fast as
slide-rules!?

This 'pool of ignorance' is particularly noticeable in folk who have
come 'up' through the 'CodeCamp'/'BootCamp' approach to training. On the
other hand, if one is not intending to 'get into' a scientific or highly
mathematical branch of computing/Python, eg commercial applications
using (only) Decimal (or int), the average web-app, and similar; why
bother?
-- 
Regards,
=dn


More information about the Python-list mailing list