Re: 1.090516455488E9 / 1000000.000 ???

2006-03-29 Thread Ben Finney
Tim Roberts <[EMAIL PROTECTED]> writes:

> "Math" <[EMAIL PROTECTED]> wrote:
>>And yes,  I really need this accuracy..
>
> Then you need to understand that you don't really HAVE this accuracy.

With the Decimal type, you do.

>>> import decimal
>>> decimal.getcontext().prec = 5
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.14286")
>>> decimal.getcontext().prec = 28
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.1428571428571428571428571429")
>>> decimal.getcontext().prec = 56
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.14285714285714285714285714285714285714285714285714285714")

It also survives arithmetic.

http://docs.python.org/lib/module-decimal.html>

-- 
 \ "The cost of a thing is the amount of what I call life which is |
  `\   required to be exchanged for it, immediately or in the long |
_o__)run."  -- Henry David Thoreau |
Ben Finney

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Tim Roberts
"Math" <[EMAIL PROTECTED]> wrote:
>
>Thanks  this does the job
>And yes,  I really need this accuracy..

Then you need to understand that you don't really HAVE this accuracy.  You
are fooling yourself.  1090.516455488 cannot be represented exactly in
binary, just like 1/3 cannot be represented exactly in decimal.  Further,
the more arithmetic you do, the less precise is your result.

Floating point is a very tricky world of approximation.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Math
Thanks  this does the job
And yes,  I really need this accuracy..

Many Thanks
- Original Message - 
From: "Felipe Almeida Lessa" <[EMAIL PROTECTED]>
To: "Fredrik Lundh" <[EMAIL PROTECTED]>
Cc: 
Sent: Tuesday, March 28, 2006 6:00 PM
Subject: Re: 1.090516455488E9 / 100.000 ???


> Em Ter, 2006-03-28 às 16:59 +0200, Fredrik Lundh escreveu:
>> and consider using
>>
>> http://www.python.org/doc/lib/module-decimal.html
>>
>> instead.
>
> $ python2.4
> Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
> [GCC 4.0.3 2005 (prerelease) (Debian 4.0.2-4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from decimal import Decimal
>>>> a = Decimal("1.090516455488E15")
>>>> a
> Decimal("1.090516455488E+15")
>>>> b = a / 100
>>>> b
> Decimal("1090516455.488")
>>>> str(b)
> '1090516455.488'
>
> *But*,
>
> $ python2.4 -mtimeit -s 'from decimal import Decimal; a =
> Decimal("1.090516455488E15")' 'a/100'
> 1000 loops, best of 3: 222 usec per loop
> $ python2.4 -mtimeit -s 'a=1.090516455488E15' 'a/100' 100 loops,
> best of 3: 0.234 usec per loop
> $ calc 222/0.234
>~948.71794871794871794872
>
> Decimal is almost one thousand times slower. Do you really need this
> accuracy?
>
> HTH,
>
> -- 
> Felipe.
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list 

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Adam DePrince
On Tue, 2006-03-28 at 16:29 +0200, Math wrote:
> Hello,
> 
> I got a simple and probably stupid newby question..
> When I compute: 
> 1.090516455488E9 / 100
> the result is 1090516455.49
> Should be 1090516455.488

>>> repr( 1.090516455488E9/100 )
'1090.516455488'
>>>

Works for me.  Code snippet?  Print does seem to round at 12 digits.  

>>> print  1.090516455488E9/100
1090.51645549
>>>


Cheers - Adam DePrince

> 
> I know something with float and //...
> 
> Anybody?
> How do I get correct number?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Felipe Almeida Lessa
Em Ter, 2006-03-28 às 16:59 +0200, Fredrik Lundh escreveu:
> and consider using
> 
> http://www.python.org/doc/lib/module-decimal.html
> 
> instead.

$ python2.4
Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
[GCC 4.0.3 2005 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> a = Decimal("1.090516455488E15")
>>> a
Decimal("1.090516455488E+15")
>>> b = a / 100
>>> b
Decimal("1090516455.488")
>>> str(b)
'1090516455.488'

*But*,

$ python2.4 -mtimeit -s 'from decimal import Decimal; a =
Decimal("1.090516455488E15")' 'a/100'
1000 loops, best of 3: 222 usec per loop
$ python2.4 -mtimeit -s 'a=1.090516455488E15' 'a/100' 100 loops,
best of 3: 0.234 usec per loop
$ calc 222/0.234
~948.71794871794871794872

Decimal is almost one thousand times slower. Do you really need this
accuracy?

HTH,

-- 
Felipe.

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Nick Craig-Wood
Math <[EMAIL PROTECTED]> wrote:
>  I got a simple and probably stupid newby question..
>  When I compute: 
>  1.090516455488E9 / 100
>  the result is 1090516455.49
>  Should be 1090516455.488
> 
>  I know something with float and //...

http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate

Eg

>>> a=1.090516455488E9 / 100
>>> print a
1090.51645549
>>> print repr(a)
1090.516455488
>>> 

If you want a given number of decimal places you can use a formatter,
eg

>>> print "%.20f" % a
1090.5164554881961354

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Fredrik Lundh
"Math" wrote:

> I got a simple and probably stupid newby question..
> When I compute:
> 1.090516455488E9 / 100
> the result is 1090516455.49
> Should be 1090516455.488

assuming you meant ~1090, it is:

>>> 1.090516455488E9 / 100
1090.516455488

unless you round it off to 12 significant digits, which is
what str() does:

>>> str(1.090516455488E9 / 100)
'1090.51645549'

on the other hand, if you meant E15 instead, you get
another problem:

>>> 1.090516455488E15 / 100
1090516455.487 # oops!

>>> str(1.090516455488E15 / 100)
'1090516455.49'

the reason is of course that floating point are stored as a limited
number of base 2-digits internally.  what you get when you con-
vert that back to decimal depends on the resulting number itself,
not on the number of significant digits you used on the original
expression.

for more on this topic, see

http://docs.python.org/tut/node16.html

and consider using

http://www.python.org/doc/lib/module-decimal.html

instead.

> I know something with float and //...

if you know how floats work, how come you're surprised by
rounding issues ?





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Georg Brandl
Math wrote:
> Hello,
> 
> I got a simple and probably stupid newby question..
> When I compute: 
> 1.090516455488E9 / 100
> the result is 1090516455.49
> Should be 1090516455.488
> 
> I know something with float and //...
> 
> Anybody?
> How do I get correct number?

Python 2.4.2 (#1, Mar 12 2006, 00:14:41)
[GCC 3.4.5 (Gentoo 3.4.5-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.090516455488E15 / 100
1090516455.487
>>> print 1.090516455488E15 / 100
1090516455.49

print is using str() which formats the float number with 12 digits
precision and therefore rounds the result.
repr() however, which is used by the interpreter when printing
out expression results, uses 17 digits precision which is why
you can see how the result is stored in the computer's memory
(1090516455.487).

Georg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread [EMAIL PROTECTED]
>>> 1.090516455488E9 / 100
1090.516455488

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Math
Should be 1.090516455488E15

- Original Message - 
From: "Math" <[EMAIL PROTECTED]>
To: 
Sent: Tuesday, March 28, 2006 4:29 PM
Subject: 1.090516455488E9 / 100.000 ???


> Hello,
> 
> I got a simple and probably stupid newby question..
> When I compute: 
> 1.090516455488E9 / 100
> the result is 1090516455.49
> Should be 1090516455.488
> 
> I know something with float and //...
> 
> Anybody?
> How do I get correct number?
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


1.090516455488E9 / 1000000.000 ???

2006-03-28 Thread Math
Hello,

I got a simple and probably stupid newby question..
When I compute: 
1.090516455488E9 / 100
the result is 1090516455.49
Should be 1090516455.488

I know something with float and //...

Anybody?
How do I get correct number?
-- 
http://mail.python.org/mailman/listinfo/python-list