Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 05-08-2006, 08:18 AM   #26 (permalink)
Forum Newbie
 
Join Date: Jun 2005
Age: 31
Posts: 45
Default

Quote:
Originally Posted by Phantom
I am still going to say your incorrect for saying its false.
To end this academic debate:

First statement was: x == (y || z)
They wanted to replace it with: x == y || x == z

[b]I say that it's incorrect, becouse these two statements are mathe,atically inequal, and in some conditions they give different results.[b]

The correct mathematical replacement is this: (x & (y || z)) || (!x & (!y & !z))


Here is the proof - binary table for all cases.

x y z x == (y || z) x == y || x == z x & (y || z)] || [!x & (!y & !z)
0 0 0 1 1 1
0 1 0 0 1 0
0 0 1 0 1 0
0 1 1 0 0 0
1 0 0 0 0 0
1 1 0 1 1 1
1 0 1 1 1 1
1 1 1 1 1 1

So, as you can see, in case of 0|1|1 or 1|0|0 x==y||x==z gives incorrect result, and that is the thing that i originally wanted to point out.


Here is the c# script to test all cases:

(replace x,y,z with true|false as required)

using System;

namespace Test
{
public class MainApp
{
public static void Main()
{
bool x = true;
bool y = true;
bool z = true;

Console.WriteLine(x == (y || z));
Console.WriteLine(x == y || x == z);
Console.WriteLine((x & (y || z)) || (!x & (!y & !z)));

Console.WriteLine("--ende--");
Console.Read();
}
}
}

Last edited by yarex; 05-08-2006 at 08:22 AM.
yarex is offline   Reply With Quote
Old 05-08-2006, 09:11 AM   #27 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,289
Send a message via MSN to arul
Default

Quote:
Originally Posted by yarex
...snip...
The problem here is that Dipset doesnt want to compare boolean expression but object references, which is not possible because logical OR '||' needs a logical value on both sides.

I also noticed small mistake in your code
Code:
Console.WriteLine((x & (y || z)) || (!x & (!y & !z)));
Those should be logical AND '&&' and not bitwise AND '&'.

Back to the topic...
For comparation like X==(y||z ... ||n) you could use following method but it obvoiusly has its flaws on the preformance.
Code:
public static bool RefsEquals ( object o, params object[] objs )
{
     for ( int i = 0; i < objs.Length; i++ )
     {
          if ( o == objs[i] ) { return(true); } 
     }
 
     return(false);
}
And then you can call the method like this
Code:
if ( RefsEquals ( x, y, z ) )
....
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!

Last edited by arul; 05-08-2006 at 06:51 PM.
arul is offline   Reply With Quote
Old 05-08-2006, 06:40 PM   #28 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by arul
I also noticed small mistake in your code
Code:
Console.WriteLine((x & (y || z)) || (!x & (!y & !z)));
Those should be logical AND '&&' and not binary AND '&'.
A couple of notes, first, (at least in this usage) both '&&' and '&' are binary AND's, i.e. they take two operands each. The correct term for the usage of '&' that you're looking for is bitwise AND. Second, '&' performs the same operation as '&&' (sans short-circuit evaluation) when applied to bool values, of course, it's still actually computing the bitwise AND when applying it to bool values, but, due to the fact that there are only two values possible, it becomes the same operation as a logical AND. See MSDN for further details.

Last edited by Sep102; 05-08-2006 at 06:58 PM.
Sep102 is offline   Reply With Quote
Old 05-08-2006, 07:03 PM   #29 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,289
Send a message via MSN to arul
Default

Quote:
Originally Posted by Sep102
A couple of notes, first, (at least in this usage) both '&&' and '&' are binary AND's, i.e. they take two operands each. The correct term for the usage of '&' that you're looking for is bitwise AND
yeah, I meant 'bitwise', thanks for the catch.

Quote:
Originally Posted by Sep102
Second, '&' performs the same operation as '&&' (sans short-circuit evaluation) when applied to bool values, it only performs a bitwise AND when applied to operands of integral type. See MSDN for further details.
Sure thing, but obfuscating bitwise and logical operators is at least bad habit and significantly degrades readability of code.
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!
arul is offline   Reply With Quote
Old 05-09-2006, 04:02 AM   #30 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Quote:
Originally Posted by arul
Sure thing, but obfuscating bitwise and logical operators is at least bad habit and significantly degrades readability of code.
Oh, don't get me wrong, I wasn't disagreeing with you for pointing that out, just adding the fact that in that situation they both have the same semantics. I even asserted a point much like this one in my last post in this thread (which, by the way, is what I remember this from), so I do agree fully with your opinion on the matter.
Sep102 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5