# Calculating the earth radius at latitude

I tried implementing the function (in Javascript) to calculate the horizontal radius for a given latitude. But nobody specified the units and I am at a loss why my function simply outputs enormous values:

``````var WGS_ELLIPSOID = { a: 6378137.0, b: 6356752.314 }; // meter

var f1 = Math.pow((Math.pow(WGS_ELLIPSOID.a, 2) * Math.cos(lat)), 2);
var f2 = Math.pow((Math.pow(WGS_ELLIPSOID.b, 2) * Math.sin(lat)), 2);
var f3 = Math.pow((WGS_ELLIPSOID.a * Math.cos(lat)), 2);
var f4 = Math.pow((WGS_ELLIPSOID.b * Math.sin(lat)), 2);

var radius =  Math.sqrt((f1 + f2) / (f3 / f4));

}
``````

For something like 0 (equator) it returns 0 (incorrect). For something like 45 degree north, it returns an astronomial value of 65354531512255.51, which I am not sure what unit it has. Also, it's completely wrong - the radius of earth has a maximum of 6378137 m.

Meanwhile, if I do: `6371000.0 * Math.cos(lat)`, I get 3346826.3907577563 (supposedly meter?) for 45 degree north. Which is much more plausible, if the earth at the equator has a radius of 6000 km, 3300 for 45° north is OK, but this does not account for the earths flatness.

What am I doing wrong? Is there an error in the formula or some Javascript weirdness or are my units off? The input is in degrees, not radians (I assumed this because of the `cos()` functions).

The formula I used:

``````radius(lat) = sqrt( (a^2 cos(lat))^2 + (b^2 sin(lat))^2 ) /
( (a cos(lat))^2   + (b sin(lat))^2 ) )
``````

You made an error in the line defining the `radius` in your function. Instead of `f3 / f4` it should be `f3 + f4`. The corrected line should be as follows -

``````var radius =  Math.sqrt((f1 + f2) / (f3 + f4));
``````

Also the `Math.sin()` and `Math.cos()` functions take the inputs a radians. So if your input for `lat` is in degrees then you would need to convert that. Add a line before defining `f1` in the function -

``````lat  = lat * (Math.PI / 180);
``````
• Thank you for your answer. I've corrected these mistakes, but the function is still wrong. It returns the correct value for 0 degrees, but near 90 degrees, it still shows the value of the equator. For an input of 89.99 for example, (near north pole), the expected outcome is something around 1100 meters. The formular returns 6356752.31 (with the corrected mistakes). I am beginning to doubt the Wikipedia formula, it seems that it just interpolates between a and b. May 30, 2017 at 17:59
• Sorry, I am stupid. Of course it interpolates between a and b, because I am calculating the radius from the earths center to the surface. So even at the north pole, this has to be somwhere in the 6000km region. I have to calculate the horizontal radius by using cosinus. May 30, 2017 at 18:07

The solution described is correct, to be more concrete - here is an formula for calculation: And, `Swift` version of code:

``````extension CLLocationCoordinate2D {

func earthDistance() -> CLLocationDistance {