Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rounding values declared as single 1

Status
Not open for further replies.

JesOakley

Programmer
Jan 21, 2008
42
0
0
GB
Hi folks.
I am looping through a text file, accumulating a certain value on each record into a variable defined as Single. The values are quite low, but to 2 decimal places.
Can anyone explain why when I say sngAmount = sngAmount + csng(strAmount) it manages to add 61.85 to 18.30 and come up with 80.14999?

I have read a wonderful comment in another post that says "if you are dealing with EXACT numbers, don't use single or double" which seems totally ludicrous to me. Surely numbers are, by their very nature, exact.

Is there a simple get-round? Access 2003.
 
The get around, as the post you read said, is not to use single or double. This is due to precision issues they have for the kind of work you are doing.

Try using Currency instead.

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Surely numbers are, by their very nature, exact
Single and Double are floating point numbers and thus only approximations ...
Why not using Variant of subtype Decimal and the CDec function ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Cheers, changed to Currency and all now hunky-dory. However I can't quite get my head around a numeric datatype that struggles with basic addition. Unless I'm doing Single a disservice, and it's the Csng function that's the culprit. Even so, pretty unbelievable that this fundamental (or so it seems to my blinkered view) problem exists.
 
Seems like you didn't make any sense with my previous post ...
 
Sorry PHV, I guess I didn't. As far as I can see, no number is an approximation. Floating-point or otherwise, it is whatever number it is. You can't say that 1.23 is only an approximation of 1.23.
 
As an addendum, PHV, after some thought, I can perhaps understand that with multiplication and division it is possible to get a degree of rounding, but I am talking about the addition of 2 numbers each less than 100 and each with exactly 2 decimal places. To suggest that I should accept 'approximations' simply because I used the Single datatype is madness.
In fact I would rather believe there is a bug with the Csng function when used on formatted text strings. But surely not...
 
Unfortunately there is no bug with CSng().

It is fully because of the way floating point numbers work with VB (and computing in general) that they work as approximations.

Here's a very quick search that turned up a bit more about the actual representation of floating point numbers

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top