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

The Single data type is dangerous

1,287 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1.     Dim single1 As Single
  2.     Dim single2 As Single
  3.     Dim single3 As Single
  4.  
  5.     single1 = 425000
  6.     single2 = 352922.2
  7.     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
Feb 26 '09 #1
6 20049
ChipR
1,287 Expert 1GB
Here's a question: Is DSUM safe for totaling a bunch of large dollar amounts including change?
Feb 26 '09 #2
Stewart Ross
2,545 Expert Mod 2GB
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
Feb 26 '09 #3
ChipR
1,287 Expert 1GB
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?
Feb 26 '09 #4
Stewart Ross
2,545 Expert Mod 2GB
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
Feb 26 '09 #5
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.
Aug 22 '11 #6
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.
Aug 23 '11 #7

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

Similar topics

21
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
1
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...
19
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...
10
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...
9
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...
29
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 :
59
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...
3
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...
0
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 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
tracyyun
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...

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.