array precision

Barry Drake bldrake1 at yahoo.com
Sat Feb 9 01:46:42 CET 2002


Hugo,
I tried to duplicate your results.  Here is the output in Idle:

Python 2.1.1 (#20, Jul 26 2001, 11:38:51) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> import array
>>> myArray = array.array('f', [.5, .5, .5, .5])
>>> myArray
array('f', [0.5, 0.5, 0.5, 0.5])
>>> n = 10
>>> for i in range(len(myArray)):
	    myArray[i] += (1.0/n)

>>> myArray
array('f', [0.60000002384185791, 0.60000002384185791,
0.60000002384185791, 0.60000002384185791])
>>> for i in range(len(myArray)):
	    if (myArray[i] > 0.0) and (myArray[i] < 1.0):
		    exit(0)
		
Traceback (most recent call last):
  File "<pyshell#11>", line 3, in ?
    exit(0)
>>> len(myArray)
4
>>> def TestFunc(min, max):
	    for i in range(len(myArray)):
		    if (myArray[i] > min) and (myArray[i] < max):
			    print "I quit!"

			
>>> TestFunc(0.0,1.0)
I quit!
I quit!
I quit!
I quit!
>>> import sys
>>> def TestFunc(min, max):
	    for i in range(len(myArray)):
		    if (myArray[i] > min) and (myArray[i] < max):
			    print "I quit!"
			    sys.exit(0)

			
>>> TestFunc(0.0,1.0)
I quit!
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in ?
    TestFunc(.0,1.)
  File "<pyshell#31>", line 5, in TestFunc
    sys.exit(0)
SystemExit: 0
>>>

As you can see I tried with exit(0) and without.  In both cases the
loop terminates as you wanted.

It might help if we knew what computer system your using and the
version of Python.  Have you tried using the debugger to inspect
memory; printing out intermmediate values, etc.?  The dubugger is
integrated with Idle and it's quite good.

Barry

hugomartires at hotmail.com (Hugo Martires) wrote in message news:<d0380df9.0202080516.7a76905d at posting.google.com>...
> "Emile van Sebille" <emile at fenx.com> wrote in message news:<a3retd$19cgik$1 at ID-11957.news.dfncis.de>...
> > "Hugo Martires" <hmartires at cultalg.pt> wrote in message
> > news:st888.7971$oI4.33917985 at newsserver.ip.pt...
> > > but i don´t need all the numbers, i only need 0.6
> > 
> > See 4.98 in the FAQ at http://python.org/doc/FAQ.html
> 
> I have read it but still confused.
> Se my point:
> --> n=0
> 
> myArray = array.array('f', [0.5, 0.5, 0.5, 0.5]
> 
> myArray[i] = myArray[i] + (1.0/n)
> 
> myArray[0] = 0.600000023 ("something like this")
> 
> for i in range ( len(myArray) ):
> 	if (myArray[i] > 0.0) and (myArray[i]<1.0):
> 		exit(0)
> 
> The problem is that he it never exit, unless i do like this:
> for i in range ( len(myArray) ):
> 	if (myArray[i] > 0.0001) and (myArray[i]<0.9999):
> 		exit(0)
> 
> that is not the better way :(
> 
> ------------------------------------------------------------------
> I have tried with:
>     myArray=array.array('f')
>     myArray=array.array('d')
> and even with a list:  myArray=[]
> 
> I know that the problem is in the internal storage of the numbers, but
> i canot understand how to solve that.
> I just want to make a simple calculation and an if statement.
> 
> Tanks for help



More information about the Python-list mailing list