CSC231 Powers of 2 in Python

From dftwiki3
Jump to: navigation, search

Back to Weekly Schedule


A Python Program that overflows (or not)

# mulby2.py
# D. Thiebaut
# csc231
# demonstrates how python changes type of
# variable as computation progresses.  Check
# how an interpreted language would react in this
# situation (mulby2.cpp)

x = 0x1
print "starting value of x = ", x
curType = type( x )
print "type( x ) = ", type( x )

while ( True ):
    lastx = x
    x = x * 2
    print "x = ", x
    continue

    if type( x )!= type( lastx ):
        print "last x = ", lastx
        print "type( last x ) = ", type( lastx ) 
        print "x = ", x
        print "type( x ) = ", type(x)
        break