A Wednesday 12 September 2007, dragan savic escrigué: > Hi! > > I have problems using Float32 type. > > I pass the Python list with values [4.1,60.0,4.1,60.0] > to be inserted into the table with following Col > types: > Col1: Float32, Col2: Float32, Col3: Float64, Col4: > Float64 > After the next insertion of new values into the table > I want to first check if the values I am sending in > the list are the same with the ones written in the > table by PyTables. If they are the new entry will not > be added in the table. The output I get with print > statement is following: > > I have already inserted [4.1,60.0,4.1,60.0] > which is displayed by the print command as: > [4.0999999999999996, 60.0, 4.0999999999999996, 60.0] > > The "same" values I read with PyTables and print > is: [4.0999999046325684, 60.0, 4.0999999999999996, > 60.0] > > The first value from both lists don't match and they > should. How can I get around this problem of > accurately comparing two Float32s?
This is because floating points are always represented as Float64 numbers in python, but PyTables does allow Float32 as well. Look at this example in NumPy: In [5]: a = 4.1 In [6]: a Out[6]: 4.0999999999999996 In [7]: b = numpy.float32(a) In [8]: b Out[8]: 4.09999990463 In [9]: a = b In [10]: a = 4.1 In [11]: a == b Out[11]: False Now, you can make a direct comparison by converting first to minimum common type. For example, if you are using NumPy, you can do: In [12]: numpy.float32(a) == b Out[12]: True Cheers, -- >0,0< Francesc Altet http://www.carabos.com/ V V Cárabos Coop. V. Enjoy Data "-" ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Pytables-users mailing list Pytables-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pytables-users