www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - 0.92 trinary op bug (?:)

for some reason using ?: inside an if within a  struct member does not
work quite right!!
the follwoing code is a little long, this is just to show that outside
the logic is correct and that it works fine.

the output is 
+-+-++---+---++-+-+
|a|b||ffd|ffe||d|e|
+-+-++---+---++-+-+
|-|-|| 0 | 0 ||0|-|
|-|0|| - | - ||-|0|
|-|+|| - | - ||-|+|
|0|-|| + | + ||+|+|
|0|0|| 0 | 0 ||0|0|
|0|+|| - | - ||-|-|
|+|-|| + | + ||+|-|
|+|0|| + | + ||+|0|
|+|+|| 0 | 0 ||0|+|
+-+-++---+---++-+-+

all four outputs ffd, ffe, d, e SHOULD be the same, e is the result
from the TriBit.e function which is the same logic as ffe (which works
as expected).

compiled with DMD 0.92 on WinXP

---------------------------------------------------------------------------------------

import std.c.stdio;

struct TriBit {
	static const TriBit NEGV = { tb:-1 };
	static const TriBit ZERO = { tb:0  };
	static const TriBit PLUS = { tb:1  };
	static TriBit[] bitorder;
	///
	/// initialise the class data
	///
	static this() {
		bitorder.length = 0;
		bitorder ~= NEGV; bitorder ~= ZERO; bitorder ~= PLUS;
		TriBit[] tba = bitorder;
	}

	int tb;

	static TriBit d( TriBit a, TriBit b ) {
		if ( a.tb < 0 ) {
			if ( b.tb < 0 ) { return ZERO; }
			return NEGV;
		}
		if ( a.tb > 0 ) {
			if ( b.tb > 0 ) { return ZERO; }
			return PLUS;
		}
		if ( b.tb < 0 ) { return PLUS; }
		if ( b.tb > 0 ) { return NEGV; }
		return ZERO;
	}
	static TriBit e( TriBit a, TriBit b ) {
		if ( a.tb < 0 ) {
			return ( b.tb < 0 ) ? ZERO : NEGV;
		}
		if ( a.tb > 0 ) {
			return  ( b.tb > 0 ) ? ZERO : PLUS;
		}
		if ( b.tb < 0 ) { return PLUS; }
		if ( b.tb > 0 ) { return NEGV; }
		return ZERO;
	}

	char[] name() {
		if ( tb > 0 )      { return "+"; }
		else if ( tb < 0 ) { return "-"; }
		else               { return "0"; }
	}
}


int ffd( int a, int b ) {
		if ( a < 0 ) {
			if ( b < 0 ) { return 0; }
			return -1;
		}
		if ( a > 0 ) {
			if ( b > 0 ) { return 0; }
			return 1;
		}
		if ( b < 0 ) { return 1; }
		if ( b > 0 ) { return -1; }
		return 0;
}

int ffe( int a, int b ) {
		if ( a < 0 ) {
			return ( b < 0 ) ? 0 : -1;
		}
		if ( a > 0 ) {
			return  ( b > 0 ) ? 0 : 1;
		}
		if ( b < 0 ) { return 1; }
		if ( b > 0 ) { return -1; }
		return 0;
}

char[] name(int tb) {
	if ( tb > 0 )      { return "+"; }
	else if ( tb < 0 ) { return "-"; }
	else               { return "0"; }
}

int main( char[][] argv ) {

 	printf( "+-+-++---+---++-+-+\n" );
 	printf( "|a|b||ffd|ffe||d|e|\n" );
 	printf( "+-+-++---+---++-+-+\n" );
	foreach( TriBit a; TriBit.bitorder ) {
		foreach( TriBit b; TriBit.bitorder ) {
    	printf( "|%.*s|%.*s|| %.*s | %.*s ||%.*s|%.*s|\n",

a.name,

b.name,

name(ffd(a.tb, b.tb)),

name(ffe(a.tb, b.tb)),

(TriBit.d( a, b )).name,

(TriBit.e( a, b )).name

);
		}
	}
 	printf( "+-+-++---+---++-+-+\n" );

 	return 0;
}
Jun 12 2004