Skybuck Flying
2015-09-22 09:26:00 UTC
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.
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.