Thread: Rounding
View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Jerry W. Lewis Jerry W. Lewis is offline
external usenet poster
 
Posts: 837
Default Rounding

Most decimal fractions are non-terminating binary fractions that can only be
approximated (just as 1/3 can only be approximated as a decimal fraction).
Single uses less than half of the bits that Double uses for approximating the
value. As a result, a Single value may differ from your intended value in
the 8th significant figure, whereas a Double value should equal your intended
value to at least 15 significant figures.

As documented, Excel will display no more than 15 significant figures, so
you cannot directly see the that an unavoidable approximation took place.
Since STaxRate is declared Single, VBA will display no more than 7
significant figures of its value, again hiding the approximation until you
explicitly convert it to a Double value by one of CDbl(), assignment to a
declared Double variable, or putting the value in an Excel cell (always
Double).

Now that large amounts of memory are readily available, the main
justification for the continued existence of the Single data type is backward
compatibility with legacy code. As a general rule of thumb, you should not
use single unless either:
- The variable will never contain non-integer values (in which case Long
would support a broader range of integers without using any more memory, and
should calculate faster)
- You sufficiently understand the implications of binary approximation to
floating point numbers to be certan that Single will not cause problems such
as this one.

Jerry

"Jim at Eagle" wrote:

Jim, The only thing different was in the declaration. I used "Single" you
used "Double". Why that makes a difference, I don't know, but it does.
It's mysteries like this that keeps me awake while driving.
--
Jim at Eagle