Connecting Tech Pros Worldwide Forums | Help | Site Map

negative value is sql

Member
 
Join Date: Oct 2007
Posts: 103
#1: Aug 28 '08
i have a query like this
select (a+b+c)-(d+e) where some condition
this fetches one record and the values in the table are like this
a=0.000
b=0.720
c=0.000
d=0.620
e=0.100

above query gives me result as -0.000
although the answer shld be 0.000

amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#2: Aug 28 '08

re: negative value is sql


You require ABS function
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 311
#3: Aug 28 '08

re: negative value is sql


What datatypes are a, b, c, d and e?

I just tried this, making a table where a, b, c, d and e are of type float, and I get this result:

2.2351741790771e-08

which is a very very small positive number.

So apparently you have a precision problem. MySql is apparently not storing a number such as 0.72 as 0.72000000000, or there is a precision problem in the mathematics within MySQL.

This is an indication that you need to be careful about defining your data types and interpreting the results.

I just tried this again where I changed the datatypes of a, b, c, d and e to DECIMAL(10,3) and now my answer is the same as yours: -0.000 . It doesn't seem as if the MySQL internals handle storage of the DECIMAL type as I would have expected.
Reply