473,378 Members | 1,404 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,378 software developers and data experts.

Queries with large values produce incorrect multiplication results

Hi

I have often found over the year odd maths stuff that queries can return (eg 1*5 = 4.9999999999999999). I have always got around them but have a problem now which has me stumped. To demonstrate this, this query:

SELECT
contractWorkOrderActivity.qty,
contractWorkOrderActivity.rate,
CDbl(CDbl([qty])*CDbl([rate])) AS [Double],
[qty]*[Rate] AS Native,
CSng(CSng([qty])*CSng([rate])) AS [Single]
FROM contractWorkOrderActivity;

Very straightforward.

When I apply this query to a table with these values:
qty 121212100
rate 77.8

I get the following results.

qty..........121212100
rate.........77.8
Double.....9430303305.91003
Native......9430303744
Single......9430303744

The problems are two fold.

1. Why do I get different results from a Single and Double conversion
2. More importantly, why are none of them correct?

The correct answer is 9430301380, and the difference is significant.

Env is Access 2003 on XP Pro.

If anyone could cast any light on this I would be greatly appreciative.

Thanks

Anthony
May 13 '07 #1
7 2370
JConsulting
603 Expert 512MB
Hi

I have often found over the year odd maths stuff that queries can return (eg 1*5 = 4.9999999999999999). I have always got around them but have a problem now which has me stumped. To demonstrate this, this query:

SELECT
contractWorkOrderActivity.qty,
contractWorkOrderActivity.rate,
CDbl(CDbl([qty])*CDbl([rate])) AS [Double],
[qty]*[Rate] AS Native,
CSng(CSng([qty])*CSng([rate])) AS [Single]
FROM contractWorkOrderActivity;

Very straightforward.

When I apply this query to a table with these values:
qty 121212100
rate 77.8

I get the following results.

qty..........121212100
rate.........77.8
Double.....9430303305.91003
Native......9430303744
Single......9430303744

The problems are two fold.

1. Why do I get different results from a Single and Double conversion
2. More importantly, why are none of them correct?

The correct answer is 9430301380, and the difference is significant.

Env is Access 2003 on XP Pro.

If anyone could cast any light on this I would be greatly appreciative.

Thanks

Anthony

Give this a read over.

http://support.microsoft.com/kb/q42980/
May 13 '07 #2
Thanks, that was an interesting read.

I did notice the discrepancy on test data and data with numbers this big is unlikely to occur for multiplication but the discrepancy is significant, in the case I gave it was over $1500. So it is not possible to avoid this? Excel and calc.exe don't have the problem, so is it something to do with the db engine and the data types it can/chooses to use?

I can't give a client and app that generates invoicing totals for them without certainty of accuracy (very small errors and rounding discrepancies are expected of course).

Thanks

Anthony
May 13 '07 #3
ADezii
8,834 Expert 8TB
Hi

I have often found over the year odd maths stuff that queries can return (eg 1*5 = 4.9999999999999999). I have always got around them but have a problem now which has me stumped. To demonstrate this, this query:

SELECT
contractWorkOrderActivity.qty,
contractWorkOrderActivity.rate,
CDbl(CDbl([qty])*CDbl([rate])) AS [Double],
[qty]*[Rate] AS Native,
CSng(CSng([qty])*CSng([rate])) AS [Single]
FROM contractWorkOrderActivity;

Very straightforward.

When I apply this query to a table with these values:
qty 121212100
rate 77.8

I get the following results.

qty..........121212100
rate.........77.8
Double.....9430303305.91003
Native......9430303744
Single......9430303744

The problems are two fold.

1. Why do I get different results from a Single and Double conversion
2. More importantly, why are none of them correct?

The correct answer is 9430301380, and the difference is significant.

Env is Access 2003 on XP Pro.

If anyone could cast any light on this I would be greatly appreciative.

Thanks

Anthony
Do not perform any Explicit Data Conversions - simply set the Data Types of both [qty] and [rate] in the underlying Table to DOUBLE and the correct answer (9430301380) will be generated.
May 13 '07 #4
JConsulting
603 Expert 512MB
Do not perform any Explicit Data Conversions - simply set the Data Types of both [qty] and [rate] in the underlying Table to DOUBLE and the correct answer (9430301380) will be generated.
From the VBA help file:
Decimal data type
A data type that contains decimal numbers scaled by a power of 10. For zero-scaled numbers, that is, numbers with no decimal places, the range is +/-79,228,162,514,264,337,593,543,950,335. For numbers with 28 decimal places the range is +/-7.9228162514264337593543950335. The smallest non-zero number that can be represented as a Decimal is 0.0000000000000000000000000001.

Note that at this time the Decimal data type can only be used within a Variant. You cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal using the CDec function.

Currency data type
A data type with a range of -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Use this data type for calculations involving money and for fixed-point calculations where accuracy is particularly important. The at sign (@) type-declaration character represents Currency in Visual Basic.
May 13 '07 #5
ADezii
8,834 Expert 8TB
From the VBA help file:
Decimal data type
A data type that contains decimal numbers scaled by a power of 10. For zero-scaled numbers, that is, numbers with no decimal places, the range is +/-79,228,162,514,264,337,593,543,950,335. For numbers with 28 decimal places the range is +/-7.9228162514264337593543950335. The smallest non-zero number that can be represented as a Decimal is 0.0000000000000000000000000001.

Note that at this time the Decimal data type can only be used within a Variant. You cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal using the CDec function.

Currency data type
A data type with a range of -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Use this data type for calculations involving money and for fixed-point calculations where accuracy is particularly important. The at sign (@) type-declaration character represents Currency in Visual Basic.
The Decimal Data Type will not generate the correct result.
May 13 '07 #6
Do not perform any Explicit Data Conversions - simply set the Data Types of both [qty] and [rate] in the underlying Table to DOUBLE and the correct answer (9430301380) will be generated.

Thanks

I thought I had done that, but then I tried so many options perhaps I got confused, or perhaps the linked tables .... or something. Seems to work now thanks.

The explicit type conversions were for demonstration purposes only, I don't normally do that.

Thanks for you help
May 14 '07 #7
ADezii
8,834 Expert 8TB
Thanks

I thought I had done that, but then I tried so many options perhaps I got confused, or perhaps the linked tables .... or something. Seems to work now thanks.

The explicit type conversions were for demonstration purposes only, I don't normally do that.

Thanks for you help
You were correct in your initial assumptions, fields like Quantity and Rate should not be stored internally as DOUBLE Precision Numbers, except in this case, of course.
May 14 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Phil Powell | last post by:
http://www.php.net/array_filter I went there at first for my information on filtering mySQL query results using PHP, to no avail. This is more of a Vignette construct (my native environment)...
3
by: Matt O'Donnell | last post by:
Does anyone know how I can 'join' the results of one SQL query to the bottom of another? Eg. I have two queries: 1. SELECT Name, Surname FROM People WHERE Surname = Smith NAME ...
9
by: Eric Lilja | last post by:
Hello, consider the following two functions: /* function foo() */ void foo() { float y = 0.0f; float sum = 0.0f; for(int i = 0; i < num; ++i) {
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
1
by: Astra | last post by:
Hi All Strange request I know, but could somebody give me pointers on how I can put 3 queries into 1 'thing' and then get only the unique entries from this 'thing'. To explain, I'm using...
5
by: cje | last post by:
I am new to access. I can work in it but don't necessarily know the correct way to do things. I am importing large tables for comparison, from 60,000 to 160,000 rows typically. I have a query...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.