IT Community - Software Programming, Web Development and Technical Support

C# math error??

This is a discussion on C# math error?? within the C# Programming forums, part of the Software Development category; After the fifth iteration, fC ends up with and additional 0.000037. This can be seen only in the debugger. ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 11-03-2007, 02:32 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default C# math error??

After the fifth iteration, fC ends up with and additional 0.000037. This can be seen only in the debugger. After the seventh iteration, an extra .0001 shows up in the console. Is this a math error in the language? I ran two versions of this code on two different computers, but they have the same processor

Code:
using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            float fC = 1000.00F;

            float fN = 23.23F;

            float fInterferer = 45.20F;

 

            for (int i = 0; i < 15; i++)

            {

                fC = fC - (fN + fInterferer);

                System.Console.Out.WriteLine( fC.ToString() );

            }

        }

    }

}
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-03-2007, 02:33 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Default Re: C# math error??

It looks like cumulative rounding errors to me. But, you're saying this only happens in the debug version of the program? That is truely strange.
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-04-2007, 11:55 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: C# math error??

Please read this: What Every Computer Scientist Should Know About Floating-Point Arithmetic

"What every computer scientist should know about floating-point arithmetic"



Basically the bottom line is trying to fit all floating point numbers into fixed 32-bit or 64-bit values in the range they support, is not possible. There are more numbers than 32/64 bits can truly represent. So you get an approximation at best. This is not a .NET / C# / or even a Java (since the doc is hosted at sun.com) specific problem -- all computer languages share it.



Since you're dealing with 2 places of decimals, I'd suggest you scale your numbers by 100 and just deal with simple integer arithmetic.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-04-2007, 11:57 PM
saloni srivatsava saloni srivatsava is offline
D-Web Trainee
 
Join Date: Sep 2007
Posts: 16
saloni srivatsava is on a distinguished road
Default Re: C# math error??

I'd follow eradicator's suggestion, or if you can't go to integer math, you can switch to decimal which is more accurate than float but has a different range (does not appear to be a problem).

the document referenced has nothing to do with Java, sun is more than just java (though that's what they are primarilly known for anyways).

Specifically it is an IEEE 754 (floating point) specification problem, encoding a decimal (base 10) number system in a binary (base 2) system. A fixed point system may not have the same issue, but it has it's own issues (very limited range, and just inability to represent certain values and percisions)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-05-2007, 12:01 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default Re: C# math error??

Thanks vinothkumar and saloni srivatsava. I read the document on the Sun website, and it helped me immensely. I also switch to using decimal in place of float. This solved my "rounding" error. I took this approach, because even though my example was to only 2 decimal places, the real data will have varying level. Thanks again.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB 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
Error: command line error MIDL1001 : cannot open input file wincodec.idl midl Mramesh C# Programming 0 02-18-2008 11:07 PM
Why do I get Error message "Unable to Start Debugging" Error Message? a.deeban ASP and ASP.NET Programming 5 09-25-2007 12:22 AM
.Debugging php ..What does this mean " Parse error: parse error, unexpected....... oxygen PHP Programming 1 07-26-2007 03:41 AM
Can I write sin(x) instead of Math.sin(x)? prasath Java Programming 2 07-20-2007 08:38 AM
I get the error "The page cannot be displayed" and an HTTP 502 Proxy Error. Why? kingmaker ASP and ASP.NET Programming 1 07-20-2007 04:43 AM


All times are GMT -7. The time now is 10:30 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0