Wrong math with Python? -


just starting out python, mistake, but...

i'm trying out python. use calculator, , i'm working through tutorials.

i ran weird today. wanted find out 2013*2013, wrote wrong thing , wrote 2013*013, , got this:

>>> 2013*013 22143 

i checked calculator, , 22143 wrong answer! 2013 * 13 supposed 26169.

why python giving me wrong answer? old casio calculator doesn't this...

because of octal arithmetic, 013 integer 11.

>>> 013 11 

with leading zero, 013 interpreted base-8 number , 1*81 + 3*80 = 11.

note: behaviour changed in python 3. here particularly appropriate quote pep 3127

the default octal representation of integers silently confusing people unfamiliar c-like languages. extremely easy inadvertently create integer object wrong value, because '013' means 'decimal 11', not 'decimal 13', python language itself, not meaning humans assign literal.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -