Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

22.800000 and a 1 on the end!!??

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


notnorwegian at yahoo

May 25, 2008, 12:58 PM

Post #1 of 5 (39 views)
Permalink
22.800000 and a 1 on the end!!??

>>> x = 5
>>> x /= 2
>>> x
2
>>> x *=11.4
>>> x
22.800000000000001

ok where does the 1 in the come from?
--
http://mail.python.org/mailman/listinfo/python-list


fuzzyman at gmail

May 25, 2008, 1:03 PM

Post #2 of 5 (39 views)
Permalink
Re: 22.800000 and a 1 on the end!!?? [In reply to]

On May 25, 8:58 pm, notnorweg...@yahoo.se wrote:
> >>> x = 5
> >>> x /= 2
> >>> x
> 2
> >>> x *=11.4
> >>> x
>
> 22.800000000000001
>
> ok where does the 1 in the come from?

Floating point arithmetic.

Michael Foord
http://www.ironpythoninaction.com/
--
http://mail.python.org/mailman/listinfo/python-list


dfnsonfsduifb at gmx

May 25, 2008, 1:04 PM

Post #3 of 5 (39 views)
Permalink
Re: 22.800000 and a 1 on the end!!?? [In reply to]

notnorwegian[at]yahoo.se schrieb:

> ok where does the 1 in the come from?

Conversion to floating point values, which have no precise
representation for all numbers that have such in the decimal system.
Read more on http://en.wikipedia.org/wiki/IEEE_754-1985

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47fa8447$0$11545$9b622d9e[at]news.freenet.de>
--
http://mail.python.org/mailman/listinfo/python-list


martin at v

May 25, 2008, 1:15 PM

Post #4 of 5 (38 views)
Permalink
Re: 22.800000 and a 1 on the end!!?? [In reply to]

> ok where does the 1 in the come from?

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

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


qwertymaniac at gmail

May 25, 2008, 1:26 PM

Post #5 of 5 (38 views)
Permalink
Re: 22.800000 and a 1 on the end!!?? [In reply to]

Like what others said, use the Decimal class to avoid this.

from decimal import Decimal
x=Decimal(5)
x/=2
x*=Decimal("11.4")
print x
# 28.50

On Mon, May 26, 2008 at 1:28 AM, <notnorwegian[at]yahoo.se> wrote:

> >>> x = 5
> >>> x /= 2
> >>> x
> 2
> >>> x *=11.4
> >>> x
> 22.800000000000001
>
> ok where does the 1 in the come from?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Harsh J
www.harshj.com

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.