Velocity Reviews > Who told str() to round my int()'s!!!

# Who told str() to round my int()'s!!!

Guest
Posts: n/a

 08-11-2007
After a fair amount of troubleshooting of why my lists were coming
back a handful of digits short, and the last digit rounded off, I
determined the str() function was to blame:

>>> foonum

0.0071299720384678782
>>> str(foonum)

'0.00712997203847'
>>>

Why in the world does str() have any business rounding my numbers, and
how do I get around this?

Marc 'BlackJack' Rintsch
Guest
Posts: n/a

 08-11-2007
On Sat, 11 Aug 2007 16:40:02 +0000, Adam W. wrote:

> After a fair amount of troubleshooting of why my lists were coming
> back a handful of digits short, and the last digit rounded off, I
> determined the str() function was to blame:
>
>>>> foonum

> 0.0071299720384678782
>>>> str(foonum)

> '0.00712997203847'
>>>>

>
> Why in the world does str() have any business rounding my numbers, and
> how do I get around this?

If `str()` would not round you would get very long numbers because of the
inaccuracies of floating point values. I know Python is lying when 0.1
prints as 0.1, but do you really want to see

Use string formatting to tell the number of digits you want to see:

In [16]: '%.56f' % 0.1
Out[16]: '0.10000000000000000555111512312578270211815834045 410156250'

Ciao,
Marc 'BlackJack' Rintsch

Guest
Posts: n/a

 08-11-2007
On Aug 11, 12:53 pm, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> If `str()` would not round you would get very long numbers because of the
> inaccuracies of floating point values. I know Python is lying when 0.1
> prints as 0.1, but do you really want to see

I want str() to convert whatever I give it to a string and do nothing
else. I will worry about long FP values in previous steps. I still
find it very disturbing that str() is doing this AND it is
undocumented in all of my books.

> Use string formatting to tell the number of digits you want to see:
>
> In [16]: '%.56f' % 0.1
> Out[16]: '0.10000000000000000555111512312578270211815834045 410156250'

The book I used to learn Python never introduced string formatting, I
suppose I will have to use another and learn it if that is the only
way...

Roel Schroeven
Guest
Posts: n/a

 08-11-2007
> After a fair amount of troubleshooting of why my lists were coming
> back a handful of digits short, and the last digit rounded off, I
> determined the str() function was to blame:
>
>>>> foonum

> 0.0071299720384678782
>>>> str(foonum)

> '0.00712997203847'
>
> Why in the world does str() have any business rounding my numbers, and
> how do I get around this?

You could use repr() instead of str() (AFAIK that's what the interactive
interpreter used to print your foonum), but in any case you should be
aware of the limits inherent in floating point arithmetic and in
conversions between decimal and binary fractions. See e.g.
http://docs.python.org/tut/node16.html

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven

Marc 'BlackJack' Rintsch
Guest
Posts: n/a

 08-11-2007
On Sat, 11 Aug 2007 17:10:05 +0000, Adam W. wrote:

> On Aug 11, 12:53 pm, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
>> If `str()` would not round you would get very long numbers because of the
>> inaccuracies of floating point values. I know Python is lying when 0.1
>> prints as 0.1, but do you really want to see

>
> I want str() to convert whatever I give it to a string and do nothing
> else.

But those long numbers are very disturbing and nobody wants them as usual
string representation. Most programming languages "lie" at this point to
the user so it would be very unexpected too.

> I will worry about long FP values in previous steps.

What do you mean here? Have you looked at the example above? That is the
closest you get to 0.1 -- there's no way to round the floating point
value, it can only be done by converting to `str()`.

Ciao,
Marc 'BlackJack' Rintsch

Guest
Posts: n/a

 08-11-2007
On Aug 11, 9:40 am, "Adam W." <(E-Mail Removed)> wrote:
> After a fair amount of troubleshooting of why my lists were coming
> back a handful of digits short, and the last digit rounded off, I
> determined the str() function was to blame:
>
> >>> foonum

>
> 0.0071299720384678782
>
> >>> str(foonum)

> '0.00712997203847'
>
> Why in the world does str() have any business rounding my numbers, and
> how do I get around this?

If 15 digit precision is a concern, I would suggest that you us the
decimal class instead of floating points. Floating point problems on
X86 machines are well documented.
http://docs.python.org/lib/module-decimal.html
http://pydoc.org/2.4.1/decimal.html
http://gmpy.sourceforge.net/

