int, float and string...
Mike C. Fletcher
mcfletch at rogers.com
Tue Jun 4 12:00:27 EDT 2002
When you cast a float to an int you potentially lose information (the
decimal fraction). The conversion is also by way of truncation (for
historic reasons), so 1.99999 -> 1 (not 2), which likely isn't what you
wanted, (use round( num, 0) instead). Floats (C doubles) are larger
than ints (C longs) at the machine level (8 vs 4 bytes on 32-bit
machines if I recall correctly), but the overhead of Python objects is
huge compared to the size of the actual data, so don't worry about it
unless you're using Numeric Python and slogging around huge arrays of
numbers (where worrying about it might actually save you time).
Store your data in the format appropriate to it (if it's a
precision-based measurement, store floats, if it's a count, store
integers. If you're multiplying an int and a float, store a float
result (which is the natural result)).
You normally only worry about converting numbers when you're going from
one working domain to another (I calculate a weighting value (float)
based on analysis of some data, then want to switch to using that value
as an index into an array of values, so need to convert to an integer so
it can be used as a simple index: my data-storage domain has a different
idea of the data type than my index-calculation domain, so I have to
translate between them). Or where calculations require floating-point
operation (most notably division).
Where possible, use the same data format for all pieces of the same data
(don't store a distance as an integer just because it happens to equal
5.00, use a float consistently throughout your code. Don't store a
count of people as a float, always keep it as an integer, and convert it
only when you want to do calculations on it which require non-integral
operation).
Natural float-to-int conversion (rounded to nearest int):
integer = int(round(value,0))
Check whether float is an integer value:
float % 1.0 == 0.0
HTH,
Mike
Shagshag13 wrote:
> Still wondering questions about pythoner's way...
>
> Why does i have :
>
>
>>>>int('0.0123456789')
>>>
> Traceback (most recent call last):
> File "<pyshell#157>", line 1, in ?
> int('0.0123456789')
> ValueError: invalid literal for int(): 0.0123456789
>
> The correct way to avoid is it :
>
>
>>>>int(float('0.0123456789'))
>>>
> 0
>
> ???
>
> And to check if a number should be use as an int rather than a float, can i use :
>
>
>>>>int(float('0.0123456789')) == float('0.0123456789')
>>>
> 0
>
>>>>int(float('5.00000000000009')) == float('5.00000000000009')
>>>
> 0
>
>>>>int(float('5.0000000000000')) == float('5.0000000000000')
>>>
> 1
>
> or i miss a better way ???
>
> By doing this kind of stuff do i save memory ?
> (having a huge list of int and float, casting them to float only for math operations)
>
> thanks,
>
> s13.
>
>
--
_______________________________________
Mike C. Fletcher
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list