473,407 Members | 2,312 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,407 software developers and data experts.

csharp and vb got different result in math calculation WHY??????

Why does for the same code I got different result
the VB code gave me what I want why not the csharp?

Thank you for your help...

CSHARP CODE
int sx, sy;
double sdegrees;

sdegrees = (90 - (5/15) * 90) * (System.Math.PI / 180);

sx = (int) System.Math.Round(95 * System.Math.Cos(sdegrees));

sy = (int) -(System.Math.Round(95 * System.Math.Sin(sdegrees)));

MessageBox.Show(sdegrees.ToString() + "\n" + sx.ToString() + "\n" +
sy.ToString());
VB CODE

Dim sx, sy As Integer

Dim sdegrees As Double

sdegrees = (90 - (5 / 15) * 90) * (System.Math.PI / 180)

sx = System.Math.Round(95 * System.Math.Cos(sdegrees))

sy = -(System.Math.Round(95 * System.Math.Sin(sdegrees)))

MessageBox.Show(sdegrees.ToString + vbCr + sx.ToString + vbCr + sy.ToString)
Nov 15 '05 #1
2 3930

"Nicolas" <nl*****@hotmail.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
Why does for the same code I got different result
the VB code gave me what I want why not the csharp?

Thank you for your help...


Because "/" is a different operator in VB than in C#.

In VB "/"

Divides two numbers and returns a floating-point result.
In C# "/"

The division operator (/) divides its first operand by its second. All
numeric types have predefined division operators.

Which means that in VB the the operands to "/" are converted to
floating-point types before the operator operates on them. In C# the the
operands are not converted. That means in C# when "/" operates on 2 int32's
the result is an int32. It's integer division. And so 5/15=0.

In VB 5/15 = 5.000/15.000 = 0.3333. VB uses "\" for integer division
because it's a very helpful language. C# assumes that you know what you're
doing and know that your literals will be evaluated as integers, and that
when you write 5/15, you really want 0.

David
Nov 15 '05 #2
Thanks a lot and it works now and you're right, I'm new in cSharp.

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:eo**************@TK2MSFTNGP10.phx.gbl...

"Nicolas" <nl*****@hotmail.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
Why does for the same code I got different result
the VB code gave me what I want why not the csharp?

Thank you for your help...

Because "/" is a different operator in VB than in C#.

In VB "/"

Divides two numbers and returns a floating-point result.
In C# "/"

The division operator (/) divides its first operand by its second. All
numeric types have predefined division operators.

Which means that in VB the the operands to "/" are converted to
floating-point types before the operator operates on them. In C# the the
operands are not converted. That means in C# when "/" operates on 2

int32's the result is an int32. It's integer division. And so 5/15=0.

In VB 5/15 = 5.000/15.000 = 0.3333. VB uses "\" for integer division
because it's a very helpful language. C# assumes that you know what you're doing and know that your literals will be evaluated as integers, and that
when you write 5/15, you really want 0.

David

Nov 15 '05 #3

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

Similar topics

16
by: Frank Millman | last post by:
Hi all I was helping my niece with her trigonometry homework last night. Her calculator's batteries were flat, so I thought I would use Python's math module to calculate sin, cos, and tan. I...
15
by: Quackker12 | last post by:
is there a function in ansi c that does this? sigma(2^n/n!,0,infinity,n); syntax sigma(expression, start, end, variable); may be expression could be a function.... I know you can do this...
5
by: Vedran Furač | last post by:
I think that this results must be the same: In : math.atan2(-0.0,-1) Out: -3.1415926535897931 In : math.atan2(-0,-1) Out: 3.1415926535897931 In : -0 == -0.0 Out: True
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
3
by: Hugh Janus | last post by:
Hi all, does anyone know why the following code gives me totally different results when running on the Windows Mobile 5 emulator and a real Windows Mobile 6 device? I am trying to find out the...
12
by: jlgraham | last post by:
I have an assignment that wants me to write an application that takes 5 numbers from a user. As the user inputs the numbers I have to make them into a double, float, long, int and byte. All I need...
6
by: Avi | last post by:
I need to implement the following calculation: f = (a*b) + (c*d) where a,b,c,d are given double values and f is a double variable for the result I found out that using two different...
2
Alireza355
by: Alireza355 | last post by:
Dear all I have one query which selects some items from my main table and inserts the results into a fresh temporary table. in this table, there is a column that remains empty to be filled with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.