View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Handle Floating point rounding errors, efficiently

Hi JE,

Interesting and useful. I've just done a quick check of long loops of 4
methods, your Constant, coerce to a Single, Round, and a calculation with
Int().

Constant and the Single methods took virtually same time, Single method very
slightly faster. Both significantly faster than Round or Int.

But your Constant method has the distinct advantage of being much more
flexible, say checking to the nearest 5th decimal (actually with fewer
places the Constant method gets faster).

Re evaluate, yes - I never use square brackets in finished code.

Many thanks,
Peter

"JE McGimpsey" wrote in message
...
Don't know about the fastest, most efficient, but

Const dEPSILON As Double = 1E-10 'set appropriately
If Abs(Cells(21, 1).Value - 1) < dEPSILON Then ...


will work. Using Range references (Cells(21,1)) instead of the evaluate
method ([A21]) will likely speed up your code more than which method of
testing you use.



In article ,
"Peter T" <peter_t@discussions wrote:

The various methods I've tried to eliminate "false" results slow the

process
at least to some extent, inevitably I suppose. My purpose and the point

of
this post is to do exactly what you suggest:

"design your programs to be tolerant of the approximations inherent in
floating point numbers"

But in the fastest most efficient way possible.