jeffpkamp
jeffpkamp

Reputation: 2866

VB6 Mod function gives incorrect values with negative values

I have a step in a script in vb6 that is failing. the code is as follows

output = ((azmnum + steps ) mod 16777216)

the values the variables in the function are

-850344 = (5184326 + -6034670) mod 16777216)

All variables are long numbers. all other programs I enter these values into (python and excel) return 15926872. I can't figure out why the modulo is being ignored.

Upvotes: 0

Views: 1341

Answers (1)

cup
cup

Reputation: 8267

mod is not the same in all languages, especially for negative numbers. VB6 (and a whole load of other compilers like C, C++, C#, Java) takes the Fortran interpretation which is the remainder after dividing. Mathematically, this is the wrong interpretation if the number is negative. What you have is

5184326 + -6034670 = -850344
-850344 mod 16777216 = -850344 

Python and excel take the correct interpretation of modulo where the result is always positive. This takes an extra step i.e.

-850344 + 16777216 = 15926872

Upvotes: 2

Related Questions