Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Lesson 4, Integ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 3701 of 3952
Post > Topic >>

Lesson 4, Integer Assignments, Unsigned to Signed with Mixed Sizes.

by "Skybuck Flying" <BloodyShame@[EMAIL PROTECTED] > Apr 19, 2008 at 05:08 AM

Hello,

See comments or see website for details:

http://members.home.nl/hbthouppermans/D2007Analysis/Index.htm

// *** Begin of AnalysisLesson4.dpr ***

program AnalysisLesson4;

{$APPTYPE CONSOLE}

{

Version 0.01 created on 19 april 2008 by Skybuck Flying

Lesson 4, Integer Assignments, Unsigned to Signed with Mixed Sizes.

Notes:

I like visual studio's pre-emptive warnings at compile time a little bit.
Delphi only warns at runtime... when extreme values encountered... which 
means bugs could slip past it.

Not so with visual studio's pre-emptive warnings. It can attract the 
attention of the programmer and force
the programmer to think about what he's doing and how to handle/prepare
for 
any extreme conditions...

However it can also be seen as nagging of the vistual studio compiler...
if 
it's never really a problem it's just
naggish... and typecasts would be needed to get rid of the nagging. Those 
typecasts might later actually introduce
bugs if data types are changed.

Conclusions:

BAD: 1 range detection failure for int8 := MaxUint64;
BAD: 1 range detection failure for int16 := MaxUint64;
BAD: 1 range detection failure for int32 := MaxUint64;
BAD: 2 spectacular range detection failures for int64 int64 :=
BigConstant; 
int64 := MaxUint64;
DOUBTFULL: 1 doubtfull range detection for in32 := BigConstant; compile 
detection failed, but run detection caught it.

Platform tested intel architecture 32.

42 out of 48 tests passed.
1 out of 48 tests doubtfull, but passed.
5 out of 48 tests failed.

Score:

8.95 out of 10

(0.20 better than c/c++)

}

uses
  SysUtils;

type
 Tint8 = shortint;
 Tuint8 = byte;

 Tint16 = smallint;
 Tuint16 = word;

 Tint32 = integer;
 Tuint32 = longword;

 Tint64 = int64;
// Tuint64 = int64; // uint64 is buggy, -1 point for delphi 2007.
 Tuint64 = uint64; // using buggy uint64 implementation for now just to
test 
ranges.

procedure Main;
var
 vMinInt8 : Tint8;
 vMaxInt8 : Tint8;

 vMinUint8 : Tuint8;
 vMaxUint8 : Tuint8;

 vMinInt16 : Tint16;
 vMaxInt16 : Tint16;

 vMinUint16 : Tuint16;
 vMaxUint16 : Tuint16;

 vMinInt32 : Tint32;
 vMaxInt32 : Tint32;

 vMinUint32 : Tuint32;
 vMaxUint32 : Tuint32;

 vMinInt64 : Tint64;
 vMaxInt64 : Tint64;

 vMinUint64 : Tuint64;
 vMaxUint64 : Tuint64;

 vInt8 : Tint8;
 vUint8 : Tuint8;

 vInt16 : Tint16;
 vUint16 : Tuint16;

 vInt32 : Tint32;
 vUint32 : Tuint32;

 vInt64 : Tint64;
 vUint64 : Tuint64;
begin
 writeln('Program Started');
 writeln;

 // setup min and max for int8
 vMinInt8 := -128;
 vMaxInt8 := 127;

 // setup min and max for uint8
 vMinUint8 := 0;
 vMaxUint8 := 255;

 // setup min and max for int16
 vMinInt16 := -32768;
 vMaxInt16 := 32767;

 // setup min and max for uint16
 vMinUint16 := 0;
 vMaxUint16 := 65535;

 // setup min and max for int32
 vMinInt32 := -2147483648;
 vMaxInt32 := 2147483647;

 // setup min and max for uint32
 vMinUint32 := 0;
 vMaxUint32 := 4294967295;

 // setup min and max for int64
 vMinInt64 := -9223372036854775808;
 vMaxInt64 := 9223372036854775807;

 // test min and max for uint64
 vMinUint64 := 0;
 vMaxUint64 := 18446744073709551615;

 // signed 8 bit = unsigned variable bit

 // TEST1
// vInt8 := vMinUint8; // Result: 0, OK

 // TEST2
// vInt8 := 200; // Error: Constant expression violates subrange bounds,
OK

 // TEST3
// vInt8 := vMaxUint8;  // Error: Range Check Error, OK

 // TEST4
// vInt8 := vMinUint16; // Result: 0, OK

 // TEST5
// vInt8 := 50000; // Error: Constant expression violates subrange bounds,

OK

 // TEST6
// vInt8 := vMaxUint16; // Error: Range Check Error, OK

 // TEST 7
