473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Python, mysql, floating point values question

I had a question about python and mysql, but I want to first thank
those who taught me about the factory method, it was exactly what I
needed.

Anyway, I have a problem where I am pulling floating point values out
of mysql and into python using the MYSQLdb module.

However, the values seem to be altered a little when I store them in python..

Because the decimal places are off, I am unable to use the values that
I have in order to write a sql statement (again using the MYSQLdb)
that pulls column values from just that single value i'm looking for.

Let me explain more clearly:

Let's say I pull column1 values for all records, and they happen to be:

1.4, 1.5632, 1.32523 in the mysql database.

Now I realize that I am interested in all the column values for just
the 1.5632 record. Unfortunately, python seems to be taking the
floating point values, and doing something with it, such that my
variable has something like 1.5632000000001 stored in it or such.

I thus, can't pull the value out using a sql statement such as
"Select * from table_name where column1 = my_variable_value" becuase
my_variable_value has 1.563200000000001 stored in it, and not 1.5632.

The way that the variable gets adjusted seems to be random.

Is there a solution to this?

Thank you very much!
Jul 19 '05 #1
2 3285


Christopher Kang wrote:
I had a question about python and mysql, but I want to first thank
those who taught me about the factory method, it was exactly what I
needed.

Anyway, I have a problem where I am pulling floating point values out
of mysql and into python using the MYSQLdb module.

However, the values seem to be altered a little when I store them in python.

Because the decimal places are off, I am unable to use the values that
I have in order to write a sql statement (again using the MYSQLdb)
that pulls column values from just that single value i'm looking for.

Let me explain more clearly:

Let's say I pull column1 values for all records, and they happen to be:

1.4, 1.5632, 1.32523 in the mysql database.

Now I realize that I am interested in all the column values for just
the 1.5632 record. Unfortunately, python seems to be taking the
floating point values, and doing something with it, such that my
variable has something like 1.5632000000001 stored in it or such.

I thus, can't pull the value out using a sql statement such as
"Select * from table_name where column1 = my_variable_value" becuase
my_variable_value has 1.563200000000001 stored in it, and not 1.5632.

The way that the variable gets adjusted seems to be random.

Is there a solution to this?
The smart-ass answer is to learn how to use floating point properly.

But along those lines, once you get a useful representation of
1.5632 in Python, you may discover that not all the 1.5632's in
the database are identical. They, too, may differ insignificantly.

You may have to apply fixes on both ends. I don't know mysql but
in MS-Acces, I would probably make a calculated field in the db query
such as Format$([TheField],"0.0000"). As a string, the differences
in the many flavors of 1.5632 are discarded.

Thank you very much!


Jul 19 '05 #2
Hi Christopher,
if you have to make calculations or comparing operations, the only
"safe" method is to save and use only integer values.
Of course there must be a preventive agreement on the precision you
want to have (2, 3 4 ... decimals).
The sw part is straigthforward:
- to save in the database you must multiply every float by
10**(precision)
- to use the "integers" and, for example, display them as float, you
must of course make the opposit operation (dividing).

There are drawbacks:
- the queries (from the point of view of the user) are much more
complicate
- the integer takes generally more place than the corresponding float
- the integer divide needs particular attention.

If the disk space occupied by the database is a concern, you can
evaluate the possibility to save the floats as string representation
as, for example, "1.5632"
(coming out from "%.4f" % 1.5631999999999999).

Sorry if the above considerations are not at the same level of those of
the experts, but I definitely am not an "expert". I encountered the
same problems many years ago (when the size of disks was at maximum 200
Mb).
Bye.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
14
by: Nils Grimsmo | last post by:
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>>
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.