PC Review


Reply
Thread Tools Rate Thread

Compare a double to a float?

 
 
Andrew Murray
Guest
Posts: n/a
 
      14th Mar 2005
Is it possible to compare a double with a float in c# without casting to
one of the types first?

It appears you cannot...


float num = 1.545f;
double dnum = 1.545;

if (dnum == num)
{
Console.WriteLine("You can");
}
else
{
Console.WriteLine("You cant");
}
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      14th Mar 2005
Andrew,

No, you can not. It's like apples and oranges, you can't compare. You
have to convert one to the other before you compare.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Andrew Murray" <(E-Mail Removed)> wrote in message
news:d14fke$1rk$(E-Mail Removed)...
> Is it possible to compare a double with a float in c# without casting to
> one of the types first?
>
> It appears you cannot...
>
>
> float num = 1.545f;
> double dnum = 1.545;
>
> if (dnum == num)
> {
> Console.WriteLine("You can");
> }
> else
> {
> Console.WriteLine("You cant");
> }



 
Reply With Quote
 
Magnus Krisell
Guest
Posts: n/a
 
      14th Mar 2005
In addition to what Nicholas said, it almost never makes
sense to compare floating point variables for equality.
Instead, check if the difference is smaller than some value,
e.g.:

if (Math.Abs(num1 - num2) < 0.001)
....

- Magnus

"Andrew Murray" <(E-Mail Removed)> wrote in message
news:d14fke$1rk$(E-Mail Removed)...
> Is it possible to compare a double with a float in c# without casting to
> one of the types first?
>
> It appears you cannot...
>
>
> float num = 1.545f;
> double dnum = 1.545;
>
> if (dnum == num)
> {
> Console.WriteLine("You can");
> }
> else
> {
> Console.WriteLine("You cant");
> }



 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      14th Mar 2005

"Magnus Krisell" <net.magnuskrisell@mail> wrote in message
news:(E-Mail Removed)...
> In addition to what Nicholas said, it almost never makes
> sense to compare floating point variables for equality.
> Instead, check if the difference is smaller than some value,
> e.g.:
>
> if (Math.Abs(num1 - num2) < 0.001)


To refine this, the threshold for equality should be related to the size of
the numbers being compared. If num1 is .00002 and num2 is .001, they're
equal to zero significant digits, but they will satisfy the condition above.
If num1 is 6.123456 * 10**23 and num2 is 6.123457 * 10**23, they're equal to
6 significant digits, but don't satisfy it.




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float or double validity Imre Ament Microsoft VC .NET 1 20th Jan 2010 10:14 PM
Casting from a double to float -- please help! almurph@altavista.com Microsoft C# .NET 1 5th Mar 2009 01:56 PM
float vs double guy Microsoft C# .NET 5 22nd Oct 2004 09:52 AM
Float/Double Substraction Bug ? cybertof Microsoft C# .NET 4 26th Nov 2003 11:13 PM
double or float? James Thurley Microsoft C# .NET 8 31st Aug 2003 08:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:35 PM.