David, Randy,
One could also introduce some "delta" value and consider the values equal if
their difference is smaller than the delta. The delta can obviously be the
machine precision but it can also be bigger.
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"Randy A. Ynchausti" <randy_ynchausti@msn.com> wrote in message
news:uY3bYwZrDHA.920@TK2MSFTNGP10.phx.gbl...[color=blue]
> David,
>[color=green]
> > Does C# have the same problem as C++ with code like this?
> >
> > if (GetDouble1() == GetDouble2())
> > {
> > }
> >
> > In C++, due to the IEEE storage format, it is possible to have 2 doubles
> > that are essentially the same value but not quite, so the above code[/color][/color]
will[color=blue][color=green]
> > not do what you expect. Boost has some extensions to STL to solve this
> > problem.
> >
> > Does C# have similar problems? If so, what is the right C# way of[/color]
> handling[color=green]
> > this issue?[/color]
>
> Yes, floating-point (not just double precision) numbers/arithmethic has[/color]
this[color=blue]
> problem in virtually every language because of the representation that you
> have noted. One way of handling this is to calculate the precision of the
> machine that your code is running on and then do a comparison to see if[/color]
the[color=blue]
> difference is less than or equal to the precision of the machine. If it[/color]
is,[color=blue]
> then the values are identical as far as you can tell.
>
> Note that C# has the "Decimal" type that can help is many situations.
>
> Regards,
>
> Randy
>
>[/color]