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 / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Floating Point Calculation Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DJ - 29 Sep 2006 21:16 GMT
Hello,

In an application that worked fine until recently I have started to have
problems with calculations involving two doubles.  For example: -24.708 +
26.0 = 1.2920000000000016, however,  -24.708 + 8.0 = -16.708.  What would
cause this?   I don't think the numbers are so large that it would be the
usual floating point calculation error because of rounding.  How can I fix
it?  I'd rather not change every calcuation in my program to include a
math.round...

.Net Framework 1.1 version 1.1.4322 SP1
Visual Basic - Visual Studio .Net 2003
Windows XP

Thanks,
Chris Saunders - 29 Sep 2006 22:42 GMT
The size of your numbers are not relevant to this issue.
Here is an URL with an article you should probably read if you wish to
better
understand this issue: http://docs.sun.com/source/806-3568/ncg_goldberg.html

I know nothing about your application but you may indeed need something
along the lines you suggested.

Regards
Chris Saunders

> Hello,
>
[quoted text clipped - 11 lines]
>
> Thanks,
Jon Skeet [C# MVP] - 30 Sep 2006 19:13 GMT
> In an application that worked fine until recently I have started to have
> problems with calculations involving two doubles.  For example: -24.708 +
[quoted text clipped - 3 lines]
> it?  I'd rather not change every calcuation in my program to include a
> math.round...

See http://www.pobox.com/~skeet/csharp/floatingpoint.html

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nick Malik [Microsoft] - 01 Oct 2006 05:11 GMT
> Hello,
>
> In an application that worked fine until recently I have started to have
> problems with calculations involving two doubles.  For example: -24.708 +
> 26.0 = 1.2920000000000016, however,  -24.708 + 8.0 = -16.708.  What would
> cause this?

I don't see a problem here.  Why do you?

> I don't think the numbers are so large that it would be the
> usual floating point calculation error because of rounding.

Who said size has anything at all to do with this calculation?  Rounding
doesn't just happen for very large numbers.  It happens for all of them.

> How can I fix
> it?

Decimal datatype

> I'd rather not change every calcuation in my program to include a
> math.round...
[quoted text clipped - 4 lines]
>
> Thanks,

Rate this thread:







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.