A Strange c# - vb Difference

T

Tina

This C# code produces a value of 0.0 in myAspectRatio....

int myWidth = 1200;
int myHeight = 1600;
double myAspectRatio = 0.0;
myAspectRatio = myWidth / myHeight;


This VB code produces a value of .75 (the correct answer) in myAspectRatio..

Dim myWidth As Integer = 1200
Dim myHeight As Integer = 1600
Dim myAspectRatio As Double = 0.0
myAspectRatio = myWidth / myHeight

Why does the C# code produce the incorrect answer of 0.0?

Thanks,
T
 
G

Greg Young

Because the operation is being done on two integers (which produces and
integer result) which is then converted to a floating point.

try
int myWidth = 1200;
int myHeight = 1600;
double myAspectRatio = 0.0;
myAspectRatio = (double) myWidth / (double) myHeight;

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
 
N

Nicholas Paldino [.NET/C# MVP]

Tina,

It's not incorrect, but rather, it is the way that C# is supposed to
work.

When you divide two integers by each other in C#, it will produce the
integer result. Any remainder is going to be dropped.

In this case, 0 is produced. Then, it is cast to a double, producing
0.0.

If you want to get the result of .75, then one of the operands has to be
a floating type (single, double). You would have to do this:

myAspectRatio = (float) myWidth / myHeight;

Hope this helps.
 
G

Greg Young

Nicholas ..
myAspectRatio = (float) myWidth / myHeight;

its a double he is setting it to (should be double, not float this type of
code can cause precision problems)

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
Nicholas Paldino said:
Tina,

It's not incorrect, but rather, it is the way that C# is supposed to
work.

When you divide two integers by each other in C#, it will produce the
integer result. Any remainder is going to be dropped.

In this case, 0 is produced. Then, it is cast to a double, producing
0.0.

If you want to get the result of .75, then one of the operands has to
be a floating type (single, double). You would have to do this:

myAspectRatio = (float) myWidth / myHeight;

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tina said:
This C# code produces a value of 0.0 in myAspectRatio....

int myWidth = 1200;
int myHeight = 1600;
double myAspectRatio = 0.0;
myAspectRatio = myWidth / myHeight;


This VB code produces a value of .75 (the correct answer) in
myAspectRatio..

Dim myWidth As Integer = 1200
Dim myHeight As Integer = 1600
Dim myAspectRatio As Double = 0.0
myAspectRatio = myWidth / myHeight

Why does the C# code produce the incorrect answer of 0.0?

Thanks,
T
 
M

Michael Nemtsev

Hello Tina,

Specify type implicitly like myAspectRatio = (double) myWidth / myHeight;

it's a C-type language feature

T> This C# code produces a value of 0.0 in myAspectRatio....
T>
T> int myWidth = 1200;
T> int myHeight = 1600;
T> double myAspectRatio = 0.0;
T> myAspectRatio = myWidth / myHeight;
T> This VB code produces a value of .75 (the correct answer) in
T> myAspectRatio..
T>
T> Dim myWidth As Integer = 1200
T> Dim myHeight As Integer = 1600
T> Dim myAspectRatio As Double = 0.0
T> myAspectRatio = myWidth / myHeight
T> Why does the C# code produce the incorrect answer of 0.0?
T>
T> Thanks,
T> T
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

Michael A. Covington

Tina said:
This C# code produces a value of 0.0 in myAspectRatio....

int myWidth = 1200;
int myHeight = 1600;
double myAspectRatio = 0.0;
myAspectRatio = myWidth / myHeight;


This VB code produces a value of .75 (the correct answer) in
myAspectRatio..

Dim myWidth As Integer = 1200
Dim myHeight As Integer = 1600
Dim myAspectRatio As Double = 0.0
myAspectRatio = myWidth / myHeight

Why does the C# code produce the incorrect answer of 0.0?

Integer division.

When you divide an integer by an integer, in C and its derivatives, you get
an integer.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The results are different because the codes are different.

In C# the meaning of the / operator is decided by the data types of the
operands.

In VB there are two separate division operators. The / operator is used
for floating point division, and the \ operator is used for integer
operations.


The VB code:

myAspectRatio = myWidth / myHeight

is equivalent to the C# code:

myAspectRatio = (double)myWidth / (double)myHeight;


The C# code (where the operands are integers):

myAspectRatio = myWidth / myHeight;

is equivalent to the VB code:

myAspectRatio = myWidth \ myHeight
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SoapFormatter Help 1
C++ to C# 3
A design question 3
C# v. C++ Performance 14
strange c# - vb difference 7
Help With ICloneable And Deep Copy 5
C# optimization 22
Problem in Writting wrapper in Managed C++ 1

Top