VB.NET vs. C# arithmetics :>

  • Thread starter Thread starter Matijaz
  • Start date Start date
M

Matijaz

Hello,
I have noticed strange behaviour. I have .NET CF project written partly in
VB, there comes also a set of function written in C#. I have 2 functions for
counting PIN number - both based on the same algorithm - and results are
different. Is there a different pattern in rounding numbers in VB and C#?
Here comes the code:

public static int getPIN(long serialNo)
{
int myPIN, byteA, byteB, byteC;
long myLongValue;
if (serialNo > 999999)
{
myPIN = 0;
}
else
{
byteA = System.Convert.ToInt32((((serialNo / 1000) % 10) * 10) +
((serialNo / 10000) % 10));
byteB = System.Convert.ToInt32((((serialNo / 100000) % 10) * 10) +
((serialNo / 10) % 10));
byteC = System.Convert.ToInt32(((serialNo % 10) * 10) + ((serialNo /
100) % 10));
myLongValue = (byteA * byteA) + (byteB * byteB) + (byteC * byteC);
myPIN = System.Convert.ToInt32(myLongValue % 10000);
if (myPIN == 0) myPIN = 4646;
}
return myPIN;
}

Public Shared Function getPIN(ByVal SerialNo As Long) As Integer
Dim myPIN, byteA, byteB, byteC As Integer
Dim myLongValue As Long
If SerialNo > 999999 Then
myPIN = 0
Else
byteA = System.Convert.ToInt32((((SerialNo / 1000) Mod 10) *
10) + ((SerialNo / 10000) Mod 10))
byteB = System.Convert.ToInt32((((SerialNo / 100000) Mod 10)
* 10) + ((SerialNo / 10) Mod 10))
byteC = System.Convert.ToInt32(((SerialNo Mod 10) * 10) +
((SerialNo / 100) Mod 10))
myLongValue = (byteA * byteA) + (byteB * byteB) + (byteC *
byteC)
myPIN = System.Convert.ToInt32(myLongValue Mod 10000)
If myPIN = 0 Then
myPIN = 4646
End If
End If
Return myPIN
End Function
 
I have noticed strange behaviour. I have .NET CF project written partly in
VB, there comes also a set of function written in C#. I have 2 functions for
counting PIN number - both based on the same algorithm - and results are
different. Is there a different pattern in rounding numbers in VB and C#?

In the C# code you get integer division since serialNo and the
denominator literals are all integers.

In VB you get floating point division because you use the / operators.

To get integer division in VB you use the \ operator. To get floating
point division in C#, the numerator or denominator (or both) must be a
floating point type.


Mattias
 
I have noticed strange behaviour. I have .NET CF project written partly
in
In the C# code you get integer division since serialNo and the
denominator literals are all integers.

In VB you get floating point division because you use the / operators.

To get integer division in VB you use the \ operator. To get floating
point division in C#, the numerator or denominator (or both) must be a
floating point type.

Thanks Mattias. I need integer division. Those're my first steps with VB.
 
Back
Top