rounding problem

A

Andy Leszczynski

It is on Windows, Linux, Python 2.3:

[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Is it normal?

Andy
 
A

Aahz

It is on Windows, Linux, Python 2.3:

[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Is it normal?

http://www.python.org/doc/current/tut/node16.html
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
 
K

Kristian M Zoerhoff

Is it normal?

Yes, for floating-point numbers. This is due to inherent imprecision
in how floats are represented in hardware. If you can live with being
a touch off that many decimal places out, you can just clean up upon
printing with something like:
1.1

for your value of a above. If you don't want to deal with this cruft,
you may want to give the Decimal class a gander:
1.1
 
M

Michael Hartl

Is it normal?

Yes. The interpreter prints back the repr of a, which reflects the
imprecision inherent in floats. If you want '1.1', use the string
returned by the str function.
'1.1'

Michael
 
T

tom

It is on Windows, Linux, Python 2.3:

[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2 Type "help",
"copyright", "credits" or "license" for more information.Is it normal?

Yes. Quite normal.

Floating point numbers are by nature approximations. When you enter your
variable "a" at the prompt, you are asking for a very raw (i.e totally
unformatted!) dump of what your variable "a" contains. It is the
most precise representation of the float 1.1 that your computer can store.
It is probably the best precision your hardware math processor subsystem
has to offer. And, incidentally, that ain't too shabby since a 1 in the
16th decimal place ain't worth spit!

Try putting a print statement in front of your variable.
The print statement ,by itself, adds the most minimial formatting, which
amounts to dropping the last significant digit your math processor holds.
That last digit will *always* contain some arithmetic slop.

If you do any serious work with floats, you will want to learn about
significant figures and the use of the very essential round() function in
whatever language you are using. If you do, you will find that this
situation is really a math thing and has little to do with computers or
any particular language. It's the exact same problem one runs into when
using a slide rule.

If anyone remembers those things :)
Thomas Bartkus
 
D

Dan Bishop

tom said:
It is on Windows, Linux, Python 2.3:

[GCC 3.3.2 (Mandrake Linux 10.0 3.3.2-6mdk)] on linux2 Type "help",
"copyright", "credits" or "license" for more information.
1.1000000000000001 ....
Is it normal?

Yes. Quite normal. ....
The print statement ,by itself, adds the most minimial formatting, which
amounts to dropping the last significant digit your math processor holds.
That last digit will *always* contain some arithmetic slop.

Your statement is misleading, because it suggests that your processor
stores digits. It doesn't; it stores *bits*.

The distinction is important because 1.1 does not have a finite
representation in binary. Instead, it's the repeating bit sequence 1.0
0011 0011 0011...
This is analogous to 1/3 = 0.333... in base ten.

IEEE 754 requires that all normalized numbers be rounded to 53
significant bits, so "1.1" is actually stored as 0x1.199999999999A,
which is equivalent to the decimal number
1.100000000000000088817841970012523233890533447265625. Python's repr
function rounds this to 17 significant digits, or "1.1000000000000001".
 
T

Thomas Bartkus

Dan Bishop said:
tom wrote:

Your statement is misleading, because it suggests that your processor
stores digits. It doesn't; it stores *bits*.

Your explanation is much clearer and more than I knew. And I am sorry you
found my statement a bit misleading. But I never implied details about how
things were stored internally.

No matter what computer, calculater, slide rule or programming language - a
floating point number will always exhibit "arithmetic slop" at the last
significant digit. This is a property inherent to floating point numbers and
has nothing to do with how it is stored on any machine.

Thomas Bartkus
 
N

Nick Coghlan

Dan said:
Your statement is misleading, because it suggests that your processor
stores digits. It doesn't; it stores *bits*.

And where does the word 'bit' come from, hmm? It couldn't possibly be an
abbreviation of Binary digIT, could it?

Cheers,
Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top