Mod Function

394 views
Skip to first unread message

Glen

unread,
Jan 18, 2008, 11:55:45 AM1/18/08
to MapInfo-L
What is the Mod function in MB? what does it do?

and Yes I read the user guide

why do I get different results when I use Excel on the same formula
Using Mod?

is their a MB function the same as excel Mod function?

Glen

Gentreau

unread,
Jan 18, 2008, 1:48:01 PM1/18/08
to mapi...@googlegroups.com

The Mod operator does the same thing in MapBasic as it does in Excel, with
the exception of the syntax and the fact that it acts on integers only. The
Help files specifically says "remainder from integer division"

Eg a=10, b=3
Excel = Mod(a,b) = 1
MB = a Mod b = 1

Are you trying to use it with integers or float values ?


Gentreau

Bill Thoen

unread,
Jan 18, 2008, 3:02:31 PM1/18/08
to mapi...@googlegroups.com
No, it is different in Excel (and correct). MapInfo doesn't implement
the modulus of negative numbers properly (although mathematically it's
not "wrong", MapInfo's result is just not in the right form).

As Gentreau says, it's the remainder of a division, and in MapInfo's
case, they limit it to working on integers only. So, for example, the
expression: 17 mod 3 is equal to 17\3 = 5, with a remainder of 2, so 17
mod 3 is 2. (And the '\' is not a typo; that's MapInfo's integer
division operator.)

What it does in a higher sense is limit numbers to fixed range of
possibilities from 0 up to, but not including the mod value. And believe
it or not, that can be very useful. Here's a MapInfo example. Suppose
you had a table of test results that you wanted to split into three
sample groups. First, you'd create an integer column for record id's
(let's call it 'id') and then you'd fill it using the SQL statement:

UPDATE MyTable SET id = rowid

Then using the mod operator, you could create a sample group number
associated with every record that would be either 0, 1, or 2 with a
select statement, like so:

SELECT id MOD 3 "Group_Id", col1, col2, ..., coln FROM MyTable INTO Samples

Now, with that group_id field attached to the Samples table, you can
choose records from any of 3 groups that you want.

It's also useful in programming when you want to split a MapInfo color
number into its red, green and blue components (e.g. if nColor is your
color code then nColor MOD 256 is the blue value, (nColor \ 256) MOD 256
is the green, and nColor \ 256^2 is the red). It's very useful in
cryptography too.

In the case of negative numbers, the result should be adjusted into the
range 0 <= mod n < n, so, for example,
-17 MOD 3 = -2 is what MapInfo gets, but it should be 1 ( because even
though -2 and 1 are congruent in the mod 3 domain, if you get a negative
number from a mod operation it's usually added to the mod number so that
the result is positive.

Glen

unread,
Jan 18, 2008, 4:05:07 PM1/18/08
to MapInfo-L
I can get this to work in Excel but not in MB

the Mod function gives me different results

I can't seem to code it correctly in MB I get the correct numbers up
to the MOD function


Worked Examples:

Suppose point 1 is LAX: (33deg 57min N, 118deg 24min W)
In radians LAX is

lat1=(33+57/60)*pi/180=0.592539, lon1=(118+24/60)*pi/180=2.066470

An enroute waypoint 100nm from LAX on the 66 degree radial (100nm
along the GC to JFK) has lat and long given by:


100nm = 100*pi/(180*60)=0.0290888radians
lat = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
= asin(sin(0.592539)*cos(0.0290888)
+cos(0.592539)*sin(0.0290888)*cos(1.150035))
= asin(0.568087)
= 0.604180radians
= 34degrees 37min N

lon = mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
= mod(2.066470- asin(sin(1.150035)*sin(0.0290888)/
cos(0.604180))+pi,2*pi)-pi
= mod(2.034206+pi,2*pi)-pi radians
= 2.034206 radians
= 116 degrees 33min W

Glen

unread,
Jan 18, 2008, 4:29:53 PM1/18/08
to MapInfo-L
1.150035 radians
= 66 degrees

Glen

unread,
Jan 18, 2008, 4:31:16 PM1/18/08
to MapInfo-L
TC = 1.150035 radians = 66 degrees

Bill Thoen

unread,
Jan 18, 2008, 4:39:32 PM1/18/08
to mapi...@googlegroups.com
That's because the mod function in MB is integer-only. You'll need to
build your own version of fmod() (MOD designed for floating point numbers).

Glen

unread,
Jan 18, 2008, 4:44:13 PM1/18/08
to MapInfo-L
Thanks Bill

At least I can stop looking for the "why" it doesn't work


FMOD(n, d) = n - d*INT(n/d)


any idea on a Lat/ Long given radial and Distance that is not Great
Circle

example from a given Lat/ Long 500 meters at 120 Deg return that
point?

Glen

Gentreau

unread,
Jan 18, 2008, 5:22:59 PM1/18/08
to mapi...@googlegroups.com

Hi Glen,

That's straight-forward, so long as the distances are reasonably short and
you don't want to be accurate within centimetres.
Just use standard trig and forget about great circles.

You need to know that 1 deg of latitude (y) ~ 111km
And that 1 deg of longitude (x) is ~ 111km * cos(latitude)

You know x1 and y1 (long & lat), the angle in degrees a and the distance in
metres d

x2 = (x1 + (d*sin(a)) / (111000 * cos(y1))
y2 = (y1 + (d*cos(a)) / 111000

You will need to convert angles to radians to use this in MapBasic, but
that's the basic calculation.

Hth

Gentreau.


-----Original Message-----
From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On
Behalf Of Glen
Sent: Friday, January 18, 2008 10:44 PM
To: MapInfo-L

Gentreau

unread,
Jan 18, 2008, 5:26:02 PM1/18/08
to mapi...@googlegroups.com

Oops, error there, brackets in wrong place.

Should read:
x2 = x1 + ((d*sin(a)) / (111000 * cos(y1)))
y2 = y1 + ((d*cos(a)) / 111000 )

Message has been deleted

Glen

unread,
Jan 21, 2008, 10:59:24 AM1/21/08
to MapInfo-L
Bill and Gentreau

thanks for the help I now have both examples working ;-)
Reply all
Reply to author
Forward
0 new messages