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!

Testing for ZERO has pitfalls

Status
Not open for further replies.

wildcard

MIS
Mar 9, 2001
67
0
0
NZ
I wrote a routine that accrued all money entries
into a variable called 'mproof'.

When checked if the running total was ZERO
eg DO WHILE .T.
* ... my data entry code
IF mproof = 0
* allow exit
EXIT
ENDIF
ENDDO
the routine decided it was NOT EQUAL to ZERO and looped
around.

I tried the same values with a calculator and the result
was actually ZERO !!! What the blazes?...

So I changed my program to compare the accrued total as
VAL(STR(mproof,10,2)) and then the routine worked
perfectly!!

My question is:-
How can a programmer ensure that a value of ZERO
is really and truly ZERO?

Cheers,
Jim
 
Checking for zero, or any other precise number, is always problem if you compare a variable straight from an equation. The problem is caused by the decimal percision of the math. If you check the value of 'mproof' in the debugger at the line 'IF mproof = 0', you may find that the actual value is 0.0001 or less but not quite zero. The best way to check for a precise number is the change you used. This will trim off any extra decimal places you did not really need to check for anyway.
 
Another way is you make a function that converts any number to string and them you compare internally. Like this

function IsZero( nNumber, nDecimal )

where nNumber is the number you want to test
nDecimal is the number of decimal digits

default nDecimal to the most commum you use to compare so you don't need to pass it most of times.

 
Thanx guys and gals.

I was just trying to flag a potential problem
for other programmers to relieve their future
frustration.

No further replies replied. Thanx again.
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top