I'm using Access 2007 on Windows XP, and I just noticed a total on one of my forms was wrong. It came down to - Dim single1 As Single
-
Dim single2 As Single
-
Dim single3 As Single
-
-
single1 = 425000
-
single2 = 352922.2
-
single3 = single1 - single2
Guess what single3 equals. 72077.81
Not good for my dollar amount calculations! I did find that the Double type and Currency type got it right, but I'm pretty sure I'll never use Single again.
Obviously this is due to the inaccuracy of representing decimal numbers in binary, but the range of Single would really suggest that you could do math with reasonable size numbers and expect accurate results.
It's amazing to me that in Single math 1,000,000.1 - 1,000,000 = .125
6 20049
Here's a question: Is DSUM safe for totaling a bunch of large dollar amounts including change?
Hi Chip. Problem is that single precision values have only 7 significant digits of precision - and if you look at the examples you give you are using all of them, which is why the calculations are giving very inexact results.
Rounding issues will always arise in any floating-point number system, but single-precision values have the least number of significant digits, so rounding problems will be a particular issue if the number of significant digits is larger than the type can actually manipulate.
You could either use double-precision to improve this (15 significant digits), or a variant typecast to the decimal type using the CDec() function (which has the most digits of precision of all VB types, 28 decimal places).
I never use single-precision values in any calculations; doubles generally work well for most things, but even so rounding errors need to be taken into account when comparing values - strict equality of floating point numbers is unachievable given the small round-off errors that are inevitable in such number systems.
See the following articles for further technical guidance: http://msdn.microsoft.com/en-us/libr...le(VS.85).aspx http://msdn.microsoft.com/en-us/libr...le(VS.85).aspx http://msdn.microsoft.com/en-us/libr...m.decimal.aspx
-Stewart
Thanks, Stewart. I wasn't aware of the Decimal type. I couldn't find those same specifications with information about the Currency type. Do you know if Currency is safe, or should I just use Decimal for my calculations?
Hi Chip. The Currency type is not only safe, it is exact within its 4-decimal place limits - it is not a floating point type at all, but a whole-number type (the currency value multiplied by 10,000 for storage - hence the 4 decimal place limitation).
The short MSDN article on the type is here (and very similar to the Access help entry): http://msdn.microsoft.com/en-us/library/aa458706.aspx
and a better MS article here: http://support.microsoft.com/kb/51414
-Stewart
All Floating Point is dangerous, single and double. Decimal type is a step in the right direction but the problem is, at least how I see it, is the more numbers to the left of the decimal, the less numbers on the right. I do not like this.
NeoPa 32,556
Expert Mod 16PB
Stewart has made clear that the misuse of the Single data type is dangerous. The correct use of it is fine. The Access Help system includes such warnings and doesn't recommend the use of floating-point number types for storing currency values.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Batista, Facundo |
last post by:
Here I send it.
Suggestions and all kinds of recomendations are more than welcomed.
If it all goes ok, it'll be a PEP when I finish writing/modifying the code.
Thank you.
.. Facundo
|
by: CODA PBC |
last post by:
We have one user who enters a transaction and then does a single row
update (updates all columns but only one is changing - this is due to
the way our sql is generated in the application), at this...
|
by: Johnny Google |
last post by:
Here is an example of the type of data from a file I will have:
Apple,4322,3435,4653,6543,4652
Banana,6934,5423,6753,6531
Carrot,3454,4534,3434,1111,9120,5453
Cheese,4411,5522,6622,6641
The...
|
by: vb |
last post by:
Hi all,
I am a newbie in C and i want to know what all pointer conversions are
"legal" according to ANSI C standard. For Example, int* to char*,
some_struct* to char* and so on ..
According to...
|
by: Jon Rea |
last post by:
I hav been looking for the last 2 hours on how to do this without much luck.
Im going to give a simplifed model of the problem i have.
I want a collection class that can holds a series or...
|
by: zoltan |
last post by:
Hi,
The scenario is like this :
struct ns_rr {
const u_char* rdata;
};
The rdata field contains some fields such as :
|
by: David Mathog |
last post by:
Apologies if this is in the FAQ. I looked, but didn't find it.
In a particular program the input read from a file is supposed to be:
+ 100 200 name1
- 101 201 name2
It is parsed by reading...
|
by: sejal17 |
last post by:
hello
Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
|
by: Hags007 |
last post by:
I have a XML file I am working with. This file has been created by hand and I now need to develop a PHP script that will create it in the same format.
Here is what I have thus far:
$query =...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |