[Tutor] Re: Temperature conversion (number types)

Charlie Clark charlie@begeistert.org
Tue Dec 3 05:21:01 2002


On 2002-12-03 at 02:30:02 [+0100], tutor@python.org wrote:
> Message: 13
> Date: Sun, 01 Dec 2002 17:53:56 -0600
> From: Chuck Baker <ChuckBaker@pokynet.com>
> Organization: C & C Enterprises
> To: tutor@python.org
> Subject: [Tutor] Temerature conversion??
> 
> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html>
> -----BEGIN PGP SIGNED MESSAGE-----
> <br>Hash: SHA1
> <p>Hello everyone.
> <p>I am new to the list and have just started playing with Python. I 
> <br>have programed in BASIC before.
> <p>I was going through and playing with it and I started out making a 
> <br>temperature conversion program.
> <p>Converting fahrenheit to celsius I had no problem <br>(98.6-32)/9*5 =37
> <p>That worked fine. But when I tried to reverse it, <br>37/5*9+32
> <br>It came out to = 95 not 98.6
> <p>and I can't figure out why??
Dear Chuck,

you're being bitten by a limitation in the way binary computers deal with 
numbers. Numbers with values right of the decimal point are called 
"floating points" and usually need to be specified as such so that the 
machinery which deals with the inexact and unreliable mapping of decimal to 
binary can be called into action. So how does this fit in your problem?

When you use a number Python attempts to identify its type so that it knows 
what to do with it. You can test this by using the built-in function type():

>>> type(1)
<type 'int'>
>>> type(1.5)
<type 'float'>

Floats take precedence over integers so that when you add them you get a 
float back:

<type 'float'>
>>> 1 + 1.5
2.5

This is why your conversion from Fahrenheit to Celsius works because you 
start with a float.

Integers don't have decimal values. Everyone knows that. What not everyone 
knows is that most programming languages expect integers to stay like that:
>>> 1 / 2
0
>>>  
Yes, divide 1 by 2 and you get 0! Everybody knows that, too!? Well, 
old-hand computer programmers at least. It's kind of logical: an operation 
involving two integers results in an integer. Divide by an equivalent float 
and you're sorted:
>>> 1 / 2.0
0.5 

However, as Tim-bot and others can explain much better than I: 2.0 is not 
2, at least not to the computer and this is why most computer languages 
don't give floats unless you explicitly request them. This shouldn't be a 
problem for languages like Python which allow object types to be changed.

So we have a problem: for compatability with other languages and lots of 
lots of code 1 / 2 gives 0 (makes sense to lots of programmers who've 
learned it) in Python whereas 1 / 2.0 gives 0.5 (makes sense to 
non-programmers).

The solution in Python is to distinguish between "natural" and "floored 
division". Future versions of Python will normally use "natural" division 
as standard but will allow "floored" if desired. 

1 / 2 = 0.5  # natural division
1 //22 = 0   # floored division

Make sense?

Charlie