What I was taught in school was not to use any floating point number for an
equal comparison and expect it to work ( <=, >=, ==). The reason is because
floating point values (floats, doubles, etc) are not stored with absolute
value. If you set a float equal to 1 (float f = 1; or float f = 1f;), the
value that is actually stored in memory could be .9999999999999 or
1.00000000000001, there is no guarantee. So if i am wanting to check two
floating point values, using the direct equals operator may end up comparing
two values are are just slightly off. This is a common problem in industries
such as banks that require calculations to be very precise (interest, etc).
This was not taught in a class that was specific to any language, it was
taught as a general principal. I assume this is just the way floating point
math is done (probably to same time).
There are two solutions:
- One solution to this is not to store the data in a data type that
approximates the value. In .Net the solution is the decimal class, it is
not approximated it is exact (In Java it would be BigDecimal). So decimal d
= 1; or decimal d = 1m; will have the value 1.00000000000000. The downfall
is the overhead to do the calculations, I also believe it takes up a little
more memory.
- The second solution is to compare the values with room for error.
EXAMPLE:
float x = 1f;
float y = 1f;
float tolerance = .00001f;
if( Math.Abs(x-y) < tolerance){
Console.WriteLine("Equal");
}else{
Console.WriteLine("Not Equal");
}
On Thu, Sep 4, 2008 at 11:17 PM, velsankar <[EMAIL PROTECTED]> wrote:
>
> A very basic problem, every one used to come across on our learning
> times itself (mostly overlooked & the result oriented attitude rather
> than why it is happening), still we have solution to avoid these.But
> anyone know why it is happening???? The problem is the following code
> only:
>
> for (float i = 10; i <= 11; i =i+ 0.1f)
> Console.WriteLine(i);
>
> We will get answer as 10,10.0,....,10.9
>
> Look @ the condition: it's <= & not <. Obviously, 11.0 should come.
> Why it is not coming?
>
> When I asked my profs on my college days(even seniors, expd's), they
> advised to use double. Of course, its a solution to get 11.0. But what
> happened to float? Is the float not precise???? For some days(truly in
> years), I believed it might be memory issue. Because when we see the
> entire 32 bytes of the increment float value, it is not increasing
> 0.10000000000.... but 0.10000010000 (The value is not constant, It can
> be in any one of the 256 bits). Recently I tested with fresh(means the
> first application to run after installed the windows embedded xp os)
> thin client. Shocked!!!! The end result is same:(
>
> Is it Really a memory problem? If so, how can we identify better than
> test as first application after os installed. Or the way I looking the
> problem???. And more it is not the language issue. Its starts with c
> only. Tested with c++, java also. I want to know the reason. Please
> don't suggest to use double, if so why it is happening with float &
> not with double???
>
> Think, its not looks like advanced level programming, But the issue @
> core, fundamentals, how far we are understanding our process. Hope I
> will get the genuine cause.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://cm.megasolutions.net/forums/default.aspx
-~----------~----~----~----~------~----~------~--~---