Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

Divide error during debug

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mudman - 03 Jun 2005 21:58 GMT
I'm running Visual Studio.NET and am experiencing a problem during debugging
(i assume this will also be the case during normal operation). If the
variable "test" is a double and "denom" a finite integer, the operation test
= 1 / denom; results in test having a value of 0.000000! However, if denom is
also a double, test has the correct value. Dumb question perhaps, but am i
missing something?
William DePalo [MVP VC++] - 03 Jun 2005 23:53 GMT
> I'm running Visual Studio.NET and am experiencing a problem during
> debugging
[quoted text clipped - 5 lines]
> also a double, test has the correct value. Dumb question perhaps, but am i
> missing something?

If you have something like this

   int denom;
   double test;

   test = 1 / denom;

then test will be assigned a value of zero (where denom is not 0). That's
because the division happens in integers, it yields a result of 0 and
integer 0 is converted to double 0.

If on the other hand you have an expression that contains integers and
doubles then the integers are promoted to doubles before the division and
assignment happen.

Is that what you see?

Regards,
Will
mudman - 06 Jun 2005 11:12 GMT
This would indeed explain what i am experiencing, although it is somewhat
illogical- essentially, the expression is being interpreted as:

test = (double)((int)(1 / denom));

Thanks.

> If you have something like this
>
[quoted text clipped - 10 lines]
> doubles then the integers are promoted to doubles before the division and
> assignment happen.
William DePalo [MVP VC++] - 06 Jun 2005 14:59 GMT
> This would indeed explain what i am experiencing, although it is somewhat
> illogical- essentially, the expression is being interpreted as:
>
> test = (double)((int)(1 / denom));

Is is that, or essentially that?

Your C style cast above

   (int)

forces the devision to be done in integers, no? And that zero result is cast
to double.

Why not try

   test = (double) 1 / denom;

> Thanks.

You are welcome.

Regards,
Will

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.