Re: 2.6.1 - simple division

2009-03-14 Thread Steve Holden
farsi...@gmail.com wrote:
 Thanks all, that's very helpful, sorry to waste your time with a
 common question. I have tried the decimal module and will definitely
 keep using it if I need to do this kind of calculation again.
 
 I have 1 more question that the floating point article that was linked
 didn't really answer:
 
   x = 0.8
   x
  0.804
   x * 5
  4.0
 
 Shouldn't I be expecting something like 4.2 ?

How different is 4.0?

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/

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


Re: 2.6.1 - simple division

2009-03-10 Thread Tim Chase

farsi...@gmail.com wrote:

4 / 5.0

0.84

0.8 * 5

4.0


Start here:
http://docs.python.org/tutorial/floatingpoint.html


Play with these:

 4/5.0
0.84
 print 4/5.0
0.8
 print repr(4/5.0)
0.84
 str(4/5.0)
'0.8'
 repr(4/5.0)
'0.80004'


And learn more here:

http://www.lahey.com/float.htm

http://en.wikipedia.org/wiki/Floating-point


-tkc


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


Re: 2.6.1 - simple division

2009-03-09 Thread Lie Ryan

Duncan Booth wrote:

farsi...@gmail.com wrote:


Thanks all, that's very helpful, sorry to waste your time with a
common question. I have tried the decimal module and will definitely
keep using it if I need to do this kind of calculation again.


Try to remember though that the decimal module simply replaces one source 
of inaccuracies with another one:



Decimal(1)/Decimal(3)

Decimal('0.')

_ * 3

Decimal('0.')

1./3.

0.1

_ * 3

1.0

Sometimes you want floating point, sometimes you want Decimal. You need to 
understand the advantages and drawbacks of each in order to make an 
informed choice.



I have 1 more question that the floating point article that was linked
didn't really answer:


x = 0.8
x

 0.804

x * 5

 4.0

Shouldn't I be expecting something like 4.2 ?



You should certainly expect that the final result may be a little bit away 
from the 'exact' result but rounding errors can work in your favour just as 
well as they work against.





You should also keep fractions.Fraction (Rational) number in your 
toolbox. Rational is the most accurate, though it still can't represent 
irrational numbers[1] (e.g. pi, e, phi, sqrt(2)) and is generally slower 
than the others.


[1] actually, neither float nor Decimal can represent irrationals.

PS: Actually, I've been thinking about reducing numbers that can't be 
represented as float, Decimals, Rationals by adding such things as surds 
and special constants. Basically getting python to have exact arithmetic 
library, but I think it'll be too large and complex while not many 
people really need exact arithmetic.

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


2.6.1 - simple division

2009-03-09 Thread farsight
 4 / 5.0
0.84
 0.8 * 5
4.0

python 2.6.1 on mac. What the hell is going on here?
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2.6.1 - simple division

2009-03-09 Thread Mohammad Tayseer
 j = .8
 j
0.80004

Python follows the IEEE-754 standard, which doesn't represent the numbers 
exactly. See http://en.wikipedia.org/wiki/IEEE_754
 Mohammad Tayseer
http://spellcoder.com/blogs/tayseer






From: farsi...@gmail.com farsi...@gmail.com
Sent: Sunday, March 8, 2009 4:16:53 PM
Subject: 2.6.1 - simple division

 4 / 5.0
0.84
 0.8 * 5
4.0

python 2.6.1 on mac. What the hell is going on here?
--
http://mail.python.org/mailman/listinfo/python-list



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


Re: 2.6.1 - simple division

2009-03-09 Thread Tim Rowe
2009/3/8  farsi...@gmail.com:
 4 / 5.0
 0.84
 0.8 * 5
 4.0

 python 2.6.1 on mac. What the hell is going on here?

I know this has already been answered in detail, but one thing that
it's easy for those new to floating point issues to miss is that
fractions that can be expressed exactly in decimal can end up as
recurring decimals in binary. 0.8 looks nice and tidy, but in binary
(if I get this right) it's 0.1100110011001100..., recurring ad
infinitum. The computer has to truncate it somewhere.

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


Re: 2.6.1 - simple division

