Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 05:15 AM
Member
 
Join Date: Oct 2007
Posts: 99
Default negative value is sql

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
Reply
  #2  
Old August 28th, 2008, 02:00 PM
amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: India
Age: 24
Posts: 2,016
Default

You require ABS function
Reply
  #3  
Old August 28th, 2008, 02:07 PM
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 305
Default

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
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles