Using < and > functions

H

Harlan

I have two text boxes on a form. I am trying to set up a conditional
statement like:

If Val(txtActual.value) > val(txtAFTemp) + 0.1 then
do something
Else
do something different
End If

Well, the values in the boxes are 172.228 and 172.128 respectively, but the
ELSE statement is running instead of the IF.... the "do something different"
happens. What am I missing here?

Thanks
Harlan
 
P

Paul

Looks to me like:
172.228 = 172.128 + 0.1

i.e. the two values are equal so your test (is Actual > Temp) returns False
(and the Else statement executes).

Maybe you need

If Val(txtActual.value) >= val(txtAFTemp) + 0.1 then

To test if the Acutal is Greater Than or Equal To the temp value.
 
G

George Nicholson

If I understand the situation, what you are asking to be evaluated is:

If 172.228 > (172.128 + .1) Then...

that evaluates as False (they are equal), so it executes the Else.
The Else will be executed if (a <= to b)
Did you mean to use (a >= b) ?
 
H

Harlan

OOPS.... I messed up again.... it is the IF statement that is running....not
the ELSE statement... that is what doesn't make sense to me.
 
G

George Nicholson

In that case, look for rounding "discrepancies". Any chance your text boxes
are formatted to show only 3 decimals but that the actual values have more
than that (any number format in Access that allows decimals allows at least
4 of them).

If it is something like that, maybe you can decide what your accuracy
tolerance is and rephrase your condition:

If valA - valB > .1005 then...

OR: round/truncuate the values before you do the comparison.

If nothing else, you can put a breakpoint within your IF statement to try
and spot *exactly* what the values are (per the code, not the form) at that
point in time. That will probably explain why it's doing what it's doing.
The remedy will depend on what you find.
 
H

Harlan

George,
I have replaced the line of code with actual numbers instead of the text
boxes
ie:
If 172.228 > 172.128 + 0.1 Then

and it still does the same thing.... I also thought about rounding
problems...but that doesn't seem to work either. I'm as wits end with this
one.
 
D

Dirk Goldgar

Harlan said:
George,
I have replaced the line of code with actual numbers instead of the
text boxes
ie:
If 172.228 > 172.128 + 0.1 Then

and it still does the same thing.... I also thought about rounding
problems...but that doesn't seem to work either. I'm as wits end with
this one.

It's probably due to the imprecision of floating-point representation.
You must bear in mind that binary floating-point data types such as
Single and Double do not represent all decimal values precisely. For
example, consider this simple question entered in the Immediate Window:

?172.228 = 172.128 + 0.1
False

Now suppose we forcibly convert those floating-point values to Currency
data type and try it again:

?CCur(172.228) = CCur(172.128) + 0.1
True

Currency is not a floating-point data type; rather, it's an integer
type that is scaled to give 15 digits to the left of the decimal point
and 4 digits to the right of it. because Currency is a precise data
type, the comparison works the way you'd expect.

In general, it is unwise to compare floating-point values for equality.
Instead, either convert them to a fixed-point format and compare that,
or else subtract one from the other and check whether the difference is
less than some small "epsilon" value. That's the sort of technique
George Nicholson was suggesting in his message. I recommend you follow
his suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top