Is Python incorrectly handling this "arbitrary precision integer"?

Jeff Snider

Python is supposed to have "arbitrary precision integers," according to the answer in Python integer ranges. But this result is plainly not arbitrary precision:

$ python -c 'print("%d" % (999999999999999999999999/3))'
333333333333333327740928

According to PEP 237, bignum is arbitrarily large (not just the size of C's long type). And Wikipedia says Python's bignum is arbitrary precision.

So why the incorrect result from the above line of code?

jb.

Actually in python3 whenever you divide ints you get float as a result. There is a // operator that does integer division:

 >>> 999999999999999999999999/3
 3.333333333333333e+23
 >>> 999999999999999999999999//3
 333333333333333333333333

 >>> type(999999999999999999999999/3)
 <class 'float'>
 >>> type(999999999999999999999999//3)
 <class 'int'>

This does give the correct arbitrary precision output:

 python -c 'print("%d" % (999999999999999999999999//3))' 
 333333333333333333333333

How to write code compatible with both python 2.2+ and 3.3

This is actually simple, just add:

 >>> from __future__ import division 

this will enable 3.X division in 2.2+ code.

>>> from sys import version 
>>> version
'2.7.6 (default, Dec 30 2013, 14:37:40) \n[GCC 4.8.2]'
>>> from __future__ import division 
>>> type(999999999999999999999999//3)
<type 'long'>
>>> type(999999999999999999999999/3)
<type 'float'>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LLVM arbitrary precision integer

From Dev

LLVM arbitrary precision integer

From Dev

Uri.MakeRelativeUri incorrectly handling ../

From Dev

w.d format incorrectly handling integers

From Dev

Python version incorrectly identified

From Dev

Python '==' incorrectly returning false

From Dev

Python Converts Float Incorrectly

From Dev

JSON.Net BSON serialization incorrectly handling DateTimeOffset?

From Dev

Swift In-App-Purchase: Constant prompt to sign in; incorrectly handling .finishTransaction?

From Dev

JSON.Net BSON serialization incorrectly handling DateTimeOffset?

From Dev

Python Unit Testing Mock incorrectly

From Dev

Python: Scrapy CSV exports incorrectly?

From Dev

python regex ignoring underscore incorrectly

From Dev

Python scipy fsolve works incorrectly

From Dev

Python Unit Testing Mock incorrectly

From Dev

Python Encode Spaces are incorrectly encoded

From Dev

Registry change through python working incorrectly

From Dev

Python byte string print incorrectly in dictionary

From Dev

Did I organise my Python directory incorrectly?

From Dev

Python recursive function executing return statement incorrectly?

From Dev

Python - Passing variables between a class function incorrectly?

From Dev

Division error in python, result is incorrectly rounded

From Dev

Python seeming to incorrectly handle time zone conversions

From Dev

python input remembering past inputs incorrectly

From Dev

handling float arguments python

From Dev

python request error handling

From Dev

python error code handling

From Dev

Event listening and handling in Python

From Dev

Understanding Exception Handling in Python

Related Related

HotTag

Archive