Author: Gerd Isenberg
Date: 07:36:06 09/26/03
Go up one level in this thread
On September 26, 2003 at 07:47:21, Omid David Tabibi wrote: >On September 26, 2003 at 06:50:59, Gerd Isenberg wrote: > >>Hi All, >> >>in C++ we have the boolean type "bool" with the value range true/false. >>I'm not sure about ANSI-C. >> >>Due to some C-related "portability" problems and possible performance drawbacks >>due sizeof(bool) == 1 implies partial register handling or zero extending to >>native word lenght, you find in most MS-sources an "own" boolean type BOOL: >> >>typedef int BOOL; // e.g. WINDEF.H >> >>This own BOOL type is of course not "typesafe" as bool, as you may assign any >>"int" expressions to it. With BOOL one should interprete zero as FALSE and any >>other value as TRUE. Due to this ambiguity, comparing BOOL-expressions with TRUE >>may be erroneous, so better compare with != FALSE. > >I have experienced this problem in the past. It is one of the hardest problems >to debug since that's the last thing you suspect. Now I solve the problem as >follows: > >If something is BOOL, I never check the value with == TRUE or == FALSE, etc, but >use it as: > > if (flag) > > if (!flag) > >etc. Yes, that's what i do too. But there are still some pitfalls with BOOL variables, simply because BOOL is same as signed int. BOOL B1,B2; bool b1,b2; B1 = intExpression1; // should be avoided of course, use intExpression1 != 0 B2 = intExpression2; // or use double boolean negation (!!) to force 0,1 b1 = intExpression1; // same error, but compiler warns b2 = intExpression2; ASSERT ( (B1 == B2) == ( b1 == b2 ) ) >On the other hand when something is not boolean, I always test it by comparing >to a number. For example, I never do things like > > if (!depth) > >instead, I always use: > > if (depth == 0) > > >or instead of > > if (ply) > >use: > > if (ply != 0) > >etc. > I'm not quite sure about the exact type of relational and boolean expressions. I guess even in C++ it is "int" and not "bool". E.g. you may even count "true" expressions, to avoid some "inner" branches: E.g. instead of if ( ((a > b) && ( b > c ) && ( a != 0 )) ) probably faster if ( ((a > b) + ( b > c ) + ( a != 0 )) == 3 ) or if ( ((a > b) || ( b > c ) || ( a != 0 )) ) probably faster if ( ((a > b) + ( b > c ) + ( a != 0 )) != 0 ) That of course is not possible with real "bool". bool cond1 = (a > b); bool cond2 = (b > c); bool cond3 = (a != 0); if ( cond1 && cond2 && cond3 ) // that's fine if ( cond1 + cond2 + cond3 == 3) // hmm... bool arithmetic? Gerd <snip>
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.