[Tutor] Floating point exercise 3 from Learn python the hard way

Alan Gauld alan.gauld at btinternet.com
Mon Jun 13 23:27:33 CEST 2011


"amt" <0101amt at gmail.com> wrote in message

> I am honestly confused. I have read the exercise and found
> three lines that could use the floating point(2,3 and line 5).

> I can understand why at line 5 I use floating point. 6,75 is more
> precise than saying 7.

Exactly, no problem with line 5 (except the odd comment about 6.83)

> But I can't understand at line 2 and 3. I mean it makes no 
> difference
> for me. Saying 30 or 30.0 is the same thing.
> As well as saying 97 or 97.0.

Precisely, thats why I asked the question.

>> print "Roosters", 100 - float(25) * 3 % 4
>>
>> This is for line 3 so it is more precised.
>>
>
> In what way do you think this is more precise?
>
> As I said, I am confused when it comes to line 2 and 3.
> But I think having more digits after the " ." makes it more
> precise.

It depends on the definition of precision.
As per a recent thread floating point numbers are
less precise in an absolute sense for integer values
because they are stored as approximations, but the
representation with multiple decimal places is a
greater precision mathematically speaking.

> I don't understand your comment on line 5.
> Why would the answer be 6.83?

> The answer at line 5 is 7 because I have integers
> and I think Python approximates.

It doesn't approximate but it does use integer
arithmetic exactly.

> I have done line 5 with pencil and paper:
> 3 + 2 + 1 - 5 = 1, 1+ 4%2 = 1, 1 - (1/4)= 1, 1 + 6 = 7

Right, that's the integer version.

> If I use floating point the right answer will be 6,75
but I don't know how to get to this answer.

> 3 + 2 + 1 - 5 = 1,
> 1+ 4%2 = 1,
> 1 - (1/4)= 1,  -> 1 - 0.25 = 0.75

0.75 + 6 = 6.75

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list