Steve Holden
Guest
Posts: n/a

 08-11-2007
Roel Schroeven wrote:
>> After a fair amount of troubleshooting of why my lists were coming
>> back a handful of digits short, and the last digit rounded off, I
>> determined the str() function was to blame:
>>
>>>>> foonum

>> 0.0071299720384678782
>>>>> str(foonum)

>> '0.00712997203847'
>>
>> Why in the world does str() have any business rounding my numbers, and
>> how do I get around this?

>
> You could use repr() instead of str() (AFAIK that's what the interactive
> interpreter used to print your foonum), but in any case you should be
> aware of the limits inherent in floating point arithmetic and in
> conversions between decimal and binary fractions. See e.g.
> http://docs.python.org/tut/node16.html
>
>

I should also point out that the subject line for this thread is
inaccurate, as it wasn't rounding ints at all.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

John Machin
Guest
Posts: n/a

 08-11-2007
On Aug 12, 5:37 am, Zentrader <(E-Mail Removed)> wrote:
> On Aug 11, 9:40 am, "Adam W." <(E-Mail Removed)> wrote:
>
> > After a fair amount of troubleshooting of why my lists were coming
> > back a handful of digits short, and the last digit rounded off, I
> > determined the str() function was to blame:

>
> > >>> foonum

>
> > 0.0071299720384678782

>
> > >>> str(foonum)

> > '0.00712997203847'

>
> > Why in the world does str() have any business rounding my numbers, and
> > how do I get around this?

>
> If 15 digit precision is a concern, I would suggest that you us the
> decimal class instead of floating points. Floating point problems on
> X86 machines are well documented.http://docs.python.org/lib/module-de...urceforge.net/

If you are concerned with precision of *communication*:

The problem is not with floating "points" in general; floating point
arithmetic can be done using a decimal representation for the numbers.
The problem is not with "X86" machines in particular; just about all
modern machines have floating point hardware which implements the IEEE
754 standard for binary flaoting point arithmetic. The problem is with
trying to represent in one base (e.g. 10) a non-integer number that is
expressed in a different base (e.g. 2) -- where the number can be
represented exactly only in one base (e.g. 1./10.) or in neither (e.g.
1./3.)

Bjoern Schliessmann
Guest
Posts: n/a

 08-11-2007

> Why in the world does str() have any business rounding my numbers,

You are at the floating point numbers precision limit. Using str,
numbers are rounded to your machine's float precision in decimal
notation. Since floats are internally represented in binary
notation of constant precision, the decimal precision expressed in
number of places is not constant. Thus, safe rounding must be
applied to get consistent results.

> and how do I get around this?

If you don't want this (and thus introduce random deviations if you
assume greater precision than the number of places str gives you),
use repr. You've been warned.

>>> foonum = .0071299720384678782
>>> foonum

0.0071299720384678782
>>> str(foonum)

'0.00712997203847'
>>> repr(foonum)

'0.0071299720384678782'

Regards,

Björn

--
BOFH excuse #5:

static from plastic slide rules

A.T.Hofkamp
Guest
Posts: n/a

 08-15-2007
On 2007-08-11, Adam W. <(E-Mail Removed)> wrote:
> After a fair amount of troubleshooting of why my lists were coming
> back a handful of digits short, and the last digit rounded off, I
> determined the str() function was to blame:
>
>>>> foonum

> 0.0071299720384678782
>>>> str(foonum)

> '0.00712997203847'
>>>>

>
> Why in the world does str() have any business rounding my numbers, and
> how do I get around this?

You have got a few good reactions already. What is not mentioned yet however is
the difference between str() and repr().

Python has two ways to stringify an object:

str() is intended to deliver a 'human-readable' representation of its argument,
and typically, humans think a few digits of a float is enough.

The repr() on the other hand is intended to deliver a 'machine-reproducible'
string representation of its argument, ie after "y = eval(repr(x))" y == x
should hold.

Note that no rounding occurs with the floating point number, arguments of both
str() and repr() are not changed.

So, depending on your intentions of str(foonum), you should either explicitly
format your floating point number yourself (ie if you want a more precise
human-readable representation of the number), or you should use repr() (if you
intend to use the string representation for reproducing the same object
elsewhere by a machine).

Albert