How to truncate/round-off decimal numbers?

per9000 per9000 at gmail.com
Wed Jun 21 07:20:15 EDT 2006


Nick Maclaren wrote:
> |> just a thought: if you *always* work with "floats" with two decimals,
> |> you are in fact working with integers, but you represent them as a
> |> floats - confusing for the internal representation.
>
> No, you aren't - you are working with fixed-point

Nick, your answer has so many layers, I'll try to explain how I think
:-D

1) if you use integers you can think of them as having one part bigger
than 100 and one part smaller than 100, like so:
>>> a = 11122
>>> (a/100,a%100)
(111, 22)
Here the output 111,22 looks like something else than an integer, but
this is just a matter of representation. a *is* an integer, but we
represent it *as if it was* a "decimal" number. (Compare with
(minutes,seconds) or (euro,cents) or (feet,inch) or any other
"arbitrary" position system)

2) If we use floats with two decimals
>>> b = 222.33
>>> b
222.33000000000001
they look like fix-point numbers (I had to look it up
http://en.wikipedia.org/wiki/Fixed-point :-D) but python stores it
(correct me if I am wrong) as a float (or double or quad or whatever).
If we want to work with fix-point aritmetics we have to invent new
functions to do most math.

3) Most "decimal numbers" cannot be stored exactly as floats - that is
why b gave the ugly print. But some can, f.x
>>> quart = 0.25
>>> quart
0.25
quart translates to a finite "decimal" number in binary (0.01 I think).

The point is: representing should-be integers as floats makes you loose
precision (negligable probalby but still...).

4)
> |> So why not work with int(float * 100) instead? This way you only have
> |> to take care of roundoffs etc when dividing.
>
> And multiplying, and calling most mathematical functions.
You are correct of course. My mistake.

But, the multiplication is exact before you start rounding off - I wont
start counting ordos for int*int vs. float*float, but it could have
some advantages
>>> a
11122
>>> b
22233
>>> a*b
247275426
>>> (a*b/10000,a*b%10000)
(24727, 5426)
On the other hand you will quickly loose accuracy if you perform
multiple multiplications or divisions or use other mathematical
functions.

5) So, when could this way of thinking be useful? Well, rarely, but if
you only add/subtract "decimals" and/or multiply "decimals" with whole
numbers or if you want to use some non-metric system to do scientific
stuff (compute square feet when having (feet1,inch1) * (feet2,inch2)
assuming that inches are atomic.)

This could of course be extended to
(feet, inch, quarter_of_afoot, sixteeth_of_a_foot) if you'd like - it
is all a matter of representation.

Regards,
Per
----
"It is a gift. A gift to the foes of 'the Terrorists'. Why not
use this 'terrorism'? Long has my father, 'George Bush Sr',
kept the forces of 'the terrorists' at bay. By the blood of
our people are your lands kept safe. Give 'the land of the
brave' the weapon of the enemy. Let us use it against him."




More information about the Python-list mailing list