Unfortunately, the term "scale factor" is ambiguous.
In cartography, maps and projections, the concept and application of scale is of fundamental importance.
By definition, it is a factor – meaning it is something to multiply or divide – so whether "scale" or "factor" is the adjective the particular word pairing has no obvious meaning, except in a particular context:
Every map or globe has a (stated or unstated) scale
The map or globe scale is a ratio of distance on map or globe to corresponding distance on the ground or reality.
Either it has no units or its units reflect the map and ground units – miles per inch, km per cm, etc.
It is variously called scale, map scale, principal scale, representational fraction, or nominal scale.
I like that last one, nominal scale, because most maps have a single statement of its scale.
Sadly, it is sometimes also called scale factor.
Every map projection results in a continuous variation in scale
All map projections distort linear scale, all over the map.
This distortion is almost always termed scale factor (and sometimes "projection scale factor" or "point scale factor").
At any point on the map, it is the ratio of the "true" (undistorted) scale and the "nominal" (distorted) scale.
In other words, it is the ratio of the true ground distance to the implied distance on the map.
Scale and computing a map projection
When computing a map projection, that is
(X, Y) ← projection (λ, φ)
you need to have some constant which depends on the size of your region of interest, the size of your map, and the map units involved.
That constant is our friend the nominal scale.
Since you don't provide the full code, and it's a little cryptic, I cannot say for certain, but I suspect that is what is meant by "scale factor" in your particular problem.
According to Mathematics_of_the_Mercator_projection on wiki, the spherical case, which uses radius, R, as a substitute for scale:
X = R λ
Y = R ln [tan (π/4 + φ/2)]
(That looks similar to your code.)
How is radius a substitute for scale? Simple. It is the constant which determines the size of the map: a larger globe yields a larger map. If R is the Earth's radius, then your map scale will be one-to-one. If R is the radius of your globe in, say, mm, inches, or pixels, then X,Y will be in those same units and the map's nominal scale, NS, will be the ratio of your globe's radius to the Earth's radius:
NS = globe radius / Earth radius
To get ground distances from a measurement on a projected map, including any distortions:
ground distance = map distance / NS
To remove distortion, see below.
Assessing scale distortion of a map projection
To properly correct distortion at any point on a projected map, you ought to be able to calculate the distortion, SF (scale factor):
SF ← distortion (λ, φ)
In this case, SF has to be calculated, or provided in a look-up table, wherever it is needed.
Does your code calculate "scale factor" as a function of "lam" and "phi"? I doubt it.
According to Mathematics_of_the_Mercator_projection on wiki, which uses K for scale factor:
Spherical case: K = sec φ
Ellipsoidal case: K = sec φ sqrt(1 - e2 sin2 φ)
where e2 is about 0.006 for all reference ellipsoids.
To correct for any projection distortion, i.e., to convert a projected map distance to get true (undistorted) globe distance, always divide by the scale factor, SF:
ground distance = map distance / SF
That might look familiar.
Are the constant nominal scale and the variable scale factor used in the same way?
Yes, they're used in exactly the same way.
However, whenever the Earth is really projected onto a map that is actually measured in terms of map units (mm, cm, inches, pixels, etc.), then you need to apply both
- a global nominal scale to get the correct globe/earth magnitude and
- a local scale factor to correct for projection distortion at any particular Earth position.
If you are not making measurement on an actual map, and you are just using coordinates that are in the same units as your Earth radius, R, then your nominal scale is trivial (NS = 1) and you only need to use the scale factor.