Result of the same account is different in VB and C#

Asked

Viewed 49 times

1

I’m trying to use a formula that gives me the expected result in C#, when translating the code to VB.NET the result is completely different.
I’m almost sure I did the translation correctly. I forgot something?

Formula in C#:

double x = 199025 / (double)(1 << 19) * 360.0 - 180;
// Resultado: -43.3403778076172

Link to test online: https://onlinegdb.com/uUuQD8kMG

Formula in VB.NET:

Dim x as Double = 199025 / CType((1 << 19) * 360.0 - 180, Double)
// Resultado: 0.00105447339908394

Link to test online: https://onlinegdb.com/02c5RaQTP

  • different code = different results :) just out of curiosity, how did you get into this? is validating the framework? ;)

  • 1

    If you’re talking about the formula, I took it off the Openstreetmaps website to convert the X coordinate to Longitude. https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#C.23

  • 1

    legal, thanks for the information

2 answers

2


Clearly the codes are different, so the result is expected to be different too.

Note that the Sts are done differently, but if you do the code in a similar way the results are the same:

C#:

double x = 199025 / (double)(1 << 19) * 360.0 - 180;

Vb.Net:

dim x as Double = 199025 / CDbl(1 << 19) * 360.0 - 180
  • It worked! I was passing everything to double instead of just the part (1 << 19). Is there any difference in using CType and CDbl?

  • 1

    yes, what really spoils the result is the CType(...., double), I left it that way in the answer because it’s easy to compare with the code in C# :)

1

Natan, in the case of C# its casting applied only to (1 << 19), while in VB.net the conversion applied to the entire account after splitting.

Replace the C#code by adding parentheses for the conversion to give the entire result:

double x = 199025 / (double)((1 << 19) * 360.0 - 180);

If the C# result is correct (-43.3403778076172) then fix the VB.net code by changing the scope of the conversion:

Dim x as Double = 199025 / CType((1 << 19), Double) * 360.0 - 180
  • It worked! Had not attacked me in parentheses. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.