Question : SQL dbSingle/dbDouble accuracy problem

I've got a VB6 application reading/writing to an Access database. A record had -0.3 in a 'double' field.

      select DISTINCT [INDEX] from [TABLE] where FieldValue <= -.3

fails to find it. So does :

      select DISTINCT [INDEX] from [TABLE] where FieldValue = -.3

Another record has +0.3 but is found by the query :

      select DISTINCT [INDEX] from [TABLE] where FieldValue<0.3

I *COULD* write the queries with everything using the BETWEEN but that's a messy solution. I'm assuming it's caused by SQL using float failing to match the accuracy of the double. Is there any beter solution, e.g. some casting within the SQL?

BJ

Answer : SQL dbSingle/dbDouble accuracy problem

Indeed access accuracy sux. I had tried to start a disussion a while back on the topic (well it was about rounding, but the idea was the same), but everyone thought I was an "idiot" and tried to explain to me how I should round values.

It's just that access sux in the accuracy department. The really screwed it up. Access is unusable when accuracy counts. You can never be sure you did it right.

I recently found an article from the big boss from FMS Inc and he agrees (the article was from 1997)

OK, now back to a solution. The only way to get any accuracy out of access is to basically first convert the number to a "string"-ish type. You could try the format function, but that has the biggest lot of accuracy bugs.

The only way to get things like this working is to use:
- the Currency type (for values with 4 decimals or less)
- the Decimal type (for all other values)

The currency works fine. Just set the format to standard and you are on your way. The currency basically is an integer value multiplied and divided by 10000 to store the (maximally 4) decimals

The Decimal type is not a type you can declare. Otherwise all my Doubles would be changed to Decimals in an minute. No MS saw it in their infinate wisdom as to not grant us a proper decimal type. The decimal is like the currency but it has much more decimals. You can however use the CDec function to convert the double to decimal. Unfortunately you need to do this every where a double is used.

So you have two choices:
- Convert the Doubles to Currency (if you can fit you data in them)
- Place a CDec() function around all uses of the double

For example your query should read:

select DISTINCT [INDEX] from [TABLE] where CDec(FieldValue) <= -.3

However this will probably slow it down since there is a selection on an expression.
Random Solutions  
 
programming4us programming4us