Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-16-2008, 11:39 PM
puter puter is offline
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0
puter is on a distinguished road
Default Strange rounding

doing a simple calculation using C#. I am setting distance = speedConv / 3600.

speedConv is between 0 and 60. When I print out all the values speedConv, for example is 30...but distance is always 0. I have calculated several values and they are in the area of .005, which should be well within the range of a double.

Does anyone know of any reason this value would be getting rounded to 0?


Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-17-2008, 12:12 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default Re: Strange rounding

How do you print it? Your problem could be that you're using a function or a format, which only allows you to print integers, and that will therefore result in the missing digits.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 01:09 AM
justto_doit justto_doit is offline
Newbie
 
Join Date: Apr 2008
Posts: 6
Rep Power: 0
justto_doit is on a distinguished road
Default Re: Strange rounding

Hello,
May I know what is the data type of

distance
speedConv
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 07:33 AM
puter puter is offline
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0
puter is on a distinguished road
Default Re: Strange rounding

distance and speedConv are both doubles.

I know the printing method works because when I print out another double (MPG) it prints out as a number similar to 50.22342123

So, that tells me that there should 1) be plenty of decimal places and 2) not be a problem with the printing.

It's almost like it's taking a number that is less than 1 and rounding it to zero for unknown reasons. Oh, and this is a debug print.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 07:51 AM
justto_doit justto_doit is offline
Newbie
 
Join Date: Apr 2008
Posts: 6
Rep Power: 0
justto_doit is on a distinguished road
Default Re: Strange rounding

its funny~
I have do something similar but I cannot get this error

e.g.
double distance;
double speedConv;
for (speedConv = 0; speedConv < 30; speedConv++)
{
distance = speedConv / 3600;
Debug.WriteLine(distance);
}

or may you point out where is the difference between us?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-17-2008, 07:58 AM
puter puter is offline
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0
puter is on a distinguished road
Default Re: Strange rounding

Quote:
Originally Posted by justto_doit View Post
its funny~
I have do something similar but I cannot get this error

e.g.
double distance;
double speedConv;
for (speedConv = 0; speedConv < 30; speedConv++)
{
distance = speedConv / 3600;
Debug.WriteLine(distance);
}

or may you point out where is the difference between us?
only difference is that yours is counting up in order.

Right now I'm generating random data to test my code, so they are never the same number.

That should not affect the test.

What error are you getting? I don't get an error, I just get a distance of 0.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-17-2008, 10:09 AM
puter puter is offline
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0
puter is on a distinguished road
Default Re: Strange rounding

I figured it out, but I think it's stupid.

apparently if you divide a double by an int literal C# thinks it would be smart to just change that thar double output to an int.

dunno why the developers thought that was a good idea, but changing it to 3600.0 fixed it.

There wasn't even a warning for the loss of precision.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-17-2008, 02:45 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,001
Last Blog:
Piano Exam
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: Strange rounding

If you divide the answer using two integer values, then of course the answer will be an integer. You're right, changing it to 3600.0 does the job.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-17-2008, 03:09 PM
puter puter is offline
Newbie
 
Join Date: Apr 2008
Posts: 5
Rep Power: 0
puter is on a distinguished road
Default Re: Strange rounding

I'm not using two integer values tho.

I'm dividing a double by an integer.

it's stupid that 1) the compile goes to the less precise result and 2) does not produce a warning about loss of precision.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-17-2008, 03:43 PM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,001
Last Blog:
Piano Exam
Rep Power: 26
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: Strange rounding

It's known as an "implicit" cast - there is no potential loss of precision, because by passing in 3600 and not 3600.0, you are effectively telling C# you only want that level of accuracy in your answer.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Characters. bladactania C and C++ 2 04-03-2008 08:09 AM
Strange but i need it. mandriva Website Design 4 11-10-2007 11:29 AM
Strange error in update app McMillan0520 C# Programming 1 08-08-2007 11:52 AM
Strange DLL Error, creating controls ChrisPepper1989 C and C++ 0 02-26-2007 04:37 PM


All times are GMT -5. The time now is 06:47 PM.

Contest Stats

John ........ 87.50000
dargueta ........ 75.00000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads