C# and Javascript code calculations giving different results

  • A+
Category:Languages

I'm doing a Unity project where I have the need to convert UTM coordinates to latitudes and longitudes. I have tried several C# solutions, but none of them were accurate enough. But I found some Javascript code that gives out the exact result I'm looking for (https://www.movable-type.co.uk/scripts/latlong-utm-mgrs.html). The problem is, when I turned the code into C#, it gives out a different result. Here are the pieces of code I see the problem in:

Javascript:

var a = 6378137; var f = 1/298.257223563; var e = Math.sqrt(f*(2-f)); var n = f / (2 - f); var n2 = n*n, n3 = n*n2, n4 = n*n3, n5 = n*n4, n6 = n*n5; var A = a/(1+n) * (1 + 1/4*n2 + 1/64*n4 + 1/256*n6); 

And the C# code I converted myself:

var a = 6378137; var f = 1 / 298.257223563; var e = Math.Sqrt(f * (2 - f)); var n = f / (2 - f); var n2 = n * n; var n3 = n2 * n; var n4 = n3 * n; var n5 = n4 * n; var n6 = n5 * n; var A = a / (1 + n) * (1 + 1 / 4 * n2 + 1 / 64 * n4 + 1 / 256 * n6); 

They should be identical, but the value of A is different. In Javascript it's 6367449.145823415 (what I want), but C# gives 6367444.6571225897487819833001. I could just use the Javascript code in my project, but conveniently Unity stopped supporting Javascript just last year.

The inputs are the same for both functions. What could be the issue here?

 


You have an issue in the last expression

var A = a / (1 + n) * (1 + 1 / 4 * n2 + 1 / 64 * n4 + 1 / 256 * n6); 

In C# 1 / 4 * n2 is evaluated to 0 since 1 and 4 are considered as integers by default and integer division 1 / 4 gives 0. Same thing happens to 1 / 64 * n4 and 1 / 256 * n6. But in JavaScript there are only 64-bit floating point numbers, so 1 / 4 is evaluated to 0.25.

Possible workaround:

var A = a / (1 + n) * (1 + 1 / 4.0 * n2 + 1 / 64.0 * n4 + 1 / 256.0 * n6); 

Now answers seem to be exactly the same.

Note: as @Lithium mentioned, you may find more elegant to add d rather than .0 to the end of the number to indicate it as double.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: