Numbers in python

B

brainy_muppet

Basically, I have a code with is almost finished but I've having
difficultly with the last stage of the process. I have a program that
gets assigns different words with a different value via looking them up
in a dictionary:

eg if THE is in the writing, it assigns 0.965

and once the whole passage is read it returns all the numbers in the
format as follows:

['0.965', '1.000', '0.291', '1.000', '0.503']

However, I can't seem to get the program to treat the numbers as
numbers. If I put them in the dictionary as 'THE' = int(0.965) the
program returns 1.0 and if I put 'THE' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right! I
basically need to access each item in the string as a number, because
for my last function I want to multiply them all together by each
other.

I have tried two bits of code for this last bit, but neither are
working (I'm not sure about the first one but the second one should
work I think if I could figure out how to return the values as
numbers):

1st code
value = codons[x] * codons[x+1]
x = (int)
x = 0

print value

x +=2

if (x<r):
new_value = value * codons[x]
value = new_value
x +=1
else:
print new_value

This gives the error message
Traceback (most recent call last):
File "C:\Python24\code2", line 88, in -toplevel-
value = codons[x] * codons[x+1]
NameError: name 'x' is not defined

Code 2 - the most likely code
prod = 1
for item in (codons): prod *= item
prod
print prod

Gives this error message:
Traceback (most recent call last):
File "C:\Python24\code2", line 90, in -toplevel-
for item in (codons): prod *= item
TypeError: can't multiply sequence by non-int


Anyone got any ideas?
Dave.
 
D

Diez B. Roggisch

However, I can't seem to get the program to treat the numbers as
numbers. If I put them in the dictionary as 'THE' = int(0.965) the
program returns 1.0

It certainoly does _not_ return 1.0 - it returns 1. And that is all it can
return for being an integer that has by definition no fractional part.
and if I put 'THE' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right! I
basically need to access each item in the string as a number, because
for my last function I want to multiply them all together by each
other.

It _is_ the right number if you use floats - you just have to take into
account that 10-based fractions can't always be represented properly by
2-based IEEE floating points, resulting in rounding errors. Which is what
you see.

http://wiki.python.org/moin/RepresentationError?highlight=(float)

Either you don't care about the minor differences, the use float. Or you do,
then use the decimal-class introduced in python 2.4

Diez
 
F

Fredrik Lundh

Basically, I have a code with is almost finished but I've having
difficultly with the last stage of the process. I have a program that
gets assigns different words with a different value via looking them up
in a dictionary:

eg if THE is in the writing, it assigns 0.965

and once the whole passage is read it returns all the numbers in the
format as follows:

['0.965', '1.000', '0.291', '1.000', '0.503']

However, I can't seem to get the program to treat the numbers as
numbers. If I put them in the dictionary as 'THE' = int(0.965) the
program returns 1.0 and if I put 'THE' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right!

int(0.965) is 0 and int("0.965") is an error, so I'm not sure what you
did to get 1.0.

but 0.965 and 0.96499999999999997 is indeed the same thing, when
stored as binary floating point numbers:
True

for an explanation, see this page:

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

in your case, explicitly formatting the numbers on the way might be good
enough, e.g.

print "%.3f" % value

for details, see:

http://docs.python.org/lib/typesseq-strings

if you really need support for decimal floating point arithmetics, see:

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

</F>
 
D

Diez B. Roggisch

It certainoly does _not_ return 1.0 - it returns 1. And that is all it can
return for being an integer that has by definition no fractional part.

Duncan was right of course. It returns 0.

Diez
 
B

brainy_muppet

'It certainoly does _not_ return 1.0 - it returns 1. And that is all it
can
return for being an integer that has by definition no fractional part.
'

For goodness sake, it was a typo, I'm so sorry!
 
B

brainy_muppet

'It certainoly does _not_ return 1.0 - it returns 1. And that is all it
can
return for being an integer that has by definition no fractional part.
'

For goodness sake, it was a typo, I'm so sorry!
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
if I put 'THE' = float(0.965) it returns 0.96555555549 or something
similar.

That's for the same reasons as you can't write 1/3rd in decimal
notation. Computers can't write 1/10th in binary notation.

Sybren
 
S

Steve Holden

'It certainoly does _not_ return 1.0 - it returns 1. And that is all it
can
return for being an integer that has by definition no fractional part.
'

For goodness sake, it was a typo, I'm so sorry!
Guess that's what you get for calling yourself "brainy_muppet" then.
There's nothing as satisfying as the sight of a ego with egg on its face :)

regards
Steve
 
C

Cameron Laird

It certainoly does _not_ return 1.0 - it returns 1. And that is all it can
return for being an integer that has by definition no fractional part.


It _is_ the right number if you use floats - you just have to take into
account that 10-based fractions can't always be represented properly by
2-based IEEE floating points, resulting in rounding errors. Which is what
you see.

http://wiki.python.org/moin/RepresentationError?highlight=(float)

Either you don't care about the minor differences, the use float. Or you do,
then use the decimal-class introduced in python 2.4

Diez

I wonder if the original poster might not be best off--or at
least think he is--with

str(0.965)
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top