Why is VBA returning an "Overflow" error? (1 Viewer)

MyTech

Access VBA
Local time
Today, 13:24
Joined
Jun 10, 2010
Messages
108
These are returning an "Overflow" error.

Num1 = 54635133914# MOD 181440
Num1 = CDbl(54635133914) MOD CDbl(181440)


Is there a certain limit for the MOD operator?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:24
Joined
Jan 20, 2009
Messages
12,852
MOD and Integer Divide (\) operands are implicitly converted to Long datatype so are limited to +/- 2^31.
 

vbaInet

AWF VIP
Local time
Today, 18:24
Joined
Jan 22, 2010
Messages
26,374
So your equivalent would be:
Code:
Abs(5463513391) - (181440 * Int(Abs(5463513391)/181440))
 

MyTech

Access VBA
Local time
Today, 13:24
Joined
Jun 10, 2010
Messages
108
Inet, can you please educate me..

Why:
Abs(5463513391) - (181440 * Int(Abs(5463513391)/181440))

Rather then:
5463513391 - (181440 * Int(5463513391/181440))
?

Why is it better to use here the ABS function?
 

vbaInet

AWF VIP
Local time
Today, 18:24
Joined
Jan 22, 2010
Messages
26,374
I like using examples. Try this out:
Code:
([COLOR=Red]-[/COLOR]5463513391) - (181440 * Int(([COLOR=Red]-[/COLOR]5463513391)/181440))
and compare it to your other result. Can you see what Abs() does now?
 

the_net_2.0

Banned
Local time
Today, 12:24
Joined
Sep 6, 2010
Messages
812
Interesting, Microsoft documented in the MSDN library that the MOD operator can handle "any numeric expression".

http://msdn.microsoft.com/en-us/library/gg264708.aspx

The funny part about that article is 2 fold:

1) the MSDN articles were copied verbatim from office help files, or vice versa. Whatever cost less money for some poor soul to do by the hour.

2) notice this part of the file:
Usually, the data type of result is a Byte, Byte variant, Integer, Integer variant, Long, or Variant containing a Long, regardless of whether or not result is a whole number.

USUALLY? Well, in MS terms, if you look deep enough into this they're basically saying that the result can't be longer than a LONG. But you're left to figure that out for yourself (as you just did!). :p

Why:
Abs(5463513391) - (181440 * Int(Abs(5463513391)/181440))

Rather then:
5463513391 - (181440 * Int(5463513391/181440))
?

Does it matter? iNet is putting the ABS() function in to cover the scenario where you pass a negative val to the function. If you do pass one, you'll get the MOD number back but with a nice little "-" sign attached to it.
 

vbaInet

AWF VIP
Local time
Today, 18:24
Joined
Jan 22, 2010
Messages
26,374
Does it matter? iNet is putting the ABS() function in to cover the scenario where you pass a negative val to the function. If you do pass one, you'll get the MOD number back but with a nice little "-" sign attached to it.
The main point is that you will get undesired results without Abs(). So it wasn't really about having a prefixed minus sign. Remember that minus * minus = plus, so this part
Code:
 - (181440 * Int((-5463513391)/181440))
will evaluate to
Code:
[COLOR=Red]-[/COLOR] ([COLOR=Red]-[/COLOR]5463521280) 
=
[COLOR=Red]+[/COLOR] 5463521280
As you can see, it will give undesired results, hence the Abs()
 

Users who are viewing this thread

Top Bottom