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 / January 2008

Tip: Looking for answers? Try searching our database.

Double is doing incorrect subtraction

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Sokalski - 12 Jan 2008 19:53 GMT
I have the following code:

 Dim x As Double = 45.333
 Dim y As Double = 45
 Dim z As Double = x - y
 Me.Label1.Text = z.ToString()

The result should obviously be 0.333, but the value displayed in
Me.Label1.Text is:

0.332999999999998

I have seen stuff like this in other situations, but I never managed to
figure out how to fix it. Can somebody help me here?
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

Teemu - 12 Jan 2008 20:12 GMT
>I have the following code:
>
[quoted text clipped - 10 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?

In order to show the correct result you have to format it. For example you
could do this:
Me.Label1.Text =z.ToString("0.000")

Doubles are floating point numbers and that causes this behavior.

If you want to find more information try keyword "IEEE 754" with Google.

-Teemu
Family Tree Mike - 12 Jan 2008 22:08 GMT
Consider using decimal instead of double if you want to get the answer you
expect below.

> I have the following code:
>
[quoted text clipped - 10 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?
Erik Funkenbusch - 13 Jan 2008 03:01 GMT
> I have the following code:
>
[quoted text clipped - 10 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?

Double is doing entirely correct subtraction.

The problem is that Double is an implementation of IEEE floating point, and
IEEE floating point is incapable of representing all numbers exactly.

Read this:

http://support.microsoft.com/kb/42980
Cor Ligthert[MVP] - 13 Jan 2008 06:09 GMT
Nathan,

This is basic

Float, Double or whatever floating poing calculation for mathimatical
programming,
Decimal for business programming,

Cor
Herfried K. Wagner [MVP] - 13 Jan 2008 12:32 GMT
"Nathan Sokalski" <njsokalski@hotmail.com> schrieb:
>  Dim x As Double = 45.333
>  Dim y As Double = 45
[quoted text clipped - 8 lines]
> I have seen stuff like this in other situations, but I never managed to
> figure out how to fix it. Can somebody help me here?

In addition to the other replies, take a look at these articles:

IEEE Standard 754 Floating Point Numbers
<URL:http://steve.hollasch.net/cgindex/coding/ieeefloat.html>

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>


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.