Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nasty behaviour of Delphi in boolean typecasts (constants optimization ?!)

3 views
Skip to first unread message

Skybuck Flying

unread,
Sep 22, 2015, 5:26:01 AM9/22/15
to
Hello,

As you may know I do not like inconsistent behaviour in computer languages.

This is a nice example of inconstant behaviour (in Delphi) and how constant
optimization in a typecast can lead to confusing results !

Perhaps somebody should file a bug report.

procedure Example;
var
x : integer;
begin
// so tests:
x := 123;
writeln( integer( not boolean(x) ) ); // should produce a 0, actually
produces a 122

x := -123;
writeln( integer( not boolean(-x) ) ); // should produce a 0, actually
produces a 122

x := 123;
writeln( integer( not not boolean(x) ) ); // should produce a 1, actually
produces a 123

x := -123;
writeln( integer( not not boolean(x) ) ); // should produce a 1, actually
produces a 5

x := 0;
writeln( integer( not boolean(x) ) ); // should produce a 1, actually
produces a 1

end;

procedure Main;
var
vIndex : integer;
begin

writeln( integer( not True) ); // produces 0
writeln( integer( not False) ); // produces 1

writeln( integer( not 1) ); // produces -2
writeln( integer( not 0) ); // produces -1

// ^ Kinda funny :)

// let's see if we can use this theory to produce a 0 or 1 for any given
value
// just like C's not. cause that's the problem... we don't know how to do
that in Delphi
// so know we may have learned it ! ;) =D

// let's assume we have some large positive value, inverting this should
produce a 0.
// let's assume we have some large negative value, inverting this should
produce a 1
// let's assume we have a zero, inverting this should produce a 1
// let's assume we have a one, inverting this should produce a 0

// so tests:
writeln( integer( not boolean(123) ) ); // should produce a 0, actually
produces a 0
writeln( integer( not boolean(-123) ) ); // should produce a 0, actually
produces a 0

writeln( integer( not not boolean(123) ) ); // should produce a 1, actually
produces a 1
writeln( integer( not not boolean(-123) ) ); // should produce a 1, actually
produces a 1

writeln( integer( not boolean(0) ) ); // should produce a 1, actually
produces a 1

// ^ problem solved ! by using typecasts correctly we should be able to
solve this problem in Delphi ! ;) =D

// it was just an illusion lol ! ;) Delphi probably optimized the constants
above ! ;) :)
writeln('example');
Example;
writeln('example');

end;

Bye,
Skybuck.

dunno

unread,
Sep 22, 2015, 9:47:26 AM9/22/15
to
"Skybuck Flying" <skybu...@hotmail.com> wrote:
> Hello,
>
> As you may know I do not like inconsistent behaviour in computer languages.
>
> This is a nice example of inconstant behaviour (in Delphi) and how
> constant optimization in a typecast can lead to confusing results !
>
> Perhaps somebody should file a bug report.

I would ask different question: why are you using Delphi?
--
dunno

Skybuck Flying

unread,
Sep 23, 2015, 4:57:49 AM9/23/15
to
"
I would ask different question: why are you using Delphi?
"

Sometimes I wonder that too =D

Here are some answers:

1. Super fast compiler allows super fast debugging and writing super fast
nearly to flawless code.

^ Big plusses there for Delphi.

Since I am most concerned about writing flawless code/new algorithm, Delphi
is the best tool out there ! ;)

Bye,
Skybuck.

JJ

unread,
Sep 23, 2015, 11:51:47 AM9/23/15
to
I didn't know it has that bug. But that's probably I rarely need to convert
an integer to boolean, and back to integer. Mostly I just do it like this.

writeln(ord(x = 0)); //for writeln(integer(not boolean(x)));
No it doesn't. The problem occurs when typecasting a variable. Typecasting a
constant won't have that problem.

> // it was just an illusion lol ! ;) Delphi probably optimized the constants
> above ! ;) :)

That's not the problem too, IMO. The code optimization compiler directive
doesn't affect this problem. The problem seems to be in the compiler itself.
i.e. how it handles and compiles typecasted variable/constant.

Whe I see it at assembly level, boolean-typecast doesn't actually convert
the value to either 0 or 1. And if the value is a variable, boolean
operators will be performed only on the least significant bit of the value
(bit 0). So, a "not boolean(x)" will simply be "boolean(x xor 1)".
0 new messages