By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

what is this?

P: n/a
Hi,
I'm a real beginner with python and I don't really know if I will get it.
But here is one of my first exercises:

Python 2.3+ (#1, Sep 23 2003, 23:07:16)
[GCC 3.3.1 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
a = 3
b = 4
b / 2 + a 5 b / (2.0 + a)

0.80000000000000004

There must be something wrong. I' running the new Suse 9.0.

Herbert
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Herbert Fritsch wrote:
b / (2.0 + a)

0.80000000000000004

There must be something wrong. I' running the new Suse 9.0.


No :-)

That's the way *all* floating point operations work, nothing python-specific
here. See

http://www.python.org/doc/faq/genera...-so-inaccurate

Peter
Jul 18 '05 #2

P: n/a
Herbert Fritsch wrote:
a = 3
b = 4
b / 2 + a
5
b / (2.0 + a)


0.80000000000000004

There must be something wrong. I' running the new Suse 9.0.


Not really. Think of the order of operations in mathematics.
Multiplication and division are ranked higher than addition. Using
parentheses for grouping is higher (before) mulitplication and division.

First example:

4 / 2 + 3 yields 2 + 3 yields 5

Second:

4 / (2.0 + 3)

Python coerces the 3 into a floating point number, so lets write all
these as floating points:

4.0 / (2.0 + 3.0)

Order of operations says do the parentheses first:

4.0 / 5.0 yields 0.8000000000000004

Theres a listing of Pythons order of operations somewhere in the
documentation on python.org (couldn't find it with a quick search).

:)


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.