2009-03-08 Thread farsight
On Mar 8, 2:16 pm, farsi...@gmail.com wrote:
  4 / 5.0

 0.84 0.8 * 5

 4.0

 python 2.6.1 on mac. What the hell is going on here?

Pure curiosity prompted me to try the following:
 40 / 5.0
8.0

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


Re: 2.6.1 - simple division

2009-03-08 Thread Gabriel Genellina

En Sun, 08 Mar 2009 12:22:50 -0200, farsi...@gmail.com escribió:


On Mar 8, 2:16 pm, farsi...@gmail.com wrote:

 4 / 5.0

0.84
 0.8 * 5

4.0

python 2.6.1 on mac. What the hell is going on here?


Pure curiosity prompted me to try the following:

40 / 5.0

8.0

Strange...


See http://docs.python.org/tutorial/floatingpoint.html


--
Gabriel Genellina

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


Re: 2.6.1 - simple division

2009-03-08 Thread Lie Ryan

farsi...@gmail.com wrote:

On Mar 8, 2:16 pm, farsi...@gmail.com wrote:

4 / 5.0

0.84


This one is a common FAQ. Basically floating point is never to be 
trusted. This issue is quite language agnostic, however some language 
decided to hide the issue, python does not. For more information on 
floating point and its intricacies: 
http://docs.python.org/tutorial/floatingpoint.html


 0.8 * 5

4.0

python 2.6.1 on mac. What the hell is going on here?


Pure curiosity prompted me to try the following:

40 / 5.0

8.0

Strange...


Strange, I don't see anything strange with that...

Perhaps you meant, python returns 4.0 instead of 4? It's because in 
division with at least one of the divisor or the dividend a floating 
point will return a floating point value.


Perhaps you're confusing it with integer division, in which both divisor 
and dividend are integers. In python 2.6, this will still return 
integers, but this will change (edit: have changed) in python 3.x, 
division of integer by integer will always result in floating point even 
if the result can be represented exactly by an integer. You can do 'from 
__future__ import division' to use the new division semantic in python 2.x

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


Re: 2.6.1 - simple division

2009-03-08 Thread koranthala
On Mar 8, 7:22 pm, farsi...@gmail.com wrote:
 On Mar 8, 2:16 pm, farsi...@gmail.com wrote:

   4 / 5.0

  0.84 0.8 * 5

  4.0

  python 2.6.1 on mac. What the hell is going on here?

 Pure curiosity prompted me to try the following: 40 / 5.0

 8.0

 Strange...

Please see http://docs.python.org/library/decimal.html for explanation
of why this happens.
To get the proper values, try
 Decimal(4)/Decimal(5.0)
Decimal(0.8)
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2.6.1 - simple division

2009-03-08 Thread farsight
Thanks all, that's very helpful, sorry to waste your time with a
common question. I have tried the decimal module and will definitely
keep using it if I need to do this kind of calculation again.

I have 1 more question that the floating point article that was linked
didn't really answer:

  x = 0.8
  x
 0.804
  x * 5
 4.0

Shouldn't I be expecting something like 4.2 ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2.6.1 - simple division

2009-03-08 Thread Duncan Booth
farsi...@gmail.com wrote:

 Thanks all, that's very helpful, sorry to waste your time with a
 common question. I have tried the decimal module and will definitely
 keep using it if I need to do this kind of calculation again.

Try to remember though that the decimal module simply replaces one source 
of inaccuracies with another one:

 Decimal(1)/Decimal(3)
Decimal('0.')
 _ * 3
Decimal('0.')
 1./3.
0.1
 _ * 3
1.0

Sometimes you want floating point, sometimes you want Decimal. You need to 
understand the advantages and drawbacks of each in order to make an 
informed choice.

 
 I have 1 more question that the floating point article that was linked
 didn't really answer:
 
  x = 0.8
  x
  0.804
  x * 5
  4.0
 
 Shouldn't I be expecting something like 4.2 ?
 

You should certainly expect that the final result may be a little bit away 
from the 'exact' result but rounding errors can work in your favour just as 
well as they work against.


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


Re: 2.6.1 - simple division

2009-03-08 Thread farsight
Thanks duncan, thats very helpful. I'll be more careful with floating
point numbers in future.
--
http://mail.python.org/mailman/listinfo/python-list