// vInt8 := vMinUint32; // Result: 0, OK

 // TEST 8
// vInt8 := 3000000000; // Error: Constant expression violates subrange 
bounds, OK

 // TEST 9
// vInt8 := vMaxUint32; // Error: Range Check Error, OK

 // TEST 10
// vInt8 := vMinUint64; // Result: 0, OK

 // TEST 11
// vInt8 := 12345678901234567890; // Error: Constant expression violates 
subrange bounds, OK

 // TEST 12
// vInt8 := vMaxUint64; // result: -1, OK, NO RANGE CHECK ERROR, BAD

// writeln('vInt8: ', vInt8 );

 // signed 16 bit := unsigned variable bit

 // TEST 13
// vInt16 := vMinUint8; // Result: 0, OK

 // TEST 14
// vInt16 := 200;       // Result: 200, OK

 // TEST 15
// vInt16 := vMaxUint8; // Result: 255, OK

 // TEST 16
// vInt16 := vMinUint16; // Result: 0, OK

 // TEST 17
// vInt16 := 50000; // Error: Constant expression violates subrange
bounds, 
OK

 // TEST 18
// vInt16 := vMaxUint16; // Error: Range Check Error, OK

 // TEST 19
// vInt16 := vMinUint32; // Result: 0, OK

 // TEST 20
// vInt16 := 3000000000; // Error: Constant expression violates subrange 
bounds, OK

 // TEST 21
// vInt16 := vMaxUint32; // Error: Range Check Error, OK

 // TEST 22
// vInt16 := vMinUint64; // Result: 0, OK

 // TEST 23
// vInt16 := 12345678901234567890; // Error: Constant expression violates 
subrange bounds, OK

 // TEST 24
// vInt16 := vMaxUint64; // Result: -1, OK, NO RANGE CHECK ERROR, BAD

// writeln('vInt16: ', vInt16 );

 // signed 32 bit := unsigned variable bit

 // TEST 25
// vInt32 := vMinUint8; // Result: 0, OK

 // TEST 26
// vInt32 := 200; // Result: 200, OK

 // TEST 27
// vInt32 := vMaxUint8; // Result: 255, OK

 // TEST 28
// vInt32 := vMinUint16; // Result: 0, OK

 // TEST 29
// vInt32 := 50000;     // Result: 50000, OK

 // TEST 30
// vInt32 := vMaxUint16; // Result: 65535, OK

 // TEST 31
// vInt32 := vMinUint32; // Result: 0, OK

 // TEST 32
// vInt32 := 3000000000; // Error: Range Check Error, OK

 // TEST 33
// vInt32 := vMaxUint32; // Error: Range Check Error, OK

 // TEST 34
// vInt32 := vMinUint64; // Result: 0, OK

 // TEST 35
// vInt32 := 12345678901234567890; // Error: Range Check Error, 
INCONSISTENT, SEMI-OK, SEMI-BAD, BUT OK

 // TEST 36
// vInt32 := vMaxUint64; // Result: -1, NO RANGE CHECK ERROR, BAD

// writeln('vInt32: ', vInt32 );

 // signed 64 bit := unsigned variable bit

 // TEST 37
// vInt64 := vMinUint8; // Result: 0, OK

 // TEST 38
// vInt64 := 200; // Result: 200, OK

 // TEST 39
//    vInt64 := vMaxUint8; // Result: 255, OK

 // TEST 40
// vInt64 := vMinUint16; // Result: 0, OK

 // TEST 41
//    vInt64 := 50000; // Result: 50000, OK

 // TEST 42
// vInt64 := vMaxUint16; // Result: 65535, OK

 // TEST 43
// vInt64 := vMinUint32; // Result: 0, OK

 // TEST 44
// vInt64 := 3000000000; // Result: 3000000000, OK

 // TEST 45
// vInt64 := vMaxUint32; // Result: 4294967295, OK

 // TEST 46
// vInt64 := vMinUint64; // Result: 0, OK

 // TEST 47
// vInt64 := 12345678901234567890; // no warning, no error, SPECTACULAR 
FAILURE, VERY BAD
           // result: -6101065172474983726, OK

 // TEST 48
 vInt64 := vMaxUint64; // no warning, no error, SPECTACULAR FAILURE, VERY 
BAD
       // result: -1, OK

 writeln('vInt64: ', vInt64 );

 writeln('Program finished');
end;

begin
 try
  Main;
 except
  on E:Exception do
   Writeln(E.Classname, ': ', E.Message);
 end;
 readln;
end.

// *** End of AnalysisLesson4.dpr ***

Bye,
  Skybuck.
 




 1 Posts in Topic:
Lesson 4, Integer Assignments, Unsigned to Signed with Mixed Siz
"Skybuck Flying"  2008-04-19 05:08:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 20:10:29 CDT 2008.