473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

A Strange c# - vb Difference

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
Jun 1 '06 #1
6 2148
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]
- mv*@spam.guard.caspershouse.com

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:ul****************@TK2MSFTNGP04.phx.gbl...
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

Jun 1 '06 #2
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
"Tina" <ti**********@nospammeexcite.com> wrote in message
news:ul****************@TK2MSFTNGP04.phx.gbl... 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

Jun 1 '06 #3
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uM****************@TK2MSFTNGP03.phx.gbl... 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]
- mv*@spam.guard.caspershouse.com

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:ul****************@TK2MSFTNGP04.phx.gbl...
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


Jun 1 '06 #4
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
Jun 1 '06 #5

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:ul****************@TK2MSFTNGP04.phx.gbl...
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.
Jun 1 '06 #6
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
Tina wrote:
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

Jun 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
3
by: Bill C. | last post by:
Hi, I've got a simple console app that just reads an XML file into a DataSet then prints out a description of each table in the DataSet, including column names and row values for each column. ...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
1
by: Kepler | last post by:
I'm fighting a really strange bug that involves both a DataGrid and a Repeater disappearing on postback. The strange thing is that I've distilled the problem down to a simple program, that...
1
by: Martin Feuersteiner | last post by:
Dear Group I'm having a very weird problem. Any hints are greatly appreciated. I'm returning two values from a MS SQL Server 2000 stored procedure to my Webapplication and store them in...
3
by: skOOb | last post by:
I am having an issue right now of displaying the 'Age' of an item, but only on 1 computer. I can take my app and install it on multiple win98, winNT, and other winXP machines and have no problem. ...
7
by: Tina | last post by:
I'm using 1.1/vs.net 2003 ... C#example.............. string myPath = Server.MapPath(".\\Data\\" + (string) Session); Produces this value in myPath ?myPath
4
by: kj | last post by:
I'm running into a strange seg fault with the module cjson. The strange part is that it does not occur when I run the code under Emacs' Pydb. Here's an example: import sys, cjson d1 =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.