ints and floatsIn which we consider the types of objects for the first, but definitely not the last, time.
Recall our program to convert from Farenheit to Celsius:
# f2c.py -- converts a temperature in Fahrenheit to its equivalent in Celsius.
# CPSC 128 Example program
# Tim Topper, Winter 2013
#
print "This program converts temperatures from Fahrenheit to Celsius."
print "Enter a temperature in Fahrenheit (e.g. 10) and press Enter."
temp_in_f = input("Temperature in Fahrenheit: ")
temp_in_c = (temp_in_f - 32) * 5 / 9
print temp_in_f, "degrees Fahrenheit =", temp_in_c, "degrees
Celsius."
Most students, used to working with hand calculators, are disappointed to write programs like this one using integers. They feel that the answers are not exact because the "decimals" are being lost.
Python allows us to work with either integers (positive and negative whole numbers without decimals), or floating point values (ones with decimals). Converting our program above to work with floating point numbers is as simple as introducing floating point constants.
# f2c.py -- converts a temperature in Fahrenheit to its equivalent in Celsius.
# CPSC 128 Example program
# Tim Topper, Winter 2013
#
print "This program converts temperatures from Fahrenheit to Celsius."
print "Enter a temperature in Fahrenheit (e.g. 10.0) and press Enter."
temp_in_f = input("Temperature in Fahrenheit: ")
temp_in_c = (temp_in_f - 32.0) * 5.0 / 9.0
print temp_in_f, "degrees Fahrenheit =", temp_in_c, "degrees Celsius."
The necessary changes are shown in red. Try running this version of the program and see how the output changes. Is it what you wanted? If not stay tuned for an update coming soon to a module near you.
The changes all consist of adding decimals to our previous integer constants. The decimals are necessary to let the computer know that these are floating point values, rather than integer ones. The key thing to realize here is that 5 and 5.0 are not the same thing.
When we do math we are used to using 5 and 5.0 interchangeably. We know which meaning is intended from the context in which the value occurs. The computer, bless its silicon heart, can't take advantage of real-world context to. It needs to be told explicitly which interpretation to use, and it stores the values very differently internally.
If you make only the second change in the program above (change 32 to 32.0), and run the program it will probably work with decimal values and produce correct answers. This is because when it is faced with an expression that combines several numeric types, e.g. the expression
temp_in_C = (temp_in_F - 32.0) * 5 / 9
which combines integers and floating point values it does its best to convert as needed. In general Python will convert from a "lower" type to a "higher" one thus preserving accuracy. But subtle problems can arise and these conversions take time to perform, time we are supposed to be conserving and using efficiently! Thus we should avoid mixing ints and floats where possible, and generally it is possible. If you find yourself combining them you should probably rethink your approach to the problem.
If you must combine types do so consciously and force the type conversion
yourself. You can convert an object of any type to an integer using the class
name of integers, int, as a function, e.g. int(4.3).
Similarly you can convert an object to a floating point number using the class
name float as a function, e.g. float(4). Using a
class name in this way invokes the class' constructor, a function that tries to
build an object of the specified type.
One common occasion where the two types are combined is in finding the average of a set of real numbers. Here the sum will be a floating point value, but the count of the number of values is best represented by an integer. When we divide the sum by the count, we are combining a floating point value with an integer.
int or float?In many problems either ints or floats can be used
depending on the type of output required. When it is not clear which choice is
best the following insight and guidelines may be helpful. The insight is that
most numbers arise as the result of either measurements (the length, duration,
weight ... of something), or counts (the number of students, cars, bacteria,
problems, ...). The resulting guidelines are to:
Note that the second guideline is stricter than the first.