Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# and Javascript code calculations giving different results

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?

like image 330
taiteilijaumbra Avatar asked Aug 16 '18 14:08

taiteilijaumbra


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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.

like image 74
Bagdan Gilevich Avatar answered Oct 08 '22 21:10

Bagdan Gilevich