Problem w/ modulus

R

R Fourt

I'm trying to determine if a the result of a calcuation is either a whole
number or a "half". In other words 1, 2, 24 are okay as are 1.5, 3.5, 7.5.

I was trying to use the modulus operator as such "Hours = iHours mod .5"
then if "int(iHours) = iHours" all is okay...however when I enter ".5" in my
code, Access puts a leading zero in front of it and then errors out with
"division by zero".

I've modified the code to evaluate the numbers as strings and it works, but
I was wondering...is this a quirk (read bug) of Access 2003?
 
R

R Fourt

actually what I ended up doing is evaluating Hours mod 1 = 0 or 5 and got it
working fine but I still wonder about the previous issue. By using .5 I could
have used one If statement instead of two.
 
R

Rick

Can you use this:

- subtract the integer portion of your result from the result itself:
i.e. y = x - int(x)

-if y=.5 or 0, your x was a whole number or a "half". Otherwise, not.
Therefore, you can test:

IsValidResult = (y=0 or y=.5)
 
K

Klatuu

Mod rounds floating point numbers to integers. If you experiment a bit, you
will find that anything .5 or less raises that error.

In any case, here is a work around that will resolve your problem:

Hours = ((iHours * 10) mod 5) / 10
 
R

R Fourt

Here's what I ended up using

If Not Int(timHours / 0.5) = (timHours / 0.5) Then
etc, etc

Thanks for the input, ya'll!
 
W

Wolfgang Kais

Hello "R Fourt".

R Fourt said:
I'm trying to determine if a the result of a calcuation is
either a whole number or a "half". In other words 1, 2, 24
are okay as are 1.5, 3.5, 7.5.

I was trying to use the modulus operator as such
"Hours = iHours mod .5" then if "int(iHours) = iHours" all
is okay...however when I enter ".5" in my code, Access puts
a leading zero in front of it and then errors out with
"division by zero".

I've modified the code to evaluate the numbers as strings and
it works, but I was wondering...is this a quirk (read bug) of
Access 2003?

The mod operator usually is used with two integer operands. Maybe
there is a way to us it here, if the calculation is based on
integers, but you would have to tell us what "the calculation" is.
Otherwise check if iHours*2 (what stands the i for?) is an integer:

If iHours*2 = Int(iHours*2) Then...
 
W

Wolfgang Kais

Hello "R Fourt".

Deviding is the same as multiplying with the reciprocal value, so
" / 0.5" is the same as " * 2".
 
T

Tim Ferguson

I'm trying to determine if a the result of a calcuation is either a
whole number or a "half". In other words 1, 2, 24 are okay as are 1.5,
3.5, 7.5.

I was trying to use the modulus operator as such "Hours = iHours mod
.5" then if "int(iHours) = iHours" all is okay...however when I enter
".5" in my code, Access puts a leading zero in front of it and then
errors out with "division by zero".

Interesting. I can confirm that Mod and \ both fail with a divide-by-zero
error in Access 2002 SP3, when the second parameter (divisor) is 0.5 or
less. It does not mention any such restriction in the help files, so I
guess it might be an (ahem) "unexpected feature".

To get what you want, if the number in question is a double, you can can
do something like

Public Function IsAHalf(someNum As Double) As Boolean
Const tolerance As Double = 0.0001
Dim temp As Double
temp = someNum * 2# ' probably saves time to do it once
IsAHalf = (Abs(temp - Fix(temp)) < tolerance)
End Function


Hope that helps


Tim F
 
J

John Nurick

Interesting. I can confirm that Mod and \ both fail with a divide-by-zero
error in Access 2002 SP3, when the second parameter (divisor) is 0.5 or
less. It does not mention any such restriction in the help files, so I
guess it might be an (ahem) "unexpected feature".

Up to a point, Lord Copper. Both help topics mention that the operands
are rounded to integer types before the division operation is performed.
Since values smaller than 0.5 are known to round to zero <g> this
doesn't seem unexpected to me.

FWIW "mod" seems to be an integer operation in many if not most other
languages (cf. Pascal, C, Perl...)
 
T

Tim Ferguson

Up to a point, Lord Copper. Both help topics mention that the operands
are rounded to integer types before the division operation is performed.
Since values smaller than 0.5 are known to round to zero <g> this
doesn't seem unexpected to me.

Oops: I was about to complain that it didn't say so in my version --
because that is what I looked for initially -- but it is there and you are
quite right.
FWIW "mod" seems to be an integer operation in many if not most other
languages (cf. Pascal, C, Perl...)

Well, yes, but what has VBA to do with most other languages..? <g>

All the best


Tim F
 

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