|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem with simple calculationI am currently using the following
Studio 2005: 8.0.50727.42 (RTM.050727-4200) ..NET Framework: Version 2.0.50727 When doing the following in C# double a = 11.08; double b = 11.04; double c = a - b; I get 0.0400000000000009 instead of 0.04. Why is this? Am i missing something? Thanks SONET <SO***@discussions.microsoft.com> wrote:
Show quote > I am currently using the following Yes - how binary floating point works.> > Studio 2005: 8.0.50727.42 (RTM.050727-4200) > .NET Framework: Version 2.0.50727 > > When doing the following in C# > > double a = 11.08; > double b = 11.04; > double c = a - b; > > I get 0.0400000000000009 instead of 0.04. Why is this? > > Am i missing something? See http://www.pobox.com/~skeet/csharp/floatingpoint.html -- Jon Skeet - <sk***@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 Yes, You are missing something. Double is a floating radix datatype, as
defined by the IEEE 754 standard. It is useful for scientific calculations. If you intend to perform simple decimal calculations, I'd suggest that you use the Decimal datatype. The C# newsgroup FAQ is here: http://www.yoda.arachsys.com/csharp/faq/ This particular item is discussed (in significant detail) here: http://www.yoda.arachsys.com/csharp/floatingpoint.html -- Show quote--- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- "SONET" <SO***@discussions.microsoft.com> wrote in message news:3A802E39-28E9-4D8C-A369-AB121281B5F4@microsoft.com... >I am currently using the following > > Studio 2005: 8.0.50727.42 (RTM.050727-4200) > .NET Framework: Version 2.0.50727 > > When doing the following in C# > > double a = 11.08; > double b = 11.04; > double c = a - b; > > I get 0.0400000000000009 instead of 0.04. Why is this? > > Am i missing something? > > Thanks > |
|||||||||||||||||||||||