Python Forum
differences between py2 and py3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
differences between py2 and py3
#1
this code, using new division in both Python2 and Python3, shows there are still differences.
from __future__ import division,print_function
efs = 9007199254739968 * 1024
print(str(efs/(2**10)),'kB kilo bytes')
print(str(efs/(2**20)),'MB Mega bytes')
print(str(efs/(2**30)),'GB Giga bytes')
print(str(efs/(2**40)),'TB Tera bytes')
print(str(efs/(2**50)),'PB Peta bytes')
print(str(efs/(2**60)),'EB Exa bytes')
FYI, this is the size of a filesystem available in the AWS cloud. i have never been able to fill it up.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
import __future__ import division, print_function
in python 2 without the future import, / does integer divide so 3/2 is 1
with the future import / does a float divide so 3/2 is 1.5
you need to use the // operator: elf//(2**10) for example. this does integer divide in both.

from __future__ import print_function # don't need the division now
efs = 9007199254739968 * 1024
print(str(efs//(2**10)),'kB kilo bytes')
print(str(efs//(2**20)),'MB Mega bytes')
print(str(efs//(2**30)),'GB Giga bytes')
print(str(efs//(2**40)),'TB Tera bytes')
print(str(efs//(2**50)),'PB Peta bytes')
print(str(efs//(2**60)),'EB Exa bytes')
Reply
#3
actually, i wanted to do float, not int, else i'd get 7 for exabytes. i did a little more digging and found that it is the str() function, or whatever it uses, that is different.
Output:
lt1/forums /home/forums 1> py3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> str(7.9999999999990905) '7.9999999999990905' >>> lt1/forums /home/forums 2> py2 Python 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> str(7.9999999999990905) '8.0' >>> lt1/forums /home/forums 3>

still more digging: the float arithmetic is the same. it's the conversion that is different.

it could appear that Python2 has only 38 bits of float precision, but in fact it has 53 (on i386/amd64) just as Python3. i presume the real difference is the configured number of digits in the conversion to decimal (w/o reading the docs, yet). i think i will make a Python version of the float analyzer i once wrote in C. it could be a useful tool if we ever get to use Python on an architecture with more bits of precision than x86/amd64. there are some out there, and gcc can be made to compile double to a larger size on many platforms (you can go up to 64 bits on i386/amd64 and up to 112 bits on S/370).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
There were changes made to the output formatting of floats in Python 2.7 and more changes in Python 3.2.

Here is an online presentation by the author of changes:

https://www.slideshare.net/dickinsm/magical-float-repr
Reply
#5
that's probably the difference i'm seeing.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Windows/Power Shell: Differences from the tutorial... diemildefreude 22 19,261 Oct-10-2016, 03:36 AM
Last Post: diemildefreude

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020