a *= b not equivalent to a = a*b

mlz mlzarathustra at gmail.com
Fri Aug 26 06:20:40 EDT 2016


It's true that a*(b/c) yields fractions which would probably accrue accuracy errors depending on how those values are implemented. For example, it is possible to represent 1/3 internally as two numbers, numerator and denominator, thus avoiding the repeating decimal (or binimal, or whatever it's called). I believe there are languages that preserve exact accuracy in this way for rational fractions. I don't know if Python is one of them.

On the other hand, (a*b)/c is safer since in this case (for the binomial coefficient) it always yields an integer.

  -= m =-



On Friday, August 26, 2016 at 1:42:31 AM UTC-7, Christian Gollwitzer wrote:
> Am 26.08.16 um 09:53 schrieb Erik:
> > On 26/08/16 08:44, mlz wrote:
> >> Here's the key:
> >>
> >> $ python2
> >> Python 2.7.10 ...
> >>>>> 1/2
> >> 0
> >>>>>
> >>
> >> $ python
> >> Python 3.5.1 ...
> >>>>> 1/2
> >> 0.5
> >>>>> 1//2
> >> 0
> >>>>>
> >>
> >> I read about this awhile ago, but it's not until it bites you that you
> >> remember fully.
> >
> > How is this related to your question? The example explicitly says Python
> > 2 and doesn't use the '//' operator.
> >
> 
> It's related by the fact that a*b/c performs integer division (intended 
> by the OP) which gives a different result than a*(b/c) (unintended by 
> the OP). Floating point (as in Python 3) *also* may give a different 
> result, but the deviation from the "true", i.e. mathematical value, is 
> far less than with integer arithmetics.
> 
> 	Christian




More information about the Python-list mailing list