Bust out your old trig textbooks to write some great code.
As programmers, we're often challenged with unusual requests, and we have to find creative ways to solve the problem.
Here is one such challenge. Based on the client's legacy code, we need to calculate mileage between two cities. The legacy code has a file, but it's not always populated correctly.
The solution is easier than you think. Using ZIP code coordinates, we can figure out how many miles are between cities--as the crow flies. There are Web services out there to calculate driving miles, but that's another article.
The best way to conquer this challenge is to use the cosine advanced math function. (For more information on cosine, just Google it.)
We all know that regular RPGLE doesn't allow advanced math functions such as trigonometry, so we need to add the C functions library to our RPGLE programs. First, we need to find latitude and longitude for a specific city. I used a third-party file that had all U.S. cities' coordinates by ZIP code. To best visualize this, follow the code. The two cities' ZIP codes in this example are 45817 (Bluffton, Ohio) and 45365 (Sidney, Ohio). The miles between them are 43.07.
H DFTACTGRP(*NO) BNDDIR('QC2LE')
DLAT1 S 8 6 inz(40.882285)
DLAT2 S 8 6 inz(40.286524)
DLON1 S 8 6 inz(-83.911911)
DLON2 S 8 6 inz(-84.155059)
DX S 10 2
DY S 10 2
D Miles S 12 2
DCosine PR 8F ExtProc('cos')
D Double 8F value
Include the C function library in your program QC2LE. For this example, I hard-coded some coordinates for you (I know it's bad, but it works in this example). You will want to use your own file or screen to pass to your program for more flexibility. Make a procedure called "cosine" with the external process called "cos." Yes, it needs to be lowercase. The return is an eight-position floating point field.
X = 69.1 *(lat2 - lat1);
Y = 69.1 *(lon2 - lon1) * cosine(lat1/57.3);
MILES = %SQRT(x*x + y*y);
//(result is 43.07 for this example)
*in69 = *on;
*inlr = *on;
The radius of the earth is 69.1 kilometers. Multiply the difference of latitude 2 minus latitude 1 by 69.1. Do the same for longitude, but multiply by the cosine of latitude 1 divided by 57.3. (To convert latitude or longitude from decimal degrees to radians, divide the latitude and longitude values in this database by 180/pi, or approximately 57.29577951.) You could do this without the cosine, but I found the results were not very accurate.
Next, use the square root of X*X + Y*Y, and you get miles. (If you use Mapquest to check this, it will likely show more miles from city to city. Again, this is because this example calculates distance as the crow flies, so there are no roads to consider.) To make this code more efficient, get a third-party file or use a Web service to get coordinates. There are lots of free Web services and files out there. Then just pass parameters to the program and return the miles.
This concludes today's tip. Using the C function on RPG adds lots to the functionality of RPGLE. There are many other C function APIs you can use as well. Most of them are more advanced and require a little more research. So dust off your old trig textbooks and start coding. I hope this was helpful to at least a few coders. Stay tuned for more tips like this in the future.